Skip to content

Commit

Permalink
[Session] Drill getAgent into feed APIs (#3701)
Browse files Browse the repository at this point in the history
* Update to desired post-feed usage

* Drill agent into feed apis

* Thread getAgent instead

---------

Co-authored-by: Dan Abramov <[email protected]>
  • Loading branch information
estrattonbailey and gaearon authored Apr 25, 2024
1 parent 282ad4b commit ec37696
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 65 deletions.
21 changes: 17 additions & 4 deletions src/lib/api/feed/author.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import {
AppBskyFeedDefs,
AppBskyFeedGetAuthorFeed as GetAuthorFeed,
BskyAgent,
} from '@atproto/api'

import {FeedAPI, FeedAPIResponse} from './types'
import {getAgent} from '#/state/session'

export class AuthorFeedAPI implements FeedAPI {
constructor(public params: GetAuthorFeed.QueryParams) {}
getAgent: () => BskyAgent
params: GetAuthorFeed.QueryParams

constructor({
getAgent,
feedParams,
}: {
getAgent: () => BskyAgent
feedParams: GetAuthorFeed.QueryParams
}) {
this.getAgent = getAgent
this.params = feedParams
}

async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
const res = await getAgent().getAuthorFeed({
const res = await this.getAgent().getAuthorFeed({
...this.params,
limit: 1,
})
Expand All @@ -23,7 +36,7 @@ export class AuthorFeedAPI implements FeedAPI {
cursor: string | undefined
limit: number
}): Promise<FeedAPIResponse> {
const res = await getAgent().getAuthorFeed({
const res = await this.getAgent().getAuthorFeed({
...this.params,
cursor,
limit,
Expand Down
28 changes: 22 additions & 6 deletions src/lib/api/feed/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@ import {
AppBskyFeedDefs,
AppBskyFeedGetFeed as GetCustomFeed,
AtpAgent,
BskyAgent,
} from '@atproto/api'

import {getContentLanguages} from '#/state/preferences/languages'
import {getAgent} from '#/state/session'
import {FeedAPI, FeedAPIResponse} from './types'

export class CustomFeedAPI implements FeedAPI {
constructor(public params: GetCustomFeed.QueryParams) {}
getAgent: () => BskyAgent
params: GetCustomFeed.QueryParams

constructor({
getAgent,
feedParams,
}: {
getAgent: () => BskyAgent
feedParams: GetCustomFeed.QueryParams
}) {
this.getAgent = getAgent
this.params = feedParams
}

async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
const contentLangs = getContentLanguages().join(',')
const res = await getAgent().app.bsky.feed.getFeed(
const res = await this.getAgent().app.bsky.feed.getFeed(
{
...this.params,
limit: 1,
Expand All @@ -31,15 +43,19 @@ export class CustomFeedAPI implements FeedAPI {
limit: number
}): Promise<FeedAPIResponse> {
const contentLangs = getContentLanguages().join(',')
const agent = getAgent()
const agent = this.getAgent()
const res = agent.session
? await getAgent().app.bsky.feed.getFeed(
? await this.getAgent().app.bsky.feed.getFeed(
{
...this.params,
cursor,
limit,
},
{headers: {'Accept-Language': contentLangs}},
{
headers: {
'Accept-Language': contentLangs,
},
},
)
: await loggedOutFetch({...this.params, cursor, limit})
if (res.success) {
Expand Down
14 changes: 9 additions & 5 deletions src/lib/api/feed/following.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import {AppBskyFeedDefs} from '@atproto/api'
import {AppBskyFeedDefs, BskyAgent} from '@atproto/api'

import {FeedAPI, FeedAPIResponse} from './types'
import {getAgent} from '#/state/session'

export class FollowingFeedAPI implements FeedAPI {
constructor() {}
getAgent: () => BskyAgent

constructor({getAgent}: {getAgent: () => BskyAgent}) {
this.getAgent = getAgent
}

async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
const res = await getAgent().getTimeline({
const res = await this.getAgent().getTimeline({
limit: 1,
})
return res.data.feed[0]
Expand All @@ -19,7 +23,7 @@ export class FollowingFeedAPI implements FeedAPI {
cursor: string | undefined
limit: number
}): Promise<FeedAPIResponse> {
const res = await getAgent().getTimeline({
const res = await this.getAgent().getTimeline({
cursor,
limit,
})
Expand Down
27 changes: 18 additions & 9 deletions src/lib/api/feed/home.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {AppBskyFeedDefs} from '@atproto/api'
import {FeedAPI, FeedAPIResponse} from './types'
import {FollowingFeedAPI} from './following'
import {CustomFeedAPI} from './custom'
import {AppBskyFeedDefs, BskyAgent} from '@atproto/api'

import {PROD_DEFAULT_FEED} from '#/lib/constants'
import {CustomFeedAPI} from './custom'
import {FollowingFeedAPI} from './following'
import {FeedAPI, FeedAPIResponse} from './types'

// HACK
// the feed API does not include any facilities for passing down
Expand All @@ -26,19 +27,27 @@ export const FALLBACK_MARKER_POST: AppBskyFeedDefs.FeedViewPost = {
}

export class HomeFeedAPI implements FeedAPI {
getAgent: () => BskyAgent
following: FollowingFeedAPI
discover: CustomFeedAPI
usingDiscover = false
itemCursor = 0

constructor() {
this.following = new FollowingFeedAPI()
this.discover = new CustomFeedAPI({feed: PROD_DEFAULT_FEED('whats-hot')})
constructor({getAgent}: {getAgent: () => BskyAgent}) {
this.getAgent = getAgent
this.following = new FollowingFeedAPI({getAgent})
this.discover = new CustomFeedAPI({
getAgent,
feedParams: {feed: PROD_DEFAULT_FEED('whats-hot')},
})
}

reset() {
this.following = new FollowingFeedAPI()
this.discover = new CustomFeedAPI({feed: PROD_DEFAULT_FEED('whats-hot')})
this.following = new FollowingFeedAPI({getAgent: this.getAgent})
this.discover = new CustomFeedAPI({
getAgent: this.getAgent,
feedParams: {feed: PROD_DEFAULT_FEED('whats-hot')},
})
this.usingDiscover = false
this.itemCursor = 0
}
Expand Down
21 changes: 17 additions & 4 deletions src/lib/api/feed/likes.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import {
AppBskyFeedDefs,
AppBskyFeedGetActorLikes as GetActorLikes,
BskyAgent,
} from '@atproto/api'

import {FeedAPI, FeedAPIResponse} from './types'
import {getAgent} from '#/state/session'

export class LikesFeedAPI implements FeedAPI {
constructor(public params: GetActorLikes.QueryParams) {}
getAgent: () => BskyAgent
params: GetActorLikes.QueryParams

constructor({
getAgent,
feedParams,
}: {
getAgent: () => BskyAgent
feedParams: GetActorLikes.QueryParams
}) {
this.getAgent = getAgent
this.params = feedParams
}

async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
const res = await getAgent().getActorLikes({
const res = await this.getAgent().getActorLikes({
...this.params,
limit: 1,
})
Expand All @@ -23,7 +36,7 @@ export class LikesFeedAPI implements FeedAPI {
cursor: string | undefined
limit: number
}): Promise<FeedAPIResponse> {
const res = await getAgent().getActorLikes({
const res = await this.getAgent().getActorLikes({
...this.params,
cursor,
limit,
Expand Down
21 changes: 17 additions & 4 deletions src/lib/api/feed/list.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import {
AppBskyFeedDefs,
AppBskyFeedGetListFeed as GetListFeed,
BskyAgent,
} from '@atproto/api'

import {FeedAPI, FeedAPIResponse} from './types'
import {getAgent} from '#/state/session'

export class ListFeedAPI implements FeedAPI {
constructor(public params: GetListFeed.QueryParams) {}
getAgent: () => BskyAgent
params: GetListFeed.QueryParams

constructor({
getAgent,
feedParams,
}: {
getAgent: () => BskyAgent
feedParams: GetListFeed.QueryParams
}) {
this.getAgent = getAgent
this.params = feedParams
}

async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
const res = await getAgent().app.bsky.feed.getListFeed({
const res = await this.getAgent().app.bsky.feed.getListFeed({
...this.params,
limit: 1,
})
Expand All @@ -23,7 +36,7 @@ export class ListFeedAPI implements FeedAPI {
cursor: string | undefined
limit: number
}): Promise<FeedAPIResponse> {
const res = await getAgent().app.bsky.feed.getListFeed({
const res = await this.getAgent().app.bsky.feed.getListFeed({
...this.params,
cursor,
limit,
Expand Down
Loading

0 comments on commit ec37696

Please sign in to comment.