Replace getAgent() with reading agent (#4243)

* Replace getAgent() with agent

* Replace {agent} with agent
This commit is contained in:
dan 2024-05-28 16:37:51 +01:00 committed by GitHub
parent 8a2f43c218
commit 9bd411c151
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
74 changed files with 400 additions and 438 deletions

View file

@ -7,22 +7,22 @@ import {
import {FeedAPI, FeedAPIResponse} from './types'
export class AuthorFeedAPI implements FeedAPI {
getAgent: () => BskyAgent
agent: BskyAgent
params: GetAuthorFeed.QueryParams
constructor({
getAgent,
agent,
feedParams,
}: {
getAgent: () => BskyAgent
agent: BskyAgent
feedParams: GetAuthorFeed.QueryParams
}) {
this.getAgent = getAgent
this.agent = agent
this.params = feedParams
}
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
const res = await this.getAgent().getAuthorFeed({
const res = await this.agent.getAuthorFeed({
...this.params,
limit: 1,
})
@ -36,7 +36,7 @@ export class AuthorFeedAPI implements FeedAPI {
cursor: string | undefined
limit: number
}): Promise<FeedAPIResponse> {
const res = await this.getAgent().getAuthorFeed({
const res = await this.agent.getAuthorFeed({
...this.params,
cursor,
limit,

View file

@ -10,27 +10,27 @@ import {FeedAPI, FeedAPIResponse} from './types'
import {createBskyTopicsHeader, isBlueskyOwnedFeed} from './utils'
export class CustomFeedAPI implements FeedAPI {
getAgent: () => BskyAgent
agent: BskyAgent
params: GetCustomFeed.QueryParams
userInterests?: string
constructor({
getAgent,
agent,
feedParams,
userInterests,
}: {
getAgent: () => BskyAgent
agent: BskyAgent
feedParams: GetCustomFeed.QueryParams
userInterests?: string
}) {
this.getAgent = getAgent
this.agent = agent
this.params = feedParams
this.userInterests = userInterests
}
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
const contentLangs = getContentLanguages().join(',')
const res = await this.getAgent().app.bsky.feed.getFeed(
const res = await this.agent.app.bsky.feed.getFeed(
{
...this.params,
limit: 1,
@ -48,11 +48,11 @@ export class CustomFeedAPI implements FeedAPI {
limit: number
}): Promise<FeedAPIResponse> {
const contentLangs = getContentLanguages().join(',')
const agent = this.getAgent()
const agent = this.agent
const isBlueskyOwned = isBlueskyOwnedFeed(this.params.feed)
const res = agent.session
? await this.getAgent().app.bsky.feed.getFeed(
? await this.agent.app.bsky.feed.getFeed(
{
...this.params,
cursor,

View file

@ -3,14 +3,14 @@ import {AppBskyFeedDefs, BskyAgent} from '@atproto/api'
import {FeedAPI, FeedAPIResponse} from './types'
export class FollowingFeedAPI implements FeedAPI {
getAgent: () => BskyAgent
agent: BskyAgent
constructor({getAgent}: {getAgent: () => BskyAgent}) {
this.getAgent = getAgent
constructor({agent}: {agent: BskyAgent}) {
this.agent = agent
}
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
const res = await this.getAgent().getTimeline({
const res = await this.agent.getTimeline({
limit: 1,
})
return res.data.feed[0]
@ -23,7 +23,7 @@ export class FollowingFeedAPI implements FeedAPI {
cursor: string | undefined
limit: number
}): Promise<FeedAPIResponse> {
const res = await this.getAgent().getTimeline({
const res = await this.agent.getTimeline({
cursor,
limit,
})

View file

@ -27,7 +27,7 @@ export const FALLBACK_MARKER_POST: AppBskyFeedDefs.FeedViewPost = {
}
export class HomeFeedAPI implements FeedAPI {
getAgent: () => BskyAgent
agent: BskyAgent
following: FollowingFeedAPI
discover: CustomFeedAPI
usingDiscover = false
@ -36,24 +36,24 @@ export class HomeFeedAPI implements FeedAPI {
constructor({
userInterests,
getAgent,
agent,
}: {
userInterests?: string
getAgent: () => BskyAgent
agent: BskyAgent
}) {
this.getAgent = getAgent
this.following = new FollowingFeedAPI({getAgent})
this.agent = agent
this.following = new FollowingFeedAPI({agent})
this.discover = new CustomFeedAPI({
getAgent,
agent,
feedParams: {feed: PROD_DEFAULT_FEED('whats-hot')},
})
this.userInterests = userInterests
}
reset() {
this.following = new FollowingFeedAPI({getAgent: this.getAgent})
this.following = new FollowingFeedAPI({agent: this.agent})
this.discover = new CustomFeedAPI({
getAgent: this.getAgent,
agent: this.agent,
feedParams: {feed: PROD_DEFAULT_FEED('whats-hot')},
userInterests: this.userInterests,
})

View file

@ -7,22 +7,22 @@ import {
import {FeedAPI, FeedAPIResponse} from './types'
export class LikesFeedAPI implements FeedAPI {
getAgent: () => BskyAgent
agent: BskyAgent
params: GetActorLikes.QueryParams
constructor({
getAgent,
agent,
feedParams,
}: {
getAgent: () => BskyAgent
agent: BskyAgent
feedParams: GetActorLikes.QueryParams
}) {
this.getAgent = getAgent
this.agent = agent
this.params = feedParams
}
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
const res = await this.getAgent().getActorLikes({
const res = await this.agent.getActorLikes({
...this.params,
limit: 1,
})
@ -36,7 +36,7 @@ export class LikesFeedAPI implements FeedAPI {
cursor: string | undefined
limit: number
}): Promise<FeedAPIResponse> {
const res = await this.getAgent().getActorLikes({
const res = await this.agent.getActorLikes({
...this.params,
cursor,
limit,

View file

@ -7,22 +7,22 @@ import {
import {FeedAPI, FeedAPIResponse} from './types'
export class ListFeedAPI implements FeedAPI {
getAgent: () => BskyAgent
agent: BskyAgent
params: GetListFeed.QueryParams
constructor({
getAgent,
agent,
feedParams,
}: {
getAgent: () => BskyAgent
agent: BskyAgent
feedParams: GetListFeed.QueryParams
}) {
this.getAgent = getAgent
this.agent = agent
this.params = feedParams
}
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
const res = await this.getAgent().app.bsky.feed.getListFeed({
const res = await this.agent.app.bsky.feed.getListFeed({
...this.params,
limit: 1,
})
@ -36,7 +36,7 @@ export class ListFeedAPI implements FeedAPI {
cursor: string | undefined
limit: number
}): Promise<FeedAPIResponse> {
const res = await this.getAgent().app.bsky.feed.getListFeed({
const res = await this.agent.app.bsky.feed.getListFeed({
...this.params,
cursor,
limit,

View file

@ -16,7 +16,7 @@ const POST_AGE_CUTOFF = 60e3 * 60 * 24 // 24hours
export class MergeFeedAPI implements FeedAPI {
userInterests?: string
getAgent: () => BskyAgent
agent: BskyAgent
params: FeedParams
feedTuners: FeedTunerFn[]
following: MergeFeedSource_Following
@ -26,29 +26,29 @@ export class MergeFeedAPI implements FeedAPI {
sampleCursor = 0
constructor({
getAgent,
agent,
feedParams,
feedTuners,
userInterests,
}: {
getAgent: () => BskyAgent
agent: BskyAgent
feedParams: FeedParams
feedTuners: FeedTunerFn[]
userInterests?: string
}) {
this.getAgent = getAgent
this.agent = agent
this.params = feedParams
this.feedTuners = feedTuners
this.userInterests = userInterests
this.following = new MergeFeedSource_Following({
getAgent: this.getAgent,
agent: this.agent,
feedTuners: this.feedTuners,
})
}
reset() {
this.following = new MergeFeedSource_Following({
getAgent: this.getAgent,
agent: this.agent,
feedTuners: this.feedTuners,
})
this.customFeeds = []
@ -60,7 +60,7 @@ export class MergeFeedAPI implements FeedAPI {
this.params.mergeFeedSources.map(
feedUri =>
new MergeFeedSource_Custom({
getAgent: this.getAgent,
agent: this.agent,
feedUri,
feedTuners: this.feedTuners,
userInterests: this.userInterests,
@ -73,7 +73,7 @@ export class MergeFeedAPI implements FeedAPI {
}
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
const res = await this.getAgent().getTimeline({
const res = await this.agent.getTimeline({
limit: 1,
})
return res.data.feed[0]
@ -167,7 +167,7 @@ export class MergeFeedAPI implements FeedAPI {
}
class MergeFeedSource {
getAgent: () => BskyAgent
agent: BskyAgent
feedTuners: FeedTunerFn[]
sourceInfo: ReasonFeedSource | undefined
cursor: string | undefined = undefined
@ -175,13 +175,13 @@ class MergeFeedSource {
hasMore = true
constructor({
getAgent,
agent,
feedTuners,
}: {
getAgent: () => BskyAgent
agent: BskyAgent
feedTuners: FeedTunerFn[]
}) {
this.getAgent = getAgent
this.agent = agent
this.feedTuners = feedTuners
}
@ -245,7 +245,7 @@ class MergeFeedSource_Following extends MergeFeedSource {
cursor: string | undefined,
limit: number,
): Promise<AppBskyFeedGetTimeline.Response> {
const res = await this.getAgent().getTimeline({cursor, limit})
const res = await this.agent.getTimeline({cursor, limit})
// run the tuner pre-emptively to ensure better mixing
const slices = this.tuner.tune(res.data.feed, {
dryRun: false,
@ -257,27 +257,27 @@ class MergeFeedSource_Following extends MergeFeedSource {
}
class MergeFeedSource_Custom extends MergeFeedSource {
getAgent: () => BskyAgent
agent: BskyAgent
minDate: Date
feedUri: string
userInterests?: string
constructor({
getAgent,
agent,
feedUri,
feedTuners,
userInterests,
}: {
getAgent: () => BskyAgent
agent: BskyAgent
feedUri: string
feedTuners: FeedTunerFn[]
userInterests?: string
}) {
super({
getAgent,
agent,
feedTuners,
})
this.getAgent = getAgent
this.agent = agent
this.feedUri = feedUri
this.userInterests = userInterests
this.sourceInfo = {
@ -295,7 +295,7 @@ class MergeFeedSource_Custom extends MergeFeedSource {
try {
const contentLangs = getContentLanguages().join(',')
const isBlueskyOwned = isBlueskyOwnedFeed(this.feedUri)
const res = await this.getAgent().app.bsky.feed.getFeed(
const res = await this.agent.app.bsky.feed.getFeed(
{
cursor,
limit,

View file

@ -14,12 +14,12 @@ const SERVICE_DID = (serviceUrl?: string) =>
: 'did:web:api.bsky.app'
async function registerPushToken(
getAgent: () => BskyAgent,
agent: BskyAgent,
account: SessionAccount,
token: Notifications.DevicePushToken,
) {
try {
await getAgent().api.app.bsky.notification.registerPush({
await agent.api.app.bsky.notification.registerPush({
serviceDid: SERVICE_DID(account.service),
platform: devicePlatform,
token: token.data,
@ -47,7 +47,7 @@ async function getPushToken(skipPermissionCheck = false) {
}
export function useNotificationsRegistration() {
const {getAgent} = useAgent()
const agent = useAgent()
const {currentAccount} = useSession()
React.useEffect(() => {
@ -60,13 +60,13 @@ export function useNotificationsRegistration() {
// According to the Expo docs, there is a chance that the token will change while the app is open in some rare
// cases. This will fire `registerPushToken` whenever that happens.
const subscription = Notifications.addPushTokenListener(async newToken => {
registerPushToken(getAgent, currentAccount, newToken)
registerPushToken(agent, currentAccount, newToken)
})
return () => {
subscription.remove()
}
}, [currentAccount, getAgent])
}, [currentAccount, agent])
}
export function useRequestNotificationsPermission() {