mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {
2 AppBskyFeedDefs,
3 AppBskyFeedGetFeed as GetCustomFeed,
4} from '@atproto/api'
5import {RootStoreModel} from 'state/index'
6import {FeedAPI, FeedAPIResponse} from './types'
7
8export class CustomFeedAPI implements FeedAPI {
9 cursor: string | undefined
10
11 constructor(
12 public rootStore: RootStoreModel,
13 public params: GetCustomFeed.QueryParams,
14 ) {}
15
16 reset() {
17 this.cursor = undefined
18 }
19
20 async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
21 const res = await this.rootStore.agent.app.bsky.feed.getFeed({
22 ...this.params,
23 limit: 1,
24 })
25 return res.data.feed[0]
26 }
27
28 async fetchNext({limit}: {limit: number}): Promise<FeedAPIResponse> {
29 const res = await this.rootStore.agent.app.bsky.feed.getFeed({
30 ...this.params,
31 cursor: this.cursor,
32 limit,
33 })
34 if (res.success) {
35 this.cursor = res.data.cursor
36 // NOTE
37 // some custom feeds fail to enforce the pagination limit
38 // so we manually truncate here
39 // -prf
40 if (res.data.feed.length > limit) {
41 res.data.feed = res.data.feed.slice(0, limit)
42 }
43 return {
44 cursor: res.data.cursor,
45 feed: res.data.feed,
46 }
47 }
48 return {
49 feed: [],
50 }
51 }
52}