Skip to content

Commit

Permalink
Apply labelers and handle language for PWI home (#5816)
Browse files Browse the repository at this point in the history
(cherry picked from commit 05ca979)
  • Loading branch information
estrattonbailey authored and haileyok committed Oct 17, 2024
1 parent 1a99242 commit 754cb25
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/lib/api/feed/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import {
jsonStringToLex,
} from '@atproto/api'

import {getContentLanguages} from '#/state/preferences/languages'
import {
getAppLanguageAsContentLanguage,
getContentLanguages,
} from '#/state/preferences/languages'
import {FeedAPI, FeedAPIResponse} from './types'
import {createBskyTopicsHeader, isBlueskyOwnedFeed} from './utils'

Expand Down Expand Up @@ -103,14 +106,27 @@ async function loggedOutFetch({
limit: number
cursor?: string
}) {
let contentLangs = getContentLanguages().join(',')
let contentLangs = getAppLanguageAsContentLanguage()

/**
* Copied from our root `Agent` class
* @see https://github.com/bluesky-social/atproto/blob/60df3fc652b00cdff71dd9235d98a7a4bb828f05/packages/api/src/agent.ts#L120
*/
const labelersHeader = {
'atproto-accept-labelers': BskyAgent.appLabelers
.map(l => `${l};redact`)
.join(', '),
}

// manually construct fetch call so we can add the `lang` cache-busting param
let res = await fetch(
`https://api.bsky.app/xrpc/app.bsky.feed.getFeed?feed=${feed}${
cursor ? `&cursor=${cursor}` : ''
}&limit=${limit}&lang=${contentLangs}`,
{method: 'GET', headers: {'Accept-Language': contentLangs}},
{
method: 'GET',
headers: {'Accept-Language': contentLangs, ...labelersHeader},
},
)
let data = res.ok ? jsonStringToLex(await res.text()) : null
if (data?.feed?.length) {
Expand All @@ -125,7 +141,7 @@ async function loggedOutFetch({
`https://api.bsky.app/xrpc/app.bsky.feed.getFeed?feed=${feed}${
cursor ? `&cursor=${cursor}` : ''
}&limit=${limit}`,
{method: 'GET', headers: {'Accept-Language': ''}},
{method: 'GET', headers: {'Accept-Language': '', ...labelersHeader}},
)
data = res.ok ? jsonStringToLex(await res.text()) : null
if (data?.feed?.length) {
Expand Down
10 changes: 10 additions & 0 deletions src/state/preferences/languages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ export function getContentLanguages() {
return persisted.get('languagePrefs').contentLanguages
}

/**
* Be careful with this. It's used for the PWI home screen so that users can
* select a UI language and have it apply to the fetched Discover feed.
*
* We only support BCP-47 two-letter codes here, hence the split.
*/
export function getAppLanguageAsContentLanguage() {
return persisted.get('languagePrefs').appLanguage.split('-')[0]
}

export function toPostLanguages(postLanguage: string): string[] {
// filter out empty strings if exist
return postLanguage.split(',').filter(Boolean)
Expand Down

0 comments on commit 754cb25

Please sign in to comment.