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