Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108import {type AppBskyFeedDefs, type AtpAgent} from '@atproto/api'
import {PROD_DEFAULT_FEED} from '#/lib/constants'import {CustomFeedAPI} from './custom'import {FollowingFeedAPI} from './following'import {type FeedAPI, type FeedAPIResponse} from './types'
// HACK// the feed API does not include any facilities for passing down// non-post elements. adding that is a bit of a heavy lift, and we// have just one temporary usecase for it: flagging when the home feed// falls back to discover.// we use this fallback marker post to drive this instead. see Feed.tsx// for the usage.// -prfexport const FALLBACK_MARKER_POST: AppBskyFeedDefs.FeedViewPost = { post: { uri: 'fallback-marker-post', cid: 'fake', record: {}, author: { did: 'did:fake', handle: 'fake.com', }, indexedAt: new Date().toISOString(), },}
export class HomeFeedAPI implements FeedAPI { agent: AtpAgent following: FollowingFeedAPI discover: CustomFeedAPI usingDiscover = false itemCursor = 0 userInterests?: string
constructor({ userInterests, agent, }: { userInterests?: string agent: AtpAgent }) { this.agent = agent this.following = new FollowingFeedAPI({agent}) this.discover = new CustomFeedAPI({ agent, feedParams: {feed: PROD_DEFAULT_FEED('blacksky-trend')}, }) this.userInterests = userInterests }
reset() { this.following = new FollowingFeedAPI({agent: this.agent}) this.discover = new CustomFeedAPI({ agent: this.agent, feedParams: {feed: PROD_DEFAULT_FEED('blacksky-trend')}, userInterests: this.userInterests, }) this.usingDiscover = false this.itemCursor = 0 }
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> { if (this.usingDiscover) { return this.discover.peekLatest() } return this.following.peekLatest() }
async fetch({ cursor, limit, }: { cursor: string | undefined limit: number }): Promise<FeedAPIResponse> { if (!cursor) { this.reset() }
let returnCursor let posts: AppBskyFeedDefs.FeedViewPost[] = []
if (!this.usingDiscover) { const res = await this.following.fetch({cursor, limit}) returnCursor = res.cursor posts = posts.concat(res.feed) if (!returnCursor) { cursor = '' posts.push(FALLBACK_MARKER_POST) this.usingDiscover = true } }
if (this.usingDiscover && !__DEV__) { const res = await this.discover.fetch({cursor, limit}) returnCursor = res.cursor posts = posts.concat(res.feed) }
return { cursor: returnCursor, feed: posts, } }}