Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242import { type AppBskyFeedDefs, type AppBskyFeedGetFeed as GetCustomFeed, AtpAgent, jsonStringToLex,} from '@atproto/api'
import { type CommunityFeedConfig, fetchCommunityFeedConfigStrict, fetchFeedServiceDid, isSpaceBackedFeed,} from '#/lib/api/community-feed'import {getServiceAuthToken} from '#/lib/api/service-auth'import {fetchSpaceFeed, signInRequiredError} from '#/lib/api/space-feed'import { getAppLanguageAsContentLanguage, getContentLanguages,} from '#/state/preferences/languages'import {type FeedAPI, type FeedAPIResponse} from './types'import { createBskyTopicsHeader, getProxyHeadersForFeed, isBlueskyOwnedFeed,} from './utils'
export class CustomFeedAPI implements FeedAPI { agent: AtpAgent params: GetCustomFeed.QueryParams userInterests?: string communityConfig: Promise<CommunityFeedConfig | null> feedServiceDid: Promise<string | null>
constructor({ agent, feedParams, userInterests, }: { agent: AtpAgent feedParams: GetCustomFeed.QueryParams userInterests?: string }) { this.agent = agent this.params = feedParams this.userInterests = userInterests this.communityConfig = fetchCommunityFeedConfigStrict( agent, feedParams.feed, ) this.feedServiceDid = this.communityConfig .then(config => config ? fetchFeedServiceDid(agent, feedParams.feed) : null, ) .catch(() => null) }
async communityAuthHeaders(): Promise<Record<string, string>> { const [config, serviceDid] = await Promise.all([ this.communityConfig, this.feedServiceDid, ]) if (!config || !serviceDid) return {} const token = await getServiceAuthToken({ agent: this.agent, aud: serviceDid, lxm: 'app.bsky.feed.getFeedSkeleton', exp: Math.floor(Date.now() / 1000) + 60, }) return {Authorization: `Bearer ${token}`} }
/** * A space-backed feed never reaches the standard route, in either method. * `app.bsky.feed.getFeed` now refuses it outright, so a poller that skipped * this branch would error on every tick, and the logged-out fallback below * would ask the public appview for private content. */ private async spaceFeed(): Promise<string | null> { const config = await this.communityConfig return isSpaceBackedFeed(config) ? this.params.feed : null }
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> { const spaceFeed = await this.spaceFeed() if (spaceFeed) { if (!this.agent.did) throw signInRequiredError() const page = await fetchSpaceFeed(this.agent, spaceFeed, {limit: 1}) return page.feed[0] } const contentLangs = getContentLanguages().join(',') const communityAuthHeaders = this.agent.did ? await this.communityAuthHeaders() : {} const res = await this.agent.app.bsky.feed.getFeed( { ...this.params, limit: 1, }, { headers: { ...getProxyHeadersForFeed(this.params.feed), ...communityAuthHeaders, 'Accept-Language': contentLangs, }, }, ) return res.data.feed[0] }
async fetch({ cursor, limit, }: { cursor: string | undefined limit: number }): Promise<FeedAPIResponse> { const spaceFeed = await this.spaceFeed() if (spaceFeed) { if (!this.agent.did) throw signInRequiredError() const page = await fetchSpaceFeed(this.agent, spaceFeed, {cursor, limit}) return { cursor: page.feed.length ? page.cursor : undefined, feed: page.feed, } }
const contentLangs = getContentLanguages().join(',') const agent = this.agent const isBlueskyOwned = isBlueskyOwnedFeed(this.params.feed) const communityAuthHeaders = agent.did ? await this.communityAuthHeaders() : {}
const res = agent.did ? await this.agent.app.bsky.feed.getFeed( { ...this.params, cursor, limit, }, { headers: { ...getProxyHeadersForFeed(this.params.feed), ...communityAuthHeaders, ...(isBlueskyOwned ? createBskyTopicsHeader(this.userInterests) : {}), 'Accept-Language': contentLangs, }, }, ) : await loggedOutFetch({...this.params, cursor, limit}) if (res.success) { // NOTE // some custom feeds fail to enforce the pagination limit // so we manually truncate here // -prf if (res.data.feed.length > limit) { res.data.feed = res.data.feed.slice(0, limit) } return { cursor: res.data.feed.length ? res.data.cursor : undefined, feed: res.data.feed, } } return { feed: [], } }}
// HACK// we want feeds to give language-specific results immediately when a// logged-out user changes their language. this comes with two problems:// 1. not all languages have content, and// 2. our public caching layer isnt correctly busting against the accept-language header// for now we handle both of these with a manual workaround// -prfasync function loggedOutFetch({ feed, limit, cursor,}: { feed: string limit: number cursor?: string}) { 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': AtpAgent.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, ...labelersHeader}, }, ) let data = res.ok ? (jsonStringToLex(await res.text()) as GetCustomFeed.OutputSchema) : null if (data?.feed?.length) { return { success: true, data, } }
// no data, try again with language headers removed res = await fetch( `https://api.bsky.app/xrpc/app.bsky.feed.getFeed?feed=${feed}${ cursor ? `&cursor=${cursor}` : '' }&limit=${limit}`, {method: 'GET', headers: {'Accept-Language': '', ...labelersHeader}}, ) data = res.ok ? (jsonStringToLex(await res.text()) as GetCustomFeed.OutputSchema) : null if (data?.feed?.length) { return { success: true, data, } }
return { success: false, data: {feed: []}, }}