Something went wrong. Try again.
cee sky
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207import { type Client, type XrpcRequestParams, XrpcResponseError,} from '@atproto/lex'
import {PUBLIC_APPVIEW} from '#/lib/constants'import {createLexClient} from '#/lib/lexClient'import { getAppLanguageAsContentLanguage, getContentLanguages,} from '#/state/preferences/languages'import {app} from '#/lexicons'import {type FeedAPI, type FeedAPIResponse} from './types'import { createBskyFeedHeaders, createBskyTopicsHeader, isBlueskyOwnedFeed,} from './utils'
type GetCustomFeedParams = XrpcRequestParams<typeof app.bsky.feed.getFeed.main>
export class CustomFeedAPI implements FeedAPI { client: Client params: GetCustomFeedParams userInterests?: string
constructor({ client, feedParams, userInterests, }: { client: Client feedParams: GetCustomFeedParams userInterests?: string }) { this.client = client this.params = feedParams this.userInterests = userInterests }
async peekLatest(): Promise<app.bsky.feed.defs.FeedViewPost> { const contentLangs = getContentLanguages().join(',') const data = await this.client.call( app.bsky.feed.getFeed, { ...this.params, limit: 1, }, { headers: { ...createBskyFeedHeaders(this.params.feed), 'Accept-Language': contentLangs, }, }, ) return data.feed[0] }
async fetch({ cursor, limit, signal, }: { cursor: string | undefined limit: number signal?: AbortSignal }): Promise<FeedAPIResponse> { const contentLangs = getContentLanguages().join(',') const isBlueskyOwned = isBlueskyOwnedFeed(this.params.feed)
/* * The authed branch rejects on failure, so the error propagates to the * query and drives the feed error UI (feedgen offline, misconfigured, rate * limited). Only the logged-out branch can resolve without data, and it * signals that with a null body. */ const data = this.client.did ? await this.client.call( app.bsky.feed.getFeed, { ...this.params, cursor, limit, }, { signal, headers: { ...createBskyFeedHeaders(this.params.feed), ...(isBlueskyOwned ? createBskyTopicsHeader(this.userInterests) : {}), 'Accept-Language': contentLangs, }, }, ) : await loggedOutFetch({...this.params, cursor, limit}, signal)
if (!data) { return { feed: [], } }
// NOTE // some custom feeds fail to enforce the pagination limit // so we manually truncate here // -prf const feed = data.feed.length > limit ? data.feed.slice(0, limit) : data.feed return { cursor: data.cursor, feed, } }}
let loggedOutAppviewClient: Client | undefined
/** * The unauthenticated {@link Client} for logged-out feed reads, pointed at the * direct appview ({@link PUBLIC_APPVIEW}, `api.bsky.app`). * * Deliberately NOT the public appview client (`public.api.bsky.app`): that host * fronts a cache which does not vary on `Accept-Language`, so it would answer a * language-filtered read from another language's cached body. The direct * appview respects the header (verified 2026-08-04), at the cost of not being * cached. See {@link loggedOutFetch}. * * A single module-level instance, because there is no session to scope it to. * Like the public chat client, it uses plain `fetch` rather than * `networkAwareFetch`, matching the ad-hoc fetch it replaces: this read has its * own failure handling and should not move the app-wide network signal. */function getLoggedOutAppviewClient(): Client { return (loggedOutAppviewClient ??= createLexClient( {service: PUBLIC_APPVIEW}, {includeDeviceSessionHeaders: true}, ))}
/* * HACK * We want feeds to give language-specific results immediately when a logged-out * user changes their language. That comes with two problems: * 1. not all languages have content, and * 2. our public caching layer does not bust against the `Accept-Language` * header. * -prf * * Problem 2 is why this uses its own client rather than the app's public * appview one: it talks to the direct appview, which honors the header, instead * of the cached `public.api.bsky.app`, which does not vary on it. That trades * CDN caching for language correctness on logged-out feed traffic. * * Problem 1 is host-independent, so it is still handled here: an empty * language-filtered feed is retried once with the language constraint removed. */async function loggedOutFetch( params: GetCustomFeedParams, signal?: AbortSignal,): Promise<app.bsky.feed.getFeed.$OutputBody | null> { const contentLangs = getAppLanguageAsContentLanguage()
let data = await getFeedOrNull(params, contentLangs, signal) if (data?.feed?.length) { return data }
// no data, try again with language headers removed data = await getFeedOrNull(params, '', signal) return data}
/** * A logged-out `getFeed` read that resolves to null on a response error. * * The pre-client code only guarded `res.ok`, so a failed RESPONSE fell through * to the next attempt while a failed REQUEST rejected. Catching * `XrpcResponseError` preserves that split: every other lex error - the fetch * and validation ones - still propagates. */async function getFeedOrNull( params: GetCustomFeedParams, contentLangs: string, signal?: AbortSignal,): Promise<app.bsky.feed.getFeed.$OutputBody | null> { try { return await getLoggedOutAppviewClient().call( app.bsky.feed.getFeed, params, { signal, headers: { ...createBskyFeedHeaders(params.feed), 'Accept-Language': contentLangs, }, }, ) } catch (e) { if (e instanceof XrpcResponseError) { return null } throw e }}