about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEric Bailey <git@esb.lol>2024-10-17 15:28:52 -0500
committerGitHub <noreply@github.com>2024-10-17 15:28:52 -0500
commit05ca979518f5d5851178aaa69469305db805b95b (patch)
tree9b13e8b34ee92b9608efc06c9db853e01d46ede8
parente9be8f4574ad5e2da145b24a2af4258fd4d7769a (diff)
downloadvoidsky-05ca979518f5d5851178aaa69469305db805b95b.tar.zst
Apply labelers and handle language for PWI home (#5816)
-rw-r--r--src/lib/api/feed/custom.ts24
-rw-r--r--src/state/preferences/languages.tsx10
2 files changed, 30 insertions, 4 deletions
diff --git a/src/lib/api/feed/custom.ts b/src/lib/api/feed/custom.ts
index f3ac45b6e..c5e0a35f5 100644
--- a/src/lib/api/feed/custom.ts
+++ b/src/lib/api/feed/custom.ts
@@ -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'
 
@@ -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) {
@@ -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) {
diff --git a/src/state/preferences/languages.tsx b/src/state/preferences/languages.tsx
index 5093cd725..8d705bf19 100644
--- a/src/state/preferences/languages.tsx
+++ b/src/state/preferences/languages.tsx
@@ -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)