[SDK] Migrate the feed api classes to the appview client (#11361)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
+24
-21
@@ -1,23 +1,25 @@
|
|||||||
import {
|
import {AppBskyFeedDefs} from '@atproto/api'
|
||||||
AppBskyFeedDefs,
|
import {type Client, type XrpcRequestParams} from '@atproto/lex'
|
||||||
type AppBskyFeedGetAuthorFeed as GetAuthorFeed,
|
|
||||||
type AtpAgent,
|
|
||||||
} from '@atproto/api'
|
|
||||||
|
|
||||||
|
import {app} from '#/lexicons'
|
||||||
import {type FeedAPI, type FeedAPIResponse} from './types'
|
import {type FeedAPI, type FeedAPIResponse} from './types'
|
||||||
|
|
||||||
|
type GetAuthorFeedParams = XrpcRequestParams<
|
||||||
|
typeof app.bsky.feed.getAuthorFeed.main
|
||||||
|
>
|
||||||
|
|
||||||
export class AuthorFeedAPI implements FeedAPI {
|
export class AuthorFeedAPI implements FeedAPI {
|
||||||
agent: AtpAgent
|
client: Client
|
||||||
_params: GetAuthorFeed.QueryParams
|
_params: GetAuthorFeedParams
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
agent,
|
client,
|
||||||
feedParams,
|
feedParams,
|
||||||
}: {
|
}: {
|
||||||
agent: AtpAgent
|
client: Client
|
||||||
feedParams: GetAuthorFeed.QueryParams
|
feedParams: GetAuthorFeedParams
|
||||||
}) {
|
}) {
|
||||||
this.agent = agent
|
this.client = client
|
||||||
this._params = feedParams
|
this._params = feedParams
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,11 +30,11 @@ export class AuthorFeedAPI implements FeedAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
||||||
const res = await this.agent.getAuthorFeed({
|
const data = await this.client.call(app.bsky.feed.getAuthorFeed, {
|
||||||
...this.params,
|
...this.params,
|
||||||
limit: 1,
|
limit: 1,
|
||||||
})
|
})
|
||||||
return res.data.feed[0]
|
return data.feed[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetch({
|
async fetch({
|
||||||
@@ -42,19 +44,20 @@ export class AuthorFeedAPI implements FeedAPI {
|
|||||||
cursor: string | undefined
|
cursor: string | undefined
|
||||||
limit: number
|
limit: number
|
||||||
}): Promise<FeedAPIResponse> {
|
}): Promise<FeedAPIResponse> {
|
||||||
const res = await this.agent.getAuthorFeed({
|
/*
|
||||||
|
* A failed request rejects rather than resolving, so the error propagates
|
||||||
|
* to the query and drives the feed error UI (blocked actor, rate limit).
|
||||||
|
* The agent behaved the same way - its `success` flag was only ever true -
|
||||||
|
* so the empty-page branch this replaces was unreachable.
|
||||||
|
*/
|
||||||
|
const data = await this.client.call(app.bsky.feed.getAuthorFeed, {
|
||||||
...this.params,
|
...this.params,
|
||||||
cursor,
|
cursor,
|
||||||
limit,
|
limit,
|
||||||
})
|
})
|
||||||
if (res.success) {
|
|
||||||
return {
|
|
||||||
cursor: res.data.cursor,
|
|
||||||
feed: this._filter(res.data.feed),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
feed: [],
|
cursor: data.cursor,
|
||||||
|
feed: this._filter(data.feed),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+45
-43
@@ -1,46 +1,46 @@
|
|||||||
import {
|
import {type AppBskyFeedDefs, AtpAgent, jsonStringToLex} from '@atproto/api'
|
||||||
type AppBskyFeedDefs,
|
import {type Client, type XrpcRequestParams} from '@atproto/lex'
|
||||||
type AppBskyFeedGetFeed as GetCustomFeed,
|
|
||||||
AtpAgent,
|
|
||||||
jsonStringToLex,
|
|
||||||
} from '@atproto/api'
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
getAppLanguageAsContentLanguage,
|
getAppLanguageAsContentLanguage,
|
||||||
getContentLanguages,
|
getContentLanguages,
|
||||||
} from '#/state/preferences/languages'
|
} from '#/state/preferences/languages'
|
||||||
|
import {app} from '#/lexicons'
|
||||||
import {type FeedAPI, type FeedAPIResponse} from './types'
|
import {type FeedAPI, type FeedAPIResponse} from './types'
|
||||||
import {createBskyTopicsHeader, isBlueskyOwnedFeed} from './utils'
|
import {createBskyTopicsHeader, isBlueskyOwnedFeed} from './utils'
|
||||||
|
|
||||||
|
type GetCustomFeedParams = XrpcRequestParams<typeof app.bsky.feed.getFeed.main>
|
||||||
|
|
||||||
export class CustomFeedAPI implements FeedAPI {
|
export class CustomFeedAPI implements FeedAPI {
|
||||||
agent: AtpAgent
|
client: Client
|
||||||
params: GetCustomFeed.QueryParams
|
params: GetCustomFeedParams
|
||||||
userInterests?: string
|
userInterests?: string
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
agent,
|
client,
|
||||||
feedParams,
|
feedParams,
|
||||||
userInterests,
|
userInterests,
|
||||||
}: {
|
}: {
|
||||||
agent: AtpAgent
|
client: Client
|
||||||
feedParams: GetCustomFeed.QueryParams
|
feedParams: GetCustomFeedParams
|
||||||
userInterests?: string
|
userInterests?: string
|
||||||
}) {
|
}) {
|
||||||
this.agent = agent
|
this.client = client
|
||||||
this.params = feedParams
|
this.params = feedParams
|
||||||
this.userInterests = userInterests
|
this.userInterests = userInterests
|
||||||
}
|
}
|
||||||
|
|
||||||
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
||||||
const contentLangs = getContentLanguages().join(',')
|
const contentLangs = getContentLanguages().join(',')
|
||||||
const res = await this.agent.app.bsky.feed.getFeed(
|
const data = await this.client.call(
|
||||||
|
app.bsky.feed.getFeed,
|
||||||
{
|
{
|
||||||
...this.params,
|
...this.params,
|
||||||
limit: 1,
|
limit: 1,
|
||||||
},
|
},
|
||||||
{headers: {'Accept-Language': contentLangs}},
|
{headers: {'Accept-Language': contentLangs}},
|
||||||
)
|
)
|
||||||
return res.data.feed[0]
|
return data.feed[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetch({
|
async fetch({
|
||||||
@@ -51,11 +51,17 @@ export class CustomFeedAPI implements FeedAPI {
|
|||||||
limit: number
|
limit: number
|
||||||
}): Promise<FeedAPIResponse> {
|
}): Promise<FeedAPIResponse> {
|
||||||
const contentLangs = getContentLanguages().join(',')
|
const contentLangs = getContentLanguages().join(',')
|
||||||
const agent = this.agent
|
|
||||||
const isBlueskyOwned = isBlueskyOwnedFeed(this.params.feed)
|
const isBlueskyOwned = isBlueskyOwnedFeed(this.params.feed)
|
||||||
|
|
||||||
const res = agent.did
|
/*
|
||||||
? await this.agent.app.bsky.feed.getFeed(
|
* The authed branch rejects on failure, so the error propagates to the
|
||||||
|
* query and drives the feed error UI (feedgen offline, misconfigured, rate
|
||||||
|
* limited). Only the logged-out branch can resolve without data, and it
|
||||||
|
* signals that with a null body.
|
||||||
|
*/
|
||||||
|
const data = this.client.did
|
||||||
|
? await this.client.call(
|
||||||
|
app.bsky.feed.getFeed,
|
||||||
{
|
{
|
||||||
...this.params,
|
...this.params,
|
||||||
cursor,
|
cursor,
|
||||||
@@ -71,21 +77,22 @@ export class CustomFeedAPI implements FeedAPI {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
: await loggedOutFetch({...this.params, cursor, limit})
|
: await loggedOutFetch({...this.params, cursor, limit})
|
||||||
if (res.success) {
|
|
||||||
// NOTE
|
if (!data) {
|
||||||
// some custom feeds fail to enforce the pagination limit
|
|
||||||
// so we manually truncate here
|
|
||||||
// -prf
|
|
||||||
if (res.data.feed.length > limit) {
|
|
||||||
res.data.feed = res.data.feed.slice(0, limit)
|
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
cursor: res.data.feed.length ? res.data.cursor : undefined,
|
feed: [],
|
||||||
feed: res.data.feed,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE
|
||||||
|
// some custom feeds fail to enforce the pagination limit
|
||||||
|
// so we manually truncate here
|
||||||
|
// -prf
|
||||||
|
const feed =
|
||||||
|
data.feed.length > limit ? data.feed.slice(0, limit) : data.feed
|
||||||
return {
|
return {
|
||||||
feed: [],
|
cursor: feed.length ? data.cursor : undefined,
|
||||||
|
feed,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -105,7 +112,7 @@ async function loggedOutFetch({
|
|||||||
feed: string
|
feed: string
|
||||||
limit: number
|
limit: number
|
||||||
cursor?: string
|
cursor?: string
|
||||||
}) {
|
}): Promise<app.bsky.feed.getFeed.$OutputBody | null> {
|
||||||
let contentLangs = getAppLanguageAsContentLanguage()
|
let contentLangs = getAppLanguageAsContentLanguage()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -128,14 +135,15 @@ async function loggedOutFetch({
|
|||||||
headers: {'Accept-Language': contentLangs, ...labelersHeader},
|
headers: {'Accept-Language': contentLangs, ...labelersHeader},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
/*
|
||||||
|
* The response is hand-decoded rather than validated, so the lex output shape
|
||||||
|
* is asserted here just as the old-world one was.
|
||||||
|
*/
|
||||||
let data = res.ok
|
let data = res.ok
|
||||||
? (jsonStringToLex(await res.text()) as GetCustomFeed.OutputSchema)
|
? (jsonStringToLex(await res.text()) as app.bsky.feed.getFeed.$OutputBody)
|
||||||
: null
|
: null
|
||||||
if (data?.feed?.length) {
|
if (data?.feed?.length) {
|
||||||
return {
|
return data
|
||||||
success: true,
|
|
||||||
data,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// no data, try again with language headers removed
|
// no data, try again with language headers removed
|
||||||
@@ -146,17 +154,11 @@ async function loggedOutFetch({
|
|||||||
{method: 'GET', headers: {'Accept-Language': '', ...labelersHeader}},
|
{method: 'GET', headers: {'Accept-Language': '', ...labelersHeader}},
|
||||||
)
|
)
|
||||||
data = res.ok
|
data = res.ok
|
||||||
? (jsonStringToLex(await res.text()) as GetCustomFeed.OutputSchema)
|
? (jsonStringToLex(await res.text()) as app.bsky.feed.getFeed.$OutputBody)
|
||||||
: null
|
: null
|
||||||
if (data?.feed?.length) {
|
if (data?.feed?.length) {
|
||||||
return {
|
return data
|
||||||
success: true,
|
|
||||||
data,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return null
|
||||||
success: false,
|
|
||||||
data: {feed: []},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
import {type AppBskyFeedDefs, type AtpAgent} from '@atproto/api'
|
import {type AppBskyFeedDefs} from '@atproto/api'
|
||||||
|
import {type Client} from '@atproto/lex'
|
||||||
|
|
||||||
import {DEMO_FEED} from '#/lib/demo'
|
import {DEMO_FEED} from '#/lib/demo'
|
||||||
import {type FeedAPI, type FeedAPIResponse} from './types'
|
import {type FeedAPI, type FeedAPIResponse} from './types'
|
||||||
|
|
||||||
export class DemoFeedAPI implements FeedAPI {
|
export class DemoFeedAPI implements FeedAPI {
|
||||||
agent: AtpAgent
|
client: Client
|
||||||
|
|
||||||
constructor({agent}: {agent: AtpAgent}) {
|
constructor({client}: {client: Client}) {
|
||||||
this.agent = agent
|
this.client = client
|
||||||
}
|
}
|
||||||
|
|
||||||
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
||||||
|
|||||||
@@ -1,19 +1,21 @@
|
|||||||
import {type AppBskyFeedDefs, type AtpAgent} from '@atproto/api'
|
import {type AppBskyFeedDefs} from '@atproto/api'
|
||||||
|
import {type Client} from '@atproto/lex'
|
||||||
|
|
||||||
|
import {app} from '#/lexicons'
|
||||||
import {type FeedAPI, type FeedAPIResponse} from './types'
|
import {type FeedAPI, type FeedAPIResponse} from './types'
|
||||||
|
|
||||||
export class FollowingFeedAPI implements FeedAPI {
|
export class FollowingFeedAPI implements FeedAPI {
|
||||||
agent: AtpAgent
|
client: Client
|
||||||
|
|
||||||
constructor({agent}: {agent: AtpAgent}) {
|
constructor({client}: {client: Client}) {
|
||||||
this.agent = agent
|
this.client = client
|
||||||
}
|
}
|
||||||
|
|
||||||
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
||||||
const res = await this.agent.getTimeline({
|
const data = await this.client.call(app.bsky.feed.getTimeline, {
|
||||||
limit: 1,
|
limit: 1,
|
||||||
})
|
})
|
||||||
return res.data.feed[0]
|
return data.feed[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetch({
|
async fetch({
|
||||||
@@ -23,18 +25,19 @@ export class FollowingFeedAPI implements FeedAPI {
|
|||||||
cursor: string | undefined
|
cursor: string | undefined
|
||||||
limit: number
|
limit: number
|
||||||
}): Promise<FeedAPIResponse> {
|
}): Promise<FeedAPIResponse> {
|
||||||
const res = await this.agent.getTimeline({
|
/*
|
||||||
|
* A failed request rejects rather than resolving, so the error propagates
|
||||||
|
* to the query and drives the feed error UI. The agent behaved the same
|
||||||
|
* way - its `success` flag was only ever true - so the empty-page branch
|
||||||
|
* this replaces was unreachable.
|
||||||
|
*/
|
||||||
|
const data = await this.client.call(app.bsky.feed.getTimeline, {
|
||||||
cursor,
|
cursor,
|
||||||
limit,
|
limit,
|
||||||
})
|
})
|
||||||
if (res.success) {
|
|
||||||
return {
|
|
||||||
cursor: res.data.cursor,
|
|
||||||
feed: res.data.feed,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
feed: [],
|
cursor: data.cursor,
|
||||||
|
feed: data.feed,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-11
@@ -1,4 +1,6 @@
|
|||||||
import {type AppBskyFeedDefs, type AtpAgent} from '@atproto/api'
|
import {type AppBskyFeedDefs} from '@atproto/api'
|
||||||
|
import {type Client} from '@atproto/lex'
|
||||||
|
import {type AtUriString} from '@atproto/syntax'
|
||||||
|
|
||||||
import {PROD_DEFAULT_FEED} from '#/lib/constants'
|
import {PROD_DEFAULT_FEED} from '#/lib/constants'
|
||||||
import {CustomFeedAPI} from './custom'
|
import {CustomFeedAPI} from './custom'
|
||||||
@@ -27,7 +29,7 @@ export const FALLBACK_MARKER_POST: AppBskyFeedDefs.FeedViewPost = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class HomeFeedAPI implements FeedAPI {
|
export class HomeFeedAPI implements FeedAPI {
|
||||||
agent: AtpAgent
|
client: Client
|
||||||
following: FollowingFeedAPI
|
following: FollowingFeedAPI
|
||||||
discover: CustomFeedAPI
|
discover: CustomFeedAPI
|
||||||
usingDiscover = false
|
usingDiscover = false
|
||||||
@@ -36,25 +38,25 @@ export class HomeFeedAPI implements FeedAPI {
|
|||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
userInterests,
|
userInterests,
|
||||||
agent,
|
client,
|
||||||
}: {
|
}: {
|
||||||
userInterests?: string
|
userInterests?: string
|
||||||
agent: AtpAgent
|
client: Client
|
||||||
}) {
|
}) {
|
||||||
this.agent = agent
|
this.client = client
|
||||||
this.following = new FollowingFeedAPI({agent})
|
this.following = new FollowingFeedAPI({client})
|
||||||
this.discover = new CustomFeedAPI({
|
this.discover = new CustomFeedAPI({
|
||||||
agent,
|
client,
|
||||||
feedParams: {feed: PROD_DEFAULT_FEED('whats-hot')},
|
feedParams: {feed: PROD_DEFAULT_FEED('whats-hot') as AtUriString},
|
||||||
})
|
})
|
||||||
this.userInterests = userInterests
|
this.userInterests = userInterests
|
||||||
}
|
}
|
||||||
|
|
||||||
reset() {
|
reset() {
|
||||||
this.following = new FollowingFeedAPI({agent: this.agent})
|
this.following = new FollowingFeedAPI({client: this.client})
|
||||||
this.discover = new CustomFeedAPI({
|
this.discover = new CustomFeedAPI({
|
||||||
agent: this.agent,
|
client: this.client,
|
||||||
feedParams: {feed: PROD_DEFAULT_FEED('whats-hot')},
|
feedParams: {feed: PROD_DEFAULT_FEED('whats-hot') as AtUriString},
|
||||||
userInterests: this.userInterests,
|
userInterests: this.userInterests,
|
||||||
})
|
})
|
||||||
this.usingDiscover = false
|
this.usingDiscover = false
|
||||||
|
|||||||
+26
-23
@@ -1,32 +1,34 @@
|
|||||||
import {
|
import {type AppBskyFeedDefs} from '@atproto/api'
|
||||||
type AppBskyFeedDefs,
|
import {type Client, type XrpcRequestParams} from '@atproto/lex'
|
||||||
type AppBskyFeedGetActorLikes as GetActorLikes,
|
|
||||||
type AtpAgent,
|
|
||||||
} from '@atproto/api'
|
|
||||||
|
|
||||||
|
import {app} from '#/lexicons'
|
||||||
import {type FeedAPI, type FeedAPIResponse} from './types'
|
import {type FeedAPI, type FeedAPIResponse} from './types'
|
||||||
|
|
||||||
|
type GetActorLikesParams = XrpcRequestParams<
|
||||||
|
typeof app.bsky.feed.getActorLikes.main
|
||||||
|
>
|
||||||
|
|
||||||
export class LikesFeedAPI implements FeedAPI {
|
export class LikesFeedAPI implements FeedAPI {
|
||||||
agent: AtpAgent
|
client: Client
|
||||||
params: GetActorLikes.QueryParams
|
params: GetActorLikesParams
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
agent,
|
client,
|
||||||
feedParams,
|
feedParams,
|
||||||
}: {
|
}: {
|
||||||
agent: AtpAgent
|
client: Client
|
||||||
feedParams: GetActorLikes.QueryParams
|
feedParams: GetActorLikesParams
|
||||||
}) {
|
}) {
|
||||||
this.agent = agent
|
this.client = client
|
||||||
this.params = feedParams
|
this.params = feedParams
|
||||||
}
|
}
|
||||||
|
|
||||||
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
||||||
const res = await this.agent.getActorLikes({
|
const data = await this.client.call(app.bsky.feed.getActorLikes, {
|
||||||
...this.params,
|
...this.params,
|
||||||
limit: 1,
|
limit: 1,
|
||||||
})
|
})
|
||||||
return res.data.feed[0]
|
return data.feed[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetch({
|
async fetch({
|
||||||
@@ -36,21 +38,22 @@ export class LikesFeedAPI implements FeedAPI {
|
|||||||
cursor: string | undefined
|
cursor: string | undefined
|
||||||
limit: number
|
limit: number
|
||||||
}): Promise<FeedAPIResponse> {
|
}): Promise<FeedAPIResponse> {
|
||||||
const res = await this.agent.getActorLikes({
|
/*
|
||||||
|
* A failed request rejects rather than resolving, so the error propagates
|
||||||
|
* to the query and drives the feed error UI. The agent behaved the same
|
||||||
|
* way - its `success` flag was only ever true - so the empty-page branch
|
||||||
|
* this replaces was unreachable.
|
||||||
|
*/
|
||||||
|
const data = await this.client.call(app.bsky.feed.getActorLikes, {
|
||||||
...this.params,
|
...this.params,
|
||||||
cursor,
|
cursor,
|
||||||
limit,
|
limit,
|
||||||
})
|
})
|
||||||
if (res.success) {
|
// HACKFIX: the API incorrectly returns a cursor when there are no items -sfn
|
||||||
// HACKFIX: the API incorrectly returns a cursor when there are no items -sfn
|
const isEmptyPage = data.feed.length === 0
|
||||||
const isEmptyPage = res.data.feed.length === 0
|
|
||||||
return {
|
|
||||||
cursor: isEmptyPage ? undefined : res.data.cursor,
|
|
||||||
feed: res.data.feed,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
feed: [],
|
cursor: isEmptyPage ? undefined : data.cursor,
|
||||||
|
feed: data.feed,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-21
@@ -1,32 +1,34 @@
|
|||||||
import {
|
import {type AppBskyFeedDefs} from '@atproto/api'
|
||||||
type Agent,
|
import {type Client, type XrpcRequestParams} from '@atproto/lex'
|
||||||
type AppBskyFeedDefs,
|
|
||||||
type AppBskyFeedGetListFeed as GetListFeed,
|
|
||||||
} from '@atproto/api'
|
|
||||||
|
|
||||||
|
import {app} from '#/lexicons'
|
||||||
import {type FeedAPI, type FeedAPIResponse} from './types'
|
import {type FeedAPI, type FeedAPIResponse} from './types'
|
||||||
|
|
||||||
|
type GetListFeedParams = XrpcRequestParams<
|
||||||
|
typeof app.bsky.feed.getListFeed.main
|
||||||
|
>
|
||||||
|
|
||||||
export class ListFeedAPI implements FeedAPI {
|
export class ListFeedAPI implements FeedAPI {
|
||||||
agent: Agent
|
client: Client
|
||||||
params: GetListFeed.QueryParams
|
params: GetListFeedParams
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
agent,
|
client,
|
||||||
feedParams,
|
feedParams,
|
||||||
}: {
|
}: {
|
||||||
agent: Agent
|
client: Client
|
||||||
feedParams: GetListFeed.QueryParams
|
feedParams: GetListFeedParams
|
||||||
}) {
|
}) {
|
||||||
this.agent = agent
|
this.client = client
|
||||||
this.params = feedParams
|
this.params = feedParams
|
||||||
}
|
}
|
||||||
|
|
||||||
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
||||||
const res = await this.agent.app.bsky.feed.getListFeed({
|
const data = await this.client.call(app.bsky.feed.getListFeed, {
|
||||||
...this.params,
|
...this.params,
|
||||||
limit: 1,
|
limit: 1,
|
||||||
})
|
})
|
||||||
return res.data.feed[0]
|
return data.feed[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetch({
|
async fetch({
|
||||||
@@ -36,19 +38,20 @@ export class ListFeedAPI implements FeedAPI {
|
|||||||
cursor: string | undefined
|
cursor: string | undefined
|
||||||
limit: number
|
limit: number
|
||||||
}): Promise<FeedAPIResponse> {
|
}): Promise<FeedAPIResponse> {
|
||||||
const res = await this.agent.app.bsky.feed.getListFeed({
|
/*
|
||||||
|
* A failed request rejects rather than resolving, so the error propagates
|
||||||
|
* to the query and drives the feed error UI. The agent behaved the same
|
||||||
|
* way - its `success` flag was only ever true - so the empty-page branch
|
||||||
|
* this replaces was unreachable.
|
||||||
|
*/
|
||||||
|
const data = await this.client.call(app.bsky.feed.getListFeed, {
|
||||||
...this.params,
|
...this.params,
|
||||||
cursor,
|
cursor,
|
||||||
limit,
|
limit,
|
||||||
})
|
})
|
||||||
if (res.success) {
|
|
||||||
return {
|
|
||||||
cursor: res.data.cursor,
|
|
||||||
feed: res.data.feed,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
feed: [],
|
cursor: data.cursor,
|
||||||
|
feed: data.feed,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+65
-46
@@ -1,8 +1,6 @@
|
|||||||
import {
|
import {type AppBskyFeedDefs} from '@atproto/api'
|
||||||
type AppBskyFeedDefs,
|
import {type Client} from '@atproto/lex'
|
||||||
type AppBskyFeedGetTimeline,
|
import {type AtUriString} from '@atproto/syntax'
|
||||||
type AtpAgent,
|
|
||||||
} from '@atproto/api'
|
|
||||||
import shuffle from 'lodash.shuffle'
|
import shuffle from 'lodash.shuffle'
|
||||||
|
|
||||||
import {bundleAsync} from '#/lib/async/bundle'
|
import {bundleAsync} from '#/lib/async/bundle'
|
||||||
@@ -10,6 +8,7 @@ import {timeout} from '#/lib/async/timeout'
|
|||||||
import {feedUriToHref} from '#/lib/strings/url-helpers'
|
import {feedUriToHref} from '#/lib/strings/url-helpers'
|
||||||
import {getContentLanguages} from '#/state/preferences/languages'
|
import {getContentLanguages} from '#/state/preferences/languages'
|
||||||
import {type FeedParams} from '#/state/queries/post-feed'
|
import {type FeedParams} from '#/state/queries/post-feed'
|
||||||
|
import {app} from '#/lexicons'
|
||||||
import {FeedTuner} from '../feed-manip'
|
import {FeedTuner} from '../feed-manip'
|
||||||
import {type FeedTunerFn} from '../feed-manip'
|
import {type FeedTunerFn} from '../feed-manip'
|
||||||
import {
|
import {
|
||||||
@@ -22,9 +21,19 @@ import {createBskyTopicsHeader, isBlueskyOwnedFeed} from './utils'
|
|||||||
const REQUEST_WAIT_MS = 500 // 500ms
|
const REQUEST_WAIT_MS = 500 // 500ms
|
||||||
const POST_AGE_CUTOFF = 60e3 * 60 * 24 // 24hours
|
const POST_AGE_CUTOFF = 60e3 * 60 * 24 // 24hours
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A page of feed items, or `null` when the source could not produce one. Only
|
||||||
|
* sources that deliberately swallow their own errors return `null`; the rest
|
||||||
|
* reject so the error reaches the caller.
|
||||||
|
*/
|
||||||
|
type MergeFeedPage = {
|
||||||
|
cursor?: string
|
||||||
|
feed: AppBskyFeedDefs.FeedViewPost[]
|
||||||
|
} | null
|
||||||
|
|
||||||
export class MergeFeedAPI implements FeedAPI {
|
export class MergeFeedAPI implements FeedAPI {
|
||||||
userInterests?: string
|
userInterests?: string
|
||||||
agent: AtpAgent
|
client: Client
|
||||||
params: FeedParams
|
params: FeedParams
|
||||||
feedTuners: FeedTunerFn[]
|
feedTuners: FeedTunerFn[]
|
||||||
following: MergeFeedSource_Following
|
following: MergeFeedSource_Following
|
||||||
@@ -34,29 +43,29 @@ export class MergeFeedAPI implements FeedAPI {
|
|||||||
sampleCursor = 0
|
sampleCursor = 0
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
agent,
|
client,
|
||||||
feedParams,
|
feedParams,
|
||||||
feedTuners,
|
feedTuners,
|
||||||
userInterests,
|
userInterests,
|
||||||
}: {
|
}: {
|
||||||
agent: AtpAgent
|
client: Client
|
||||||
feedParams: FeedParams
|
feedParams: FeedParams
|
||||||
feedTuners: FeedTunerFn[]
|
feedTuners: FeedTunerFn[]
|
||||||
userInterests?: string
|
userInterests?: string
|
||||||
}) {
|
}) {
|
||||||
this.agent = agent
|
this.client = client
|
||||||
this.params = feedParams
|
this.params = feedParams
|
||||||
this.feedTuners = feedTuners
|
this.feedTuners = feedTuners
|
||||||
this.userInterests = userInterests
|
this.userInterests = userInterests
|
||||||
this.following = new MergeFeedSource_Following({
|
this.following = new MergeFeedSource_Following({
|
||||||
agent: this.agent,
|
client: this.client,
|
||||||
feedTuners: this.feedTuners,
|
feedTuners: this.feedTuners,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
reset() {
|
reset() {
|
||||||
this.following = new MergeFeedSource_Following({
|
this.following = new MergeFeedSource_Following({
|
||||||
agent: this.agent,
|
client: this.client,
|
||||||
feedTuners: this.feedTuners,
|
feedTuners: this.feedTuners,
|
||||||
})
|
})
|
||||||
this.customFeeds = []
|
this.customFeeds = []
|
||||||
@@ -68,7 +77,7 @@ export class MergeFeedAPI implements FeedAPI {
|
|||||||
this.params.mergeFeedSources.map(
|
this.params.mergeFeedSources.map(
|
||||||
feedUri =>
|
feedUri =>
|
||||||
new MergeFeedSource_Custom({
|
new MergeFeedSource_Custom({
|
||||||
agent: this.agent,
|
client: this.client,
|
||||||
feedUri,
|
feedUri,
|
||||||
feedTuners: this.feedTuners,
|
feedTuners: this.feedTuners,
|
||||||
userInterests: this.userInterests,
|
userInterests: this.userInterests,
|
||||||
@@ -81,10 +90,10 @@ export class MergeFeedAPI implements FeedAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
||||||
const res = await this.agent.getTimeline({
|
const data = await this.client.call(app.bsky.feed.getTimeline, {
|
||||||
limit: 1,
|
limit: 1,
|
||||||
})
|
})
|
||||||
return res.data.feed[0]
|
return data.feed[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetch({
|
async fetch({
|
||||||
@@ -175,7 +184,7 @@ export class MergeFeedAPI implements FeedAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class MergeFeedSource {
|
class MergeFeedSource {
|
||||||
agent: AtpAgent
|
client: Client
|
||||||
feedTuners: FeedTunerFn[]
|
feedTuners: FeedTunerFn[]
|
||||||
sourceInfo: ReasonFeedSource | undefined
|
sourceInfo: ReasonFeedSource | undefined
|
||||||
cursor: string | undefined = undefined
|
cursor: string | undefined = undefined
|
||||||
@@ -183,13 +192,13 @@ class MergeFeedSource {
|
|||||||
hasMore = true
|
hasMore = true
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
agent,
|
client,
|
||||||
feedTuners,
|
feedTuners,
|
||||||
}: {
|
}: {
|
||||||
agent: AtpAgent
|
client: Client
|
||||||
feedTuners: FeedTunerFn[]
|
feedTuners: FeedTunerFn[]
|
||||||
}) {
|
}) {
|
||||||
this.agent = agent
|
this.client = client
|
||||||
this.feedTuners = feedTuners
|
this.feedTuners = feedTuners
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,11 +219,11 @@ class MergeFeedSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_fetchNextInner = bundleAsync(async (n: number) => {
|
_fetchNextInner = bundleAsync(async (n: number) => {
|
||||||
const res = await this._getFeed(this.cursor, n)
|
const page = await this._getFeed(this.cursor, n)
|
||||||
if (res.success) {
|
if (page) {
|
||||||
this.cursor = res.data.cursor
|
this.cursor = page.cursor
|
||||||
if (res.data.feed.length) {
|
if (page.feed.length) {
|
||||||
this.queue = this.queue.concat(res.data.feed)
|
this.queue = this.queue.concat(page.feed)
|
||||||
} else {
|
} else {
|
||||||
this.hasMore = false
|
this.hasMore = false
|
||||||
}
|
}
|
||||||
@@ -226,7 +235,7 @@ class MergeFeedSource {
|
|||||||
protected _getFeed(
|
protected _getFeed(
|
||||||
_cursor: string | undefined,
|
_cursor: string | undefined,
|
||||||
_limit: number,
|
_limit: number,
|
||||||
): Promise<AppBskyFeedGetTimeline.Response> {
|
): Promise<MergeFeedPage> {
|
||||||
throw new Error('Must be overridden')
|
throw new Error('Must be overridden')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -238,42 +247,49 @@ class MergeFeedSource_Following extends MergeFeedSource {
|
|||||||
return this._fetchNextInner(n)
|
return this._fetchNextInner(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* No error handling: a failed timeline read rejects, which is what the agent
|
||||||
|
* did too, so the error still reaches `MergeFeedAPI.fetch` and the query.
|
||||||
|
*/
|
||||||
protected async _getFeed(
|
protected async _getFeed(
|
||||||
cursor: string | undefined,
|
cursor: string | undefined,
|
||||||
limit: number,
|
limit: number,
|
||||||
): Promise<AppBskyFeedGetTimeline.Response> {
|
): Promise<MergeFeedPage> {
|
||||||
const res = await this.agent.getTimeline({cursor, limit})
|
const data = await this.client.call(app.bsky.feed.getTimeline, {
|
||||||
|
cursor,
|
||||||
|
limit,
|
||||||
|
})
|
||||||
// run the tuner pre-emptively to ensure better mixing
|
// run the tuner pre-emptively to ensure better mixing
|
||||||
const slices = this.tuner.tune(res.data.feed, {
|
const slices = this.tuner.tune(data.feed, {
|
||||||
dryRun: false,
|
dryRun: false,
|
||||||
})
|
})
|
||||||
res.data.feed = slices.map(slice => slice._feedPost)
|
return {
|
||||||
return res
|
cursor: data.cursor,
|
||||||
|
feed: slices.map(slice => slice._feedPost),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MergeFeedSource_Custom extends MergeFeedSource {
|
class MergeFeedSource_Custom extends MergeFeedSource {
|
||||||
agent: AtpAgent
|
|
||||||
minDate: Date
|
minDate: Date
|
||||||
feedUri: string
|
feedUri: string
|
||||||
userInterests?: string
|
userInterests?: string
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
agent,
|
client,
|
||||||
feedUri,
|
feedUri,
|
||||||
feedTuners,
|
feedTuners,
|
||||||
userInterests,
|
userInterests,
|
||||||
}: {
|
}: {
|
||||||
agent: AtpAgent
|
client: Client
|
||||||
feedUri: string
|
feedUri: string
|
||||||
feedTuners: FeedTunerFn[]
|
feedTuners: FeedTunerFn[]
|
||||||
userInterests?: string
|
userInterests?: string
|
||||||
}) {
|
}) {
|
||||||
super({
|
super({
|
||||||
agent,
|
client,
|
||||||
feedTuners,
|
feedTuners,
|
||||||
})
|
})
|
||||||
this.agent = agent
|
|
||||||
this.feedUri = feedUri
|
this.feedUri = feedUri
|
||||||
this.userInterests = userInterests
|
this.userInterests = userInterests
|
||||||
this.sourceInfo = {
|
this.sourceInfo = {
|
||||||
@@ -287,15 +303,16 @@ class MergeFeedSource_Custom extends MergeFeedSource {
|
|||||||
protected async _getFeed(
|
protected async _getFeed(
|
||||||
cursor: string | undefined,
|
cursor: string | undefined,
|
||||||
limit: number,
|
limit: number,
|
||||||
): Promise<AppBskyFeedGetTimeline.Response> {
|
): Promise<MergeFeedPage> {
|
||||||
try {
|
try {
|
||||||
const contentLangs = getContentLanguages().join(',')
|
const contentLangs = getContentLanguages().join(',')
|
||||||
const isBlueskyOwned = isBlueskyOwnedFeed(this.feedUri)
|
const isBlueskyOwned = isBlueskyOwnedFeed(this.feedUri)
|
||||||
const res = await this.agent.app.bsky.feed.getFeed(
|
const data = await this.client.call(
|
||||||
|
app.bsky.feed.getFeed,
|
||||||
{
|
{
|
||||||
cursor,
|
cursor,
|
||||||
limit,
|
limit,
|
||||||
feed: this.feedUri,
|
feed: this.feedUri as AtUriString,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
@@ -310,22 +327,24 @@ class MergeFeedSource_Custom extends MergeFeedSource {
|
|||||||
// some custom feeds fail to enforce the pagination limit
|
// some custom feeds fail to enforce the pagination limit
|
||||||
// so we manually truncate here
|
// so we manually truncate here
|
||||||
// -prf
|
// -prf
|
||||||
if (limit && res.data.feed.length > limit) {
|
let feed: AppBskyFeedDefs.FeedViewPost[] =
|
||||||
res.data.feed = res.data.feed.slice(0, limit)
|
limit && data.feed.length > limit
|
||||||
}
|
? data.feed.slice(0, limit)
|
||||||
|
: data.feed
|
||||||
// filter out older posts
|
// filter out older posts
|
||||||
res.data.feed = res.data.feed.filter(
|
feed = feed.filter(post => new Date(post.post.indexedAt) > this.minDate)
|
||||||
post => new Date(post.post.indexedAt) > this.minDate,
|
|
||||||
)
|
|
||||||
// attach source info
|
// attach source info
|
||||||
for (const post of res.data.feed) {
|
for (const post of feed) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
post.__source = this.sourceInfo
|
post.__source = this.sourceInfo
|
||||||
}
|
}
|
||||||
return res
|
return {
|
||||||
|
cursor: data.cursor,
|
||||||
|
feed,
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// dont bubble custom-feed errors
|
// dont bubble custom-feed errors
|
||||||
return {success: false, headers: {}, data: {feed: []}}
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-19
@@ -1,25 +1,25 @@
|
|||||||
import {
|
import {type AppBskyFeedDefs} from '@atproto/api'
|
||||||
type Agent,
|
import {type Client, type XrpcRequestParams} from '@atproto/lex'
|
||||||
type AppBskyFeedDefs,
|
|
||||||
type AppBskyFeedGetPosts,
|
|
||||||
} from '@atproto/api'
|
|
||||||
|
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
|
import {app} from '#/lexicons'
|
||||||
import {type FeedAPI, type FeedAPIResponse} from './types'
|
import {type FeedAPI, type FeedAPIResponse} from './types'
|
||||||
|
|
||||||
|
type GetPostsParams = XrpcRequestParams<typeof app.bsky.feed.getPosts.main>
|
||||||
|
|
||||||
export class PostListFeedAPI implements FeedAPI {
|
export class PostListFeedAPI implements FeedAPI {
|
||||||
agent: Agent
|
client: Client
|
||||||
params: AppBskyFeedGetPosts.QueryParams
|
params: GetPostsParams
|
||||||
peek: AppBskyFeedDefs.FeedViewPost | null = null
|
peek: AppBskyFeedDefs.FeedViewPost | null = null
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
agent,
|
client,
|
||||||
feedParams,
|
feedParams,
|
||||||
}: {
|
}: {
|
||||||
agent: Agent
|
client: Client
|
||||||
feedParams: AppBskyFeedGetPosts.QueryParams
|
feedParams: GetPostsParams
|
||||||
}) {
|
}) {
|
||||||
this.agent = agent
|
this.client = client
|
||||||
if (feedParams.uris.length > 25) {
|
if (feedParams.uris.length > 25) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
`Too many URIs provided - expected 25, got ${feedParams.uris.length}`,
|
`Too many URIs provided - expected 25, got ${feedParams.uris.length}`,
|
||||||
@@ -36,17 +36,18 @@ export class PostListFeedAPI implements FeedAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fetch({}: {}): Promise<FeedAPIResponse> {
|
async fetch({}: {}): Promise<FeedAPIResponse> {
|
||||||
const res = await this.agent.app.bsky.feed.getPosts({
|
/*
|
||||||
|
* A failed request rejects rather than resolving, so the error propagates
|
||||||
|
* to the query and drives the feed error UI. The agent behaved the same
|
||||||
|
* way - its `success` flag was only ever true - so the empty-page branch
|
||||||
|
* this replaces was unreachable.
|
||||||
|
*/
|
||||||
|
const data = await this.client.call(app.bsky.feed.getPosts, {
|
||||||
...this.params,
|
...this.params,
|
||||||
})
|
})
|
||||||
if (res.success) {
|
this.peek = {post: data.posts[0]}
|
||||||
this.peek = {post: res.data.posts[0]}
|
|
||||||
return {
|
|
||||||
feed: res.data.posts.map(post => ({post})),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
feed: [],
|
feed: data.posts.map(post => ({post})),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import {
|
|||||||
AtUri,
|
AtUri,
|
||||||
moderatePost,
|
moderatePost,
|
||||||
} from '@atproto/api'
|
} from '@atproto/api'
|
||||||
|
import {type AtUriString} from '@atproto/syntax'
|
||||||
import {msg} from '@lingui/core/macro'
|
import {msg} from '@lingui/core/macro'
|
||||||
import {useLingui} from '@lingui/react'
|
import {useLingui} from '@lingui/react'
|
||||||
import {
|
import {
|
||||||
@@ -28,7 +29,7 @@ import {
|
|||||||
embedViewRecordToPostView,
|
embedViewRecordToPostView,
|
||||||
getEmbeddedPost,
|
getEmbeddedPost,
|
||||||
} from '#/state/queries/util'
|
} from '#/state/queries/util'
|
||||||
import {useAgent} from '#/state/session'
|
import {useAppviewClient} from '#/state/session'
|
||||||
|
|
||||||
const RQKEY_ROOT = 'feed-previews'
|
const RQKEY_ROOT = 'feed-previews'
|
||||||
const RQKEY = (feeds: string[]) => [RQKEY_ROOT, feeds]
|
const RQKEY = (feeds: string[]) => [RQKEY_ROOT, feeds]
|
||||||
@@ -121,7 +122,7 @@ export function useFeedPreviews(
|
|||||||
|
|
||||||
const uris = feeds.map(feed => feed.uri)
|
const uris = feeds.map(feed => feed.uri)
|
||||||
const {_} = useLingui()
|
const {_} = useLingui()
|
||||||
const agent = useAgent()
|
const client = useAppviewClient()
|
||||||
const {data: preferences} = usePreferencesQuery()
|
const {data: preferences} = usePreferencesQuery()
|
||||||
const userInterests = aggregateUserInterests(preferences)
|
const userInterests = aggregateUserInterests(preferences)
|
||||||
const moderationOpts = useModerationOpts()
|
const moderationOpts = useModerationOpts()
|
||||||
@@ -143,8 +144,8 @@ export function useFeedPreviews(
|
|||||||
queryFn: async ({pageParam}) => {
|
queryFn: async ({pageParam}) => {
|
||||||
const feed = feeds[pageParam]
|
const feed = feeds[pageParam]
|
||||||
const api = new CustomFeedAPI({
|
const api = new CustomFeedAPI({
|
||||||
agent,
|
client,
|
||||||
feedParams: {feed: feed.uri},
|
feedParams: {feed: feed.uri as AtUriString},
|
||||||
userInterests,
|
userInterests,
|
||||||
})
|
})
|
||||||
const data = await api.fetch({cursor: undefined, limit: LIMIT})
|
const data = await api.fetch({cursor: undefined, limit: LIMIT})
|
||||||
|
|||||||
@@ -4,12 +4,13 @@ import {
|
|||||||
type AppBskyActorDefs,
|
type AppBskyActorDefs,
|
||||||
AppBskyFeedDefs,
|
AppBskyFeedDefs,
|
||||||
type AppBskyFeedPost,
|
type AppBskyFeedPost,
|
||||||
type AtpAgent,
|
|
||||||
AtUri,
|
AtUri,
|
||||||
moderatePost,
|
moderatePost,
|
||||||
type ModerationDecision,
|
type ModerationDecision,
|
||||||
type ModerationPrefs,
|
type ModerationPrefs,
|
||||||
} from '@atproto/api'
|
} from '@atproto/api'
|
||||||
|
import {type Client} from '@atproto/lex'
|
||||||
|
import {type AtIdentifierString, type AtUriString} from '@atproto/syntax'
|
||||||
import {
|
import {
|
||||||
type InfiniteData,
|
type InfiniteData,
|
||||||
type QueryClient,
|
type QueryClient,
|
||||||
@@ -33,7 +34,7 @@ import {DISCOVER_FEED_URI} from '#/lib/constants'
|
|||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
import {STALE} from '#/state/queries'
|
import {STALE} from '#/state/queries'
|
||||||
import {DEFAULT_LOGGED_OUT_PREFERENCES} from '#/state/queries/preferences/const'
|
import {DEFAULT_LOGGED_OUT_PREFERENCES} from '#/state/queries/preferences/const'
|
||||||
import {useAgent} from '#/state/session'
|
import {useAgent, useAppviewClient} from '#/state/session'
|
||||||
import * as userActionHistory from '#/state/userActionHistory'
|
import * as userActionHistory from '#/state/userActionHistory'
|
||||||
import {KnownError} from '#/view/com/posts/PostFeedErrorMessage'
|
import {KnownError} from '#/view/com/posts/PostFeedErrorMessage'
|
||||||
import {useFeedTuners} from '../preferences/feed-tuners'
|
import {useFeedTuners} from '../preferences/feed-tuners'
|
||||||
@@ -149,6 +150,7 @@ export function usePostFeedQuery(
|
|||||||
) ?? -1
|
) ?? -1
|
||||||
const enableFollowingToDiscoverFallback = followingPinnedIndex === 0
|
const enableFollowingToDiscoverFallback = followingPinnedIndex === 0
|
||||||
const agent = useAgent()
|
const agent = useAgent()
|
||||||
|
const client = useAppviewClient()
|
||||||
const lastRun = useRef<{
|
const lastRun = useRef<{
|
||||||
data: InfiniteData<FeedPageUnselected>
|
data: InfiniteData<FeedPageUnselected>
|
||||||
args: typeof selectArgs
|
args: typeof selectArgs
|
||||||
@@ -193,7 +195,7 @@ export function usePostFeedQuery(
|
|||||||
feedDesc,
|
feedDesc,
|
||||||
feedParams: params || {},
|
feedParams: params || {},
|
||||||
feedTuners,
|
feedTuners,
|
||||||
agent,
|
client,
|
||||||
// Not in the query key because they don't change:
|
// Not in the query key because they don't change:
|
||||||
userInterests,
|
userInterests,
|
||||||
// Not in the query key. Reacting to it switching isn't important:
|
// Not in the query key. Reacting to it switching isn't important:
|
||||||
@@ -443,55 +445,68 @@ function createApi({
|
|||||||
feedParams,
|
feedParams,
|
||||||
feedTuners,
|
feedTuners,
|
||||||
userInterests,
|
userInterests,
|
||||||
agent,
|
client,
|
||||||
enableFollowingToDiscoverFallback,
|
enableFollowingToDiscoverFallback,
|
||||||
}: {
|
}: {
|
||||||
feedDesc: FeedDescriptor
|
feedDesc: FeedDescriptor
|
||||||
feedParams: FeedParams
|
feedParams: FeedParams
|
||||||
feedTuners: FeedTunerFn[]
|
feedTuners: FeedTunerFn[]
|
||||||
userInterests?: string
|
userInterests?: string
|
||||||
agent: AtpAgent
|
client: Client
|
||||||
enableFollowingToDiscoverFallback: boolean
|
enableFollowingToDiscoverFallback: boolean
|
||||||
}) {
|
}) {
|
||||||
if (feedDesc === 'following') {
|
if (feedDesc === 'following') {
|
||||||
if (feedParams.mergeFeedEnabled) {
|
if (feedParams.mergeFeedEnabled) {
|
||||||
return new MergeFeedAPI({
|
return new MergeFeedAPI({
|
||||||
agent,
|
client,
|
||||||
feedParams,
|
feedParams,
|
||||||
feedTuners,
|
feedTuners,
|
||||||
userInterests,
|
userInterests,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
if (enableFollowingToDiscoverFallback) {
|
if (enableFollowingToDiscoverFallback) {
|
||||||
return new HomeFeedAPI({agent, userInterests})
|
return new HomeFeedAPI({client, userInterests})
|
||||||
} else {
|
} else {
|
||||||
return new FollowingFeedAPI({agent})
|
return new FollowingFeedAPI({client})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (feedDesc.startsWith('author')) {
|
} else if (feedDesc.startsWith('author')) {
|
||||||
const [__, actor, filter] = feedDesc.split('|')
|
const [__, actor, filter] = feedDesc.split('|')
|
||||||
return new AuthorFeedAPI({agent, feedParams: {actor, filter}})
|
/*
|
||||||
|
* The descriptor is split out of an internally-built string, so neither the
|
||||||
|
* actor identifier nor the filter token is narrowed by the compiler here.
|
||||||
|
*/
|
||||||
|
return new AuthorFeedAPI({
|
||||||
|
client,
|
||||||
|
feedParams: {actor: actor as AtIdentifierString, filter},
|
||||||
|
})
|
||||||
} else if (feedDesc.startsWith('likes')) {
|
} else if (feedDesc.startsWith('likes')) {
|
||||||
const [__, actor] = feedDesc.split('|')
|
const [__, actor] = feedDesc.split('|')
|
||||||
return new LikesFeedAPI({agent, feedParams: {actor}})
|
return new LikesFeedAPI({
|
||||||
|
client,
|
||||||
|
feedParams: {actor: actor as AtIdentifierString},
|
||||||
|
})
|
||||||
} else if (feedDesc.startsWith('feedgen')) {
|
} else if (feedDesc.startsWith('feedgen')) {
|
||||||
const [__, feed] = feedDesc.split('|')
|
const [__, feed] = feedDesc.split('|')
|
||||||
return new CustomFeedAPI({
|
return new CustomFeedAPI({
|
||||||
agent,
|
client,
|
||||||
feedParams: {feed},
|
feedParams: {feed: feed as AtUriString},
|
||||||
userInterests,
|
userInterests,
|
||||||
})
|
})
|
||||||
} else if (feedDesc.startsWith('list')) {
|
} else if (feedDesc.startsWith('list')) {
|
||||||
const [__, list] = feedDesc.split('|')
|
const [__, list] = feedDesc.split('|')
|
||||||
return new ListFeedAPI({agent, feedParams: {list}})
|
return new ListFeedAPI({client, feedParams: {list: list as AtUriString}})
|
||||||
} else if (feedDesc.startsWith('posts')) {
|
} else if (feedDesc.startsWith('posts')) {
|
||||||
const [__, uriList] = feedDesc.split('|')
|
const [__, uriList] = feedDesc.split('|')
|
||||||
return new PostListFeedAPI({agent, feedParams: {uris: uriList.split(',')}})
|
return new PostListFeedAPI({
|
||||||
|
client,
|
||||||
|
feedParams: {uris: uriList.split(',') as AtUriString[]},
|
||||||
|
})
|
||||||
} else if (feedDesc === 'demo') {
|
} else if (feedDesc === 'demo') {
|
||||||
return new DemoFeedAPI({agent})
|
return new DemoFeedAPI({client})
|
||||||
} else {
|
} else {
|
||||||
// shouldnt happen
|
// shouldnt happen
|
||||||
return new FollowingFeedAPI({agent})
|
return new FollowingFeedAPI({client})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import {type AppBskyActorDefs, type AppBskyFeedGetLikes} from '@atproto/api'
|
import {type AppBskyActorDefs} from '@atproto/api'
|
||||||
|
import {type AtUriString} from '@atproto/syntax'
|
||||||
import {
|
import {
|
||||||
type InfiniteData,
|
type InfiniteData,
|
||||||
type QueryClient,
|
type QueryClient,
|
||||||
@@ -9,7 +10,8 @@ import {
|
|||||||
|
|
||||||
import {STALE} from '#/state/queries'
|
import {STALE} from '#/state/queries'
|
||||||
import {createQueryKey} from '#/state/queries/util'
|
import {createQueryKey} from '#/state/queries/util'
|
||||||
import {useAgent} from '#/state/session'
|
import {useAppviewClient} from '#/state/session'
|
||||||
|
import {app} from '#/lexicons'
|
||||||
|
|
||||||
const PAGE_SIZE = 30
|
const PAGE_SIZE = 30
|
||||||
type RQPageParam = string | undefined
|
type RQPageParam = string | undefined
|
||||||
@@ -19,22 +21,22 @@ const RQKEY_ROOT = 'liked-by'
|
|||||||
export const RQKEY = (resolvedUri: string) => [RQKEY_ROOT, resolvedUri]
|
export const RQKEY = (resolvedUri: string) => [RQKEY_ROOT, resolvedUri]
|
||||||
|
|
||||||
export function useLikedByQuery(resolvedUri: string | undefined) {
|
export function useLikedByQuery(resolvedUri: string | undefined) {
|
||||||
const agent = useAgent()
|
const client = useAppviewClient()
|
||||||
return useInfiniteQuery<
|
return useInfiniteQuery<
|
||||||
AppBskyFeedGetLikes.OutputSchema,
|
app.bsky.feed.getLikes.$OutputBody,
|
||||||
Error,
|
Error,
|
||||||
InfiniteData<AppBskyFeedGetLikes.OutputSchema>,
|
InfiniteData<app.bsky.feed.getLikes.$OutputBody>,
|
||||||
QueryKey,
|
QueryKey,
|
||||||
RQPageParam
|
RQPageParam
|
||||||
>({
|
>({
|
||||||
queryKey: RQKEY(resolvedUri || ''),
|
queryKey: RQKEY(resolvedUri || ''),
|
||||||
async queryFn({pageParam}: {pageParam: RQPageParam}) {
|
async queryFn({pageParam}: {pageParam: RQPageParam}) {
|
||||||
const res = await agent.getLikes({
|
return await client.call(app.bsky.feed.getLikes, {
|
||||||
uri: resolvedUri || '',
|
// the enabled flag prevents this from running until resolvedUri is set
|
||||||
|
uri: (resolvedUri || '') as AtUriString,
|
||||||
limit: PAGE_SIZE,
|
limit: PAGE_SIZE,
|
||||||
cursor: pageParam,
|
cursor: pageParam,
|
||||||
})
|
})
|
||||||
return res.data
|
|
||||||
},
|
},
|
||||||
initialPageParam: undefined,
|
initialPageParam: undefined,
|
||||||
getNextPageParam: lastPage => lastPage.cursor,
|
getNextPageParam: lastPage => lastPage.cursor,
|
||||||
@@ -59,12 +61,14 @@ export const createLikedBySampleQueryKey = (args: {uri: string}) =>
|
|||||||
* perturb the liked-by screen's pagination.
|
* perturb the liked-by screen's pagination.
|
||||||
*/
|
*/
|
||||||
export function useLikedBySampleQuery({uri}: {uri: string | undefined}) {
|
export function useLikedBySampleQuery({uri}: {uri: string | undefined}) {
|
||||||
const agent = useAgent()
|
const client = useAppviewClient()
|
||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: createLikedBySampleQueryKey({uri: uri ?? ''}),
|
queryKey: createLikedBySampleQueryKey({uri: uri ?? ''}),
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const res = await agent.getLikes({uri: uri ?? '', limit: SAMPLE_SIZE})
|
return await client.call(app.bsky.feed.getLikes, {
|
||||||
return res.data
|
uri: (uri ?? '') as AtUriString,
|
||||||
|
limit: SAMPLE_SIZE,
|
||||||
|
})
|
||||||
},
|
},
|
||||||
staleTime: STALE.MINUTES.FIVE,
|
staleTime: STALE.MINUTES.FIVE,
|
||||||
enabled: !!uri,
|
enabled: !!uri,
|
||||||
@@ -81,7 +85,7 @@ export function* findAllProfilesInQueryData(
|
|||||||
did: string,
|
did: string,
|
||||||
): Generator<AppBskyActorDefs.ProfileView, void> {
|
): Generator<AppBskyActorDefs.ProfileView, void> {
|
||||||
const queryDatas = queryClient.getQueriesData<
|
const queryDatas = queryClient.getQueriesData<
|
||||||
InfiniteData<AppBskyFeedGetLikes.OutputSchema>
|
InfiniteData<app.bsky.feed.getLikes.$OutputBody>
|
||||||
>({
|
>({
|
||||||
queryKey: [RQKEY_ROOT],
|
queryKey: [RQKEY_ROOT],
|
||||||
})
|
})
|
||||||
@@ -98,7 +102,7 @@ export function* findAllProfilesInQueryData(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const sampleQueryDatas =
|
const sampleQueryDatas =
|
||||||
queryClient.getQueriesData<AppBskyFeedGetLikes.OutputSchema>({
|
queryClient.getQueriesData<app.bsky.feed.getLikes.$OutputBody>({
|
||||||
queryKey: [likedBySampleQueryKeyRoot],
|
queryKey: [likedBySampleQueryKeyRoot],
|
||||||
})
|
})
|
||||||
for (const [_queryKey, queryData] of sampleQueryDatas) {
|
for (const [_queryKey, queryData] of sampleQueryDatas) {
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ import {
|
|||||||
type AppBskyActorDefs,
|
type AppBskyActorDefs,
|
||||||
AppBskyEmbedRecord,
|
AppBskyEmbedRecord,
|
||||||
type AppBskyFeedDefs,
|
type AppBskyFeedDefs,
|
||||||
type AppBskyFeedGetQuotes,
|
|
||||||
AtUri,
|
AtUri,
|
||||||
} from '@atproto/api'
|
} from '@atproto/api'
|
||||||
|
import {type AtUriString} from '@atproto/syntax'
|
||||||
import {
|
import {
|
||||||
type InfiniteData,
|
type InfiniteData,
|
||||||
type QueryClient,
|
type QueryClient,
|
||||||
@@ -12,7 +12,8 @@ import {
|
|||||||
useInfiniteQuery,
|
useInfiniteQuery,
|
||||||
} from '@tanstack/react-query'
|
} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {useAgent} from '#/state/session'
|
import {useAppviewClient} from '#/state/session'
|
||||||
|
import {app} from '#/lexicons'
|
||||||
import {
|
import {
|
||||||
didOrHandleUriMatches,
|
didOrHandleUriMatches,
|
||||||
embedViewRecordToPostView,
|
embedViewRecordToPostView,
|
||||||
@@ -26,22 +27,22 @@ const RQKEY_ROOT = 'post-quotes'
|
|||||||
export const RQKEY = (resolvedUri: string) => [RQKEY_ROOT, resolvedUri]
|
export const RQKEY = (resolvedUri: string) => [RQKEY_ROOT, resolvedUri]
|
||||||
|
|
||||||
export function usePostQuotesQuery(resolvedUri: string | undefined) {
|
export function usePostQuotesQuery(resolvedUri: string | undefined) {
|
||||||
const agent = useAgent()
|
const client = useAppviewClient()
|
||||||
return useInfiniteQuery<
|
return useInfiniteQuery<
|
||||||
AppBskyFeedGetQuotes.OutputSchema,
|
app.bsky.feed.getQuotes.$OutputBody,
|
||||||
Error,
|
Error,
|
||||||
InfiniteData<AppBskyFeedGetQuotes.OutputSchema>,
|
InfiniteData<app.bsky.feed.getQuotes.$OutputBody>,
|
||||||
QueryKey,
|
QueryKey,
|
||||||
RQPageParam
|
RQPageParam
|
||||||
>({
|
>({
|
||||||
queryKey: RQKEY(resolvedUri || ''),
|
queryKey: RQKEY(resolvedUri || ''),
|
||||||
async queryFn({pageParam}: {pageParam: RQPageParam}) {
|
async queryFn({pageParam}: {pageParam: RQPageParam}) {
|
||||||
const res = await agent.api.app.bsky.feed.getQuotes({
|
return await client.call(app.bsky.feed.getQuotes, {
|
||||||
uri: resolvedUri || '',
|
// the enabled flag prevents this from running until resolvedUri is set
|
||||||
|
uri: (resolvedUri || '') as AtUriString,
|
||||||
limit: PAGE_SIZE,
|
limit: PAGE_SIZE,
|
||||||
cursor: pageParam,
|
cursor: pageParam,
|
||||||
})
|
})
|
||||||
return res.data
|
|
||||||
},
|
},
|
||||||
initialPageParam: undefined,
|
initialPageParam: undefined,
|
||||||
getNextPageParam: lastPage => lastPage.cursor,
|
getNextPageParam: lastPage => lastPage.cursor,
|
||||||
@@ -72,7 +73,7 @@ export function* findAllProfilesInQueryData(
|
|||||||
did: string,
|
did: string,
|
||||||
): Generator<AppBskyActorDefs.ProfileViewBasic, void> {
|
): Generator<AppBskyActorDefs.ProfileViewBasic, void> {
|
||||||
const queryDatas = queryClient.getQueriesData<
|
const queryDatas = queryClient.getQueriesData<
|
||||||
InfiniteData<AppBskyFeedGetQuotes.OutputSchema>
|
InfiniteData<app.bsky.feed.getQuotes.$OutputBody>
|
||||||
>({
|
>({
|
||||||
queryKey: [RQKEY_ROOT],
|
queryKey: [RQKEY_ROOT],
|
||||||
})
|
})
|
||||||
@@ -99,7 +100,7 @@ export function* findAllPostsInQueryData(
|
|||||||
uri: string,
|
uri: string,
|
||||||
): Generator<AppBskyFeedDefs.PostView, undefined> {
|
): Generator<AppBskyFeedDefs.PostView, undefined> {
|
||||||
const queryDatas = queryClient.getQueriesData<
|
const queryDatas = queryClient.getQueriesData<
|
||||||
InfiniteData<AppBskyFeedGetQuotes.OutputSchema>
|
InfiniteData<app.bsky.feed.getQuotes.$OutputBody>
|
||||||
>({
|
>({
|
||||||
queryKey: [RQKEY_ROOT],
|
queryKey: [RQKEY_ROOT],
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import {
|
import {type AppBskyActorDefs} from '@atproto/api'
|
||||||
type AppBskyActorDefs,
|
import {type AtUriString} from '@atproto/syntax'
|
||||||
type AppBskyFeedGetRepostedBy,
|
|
||||||
} from '@atproto/api'
|
|
||||||
import {
|
import {
|
||||||
type InfiniteData,
|
type InfiniteData,
|
||||||
type QueryClient,
|
type QueryClient,
|
||||||
@@ -9,7 +7,8 @@ import {
|
|||||||
useInfiniteQuery,
|
useInfiniteQuery,
|
||||||
} from '@tanstack/react-query'
|
} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {useAgent} from '#/state/session'
|
import {useAppviewClient} from '#/state/session'
|
||||||
|
import {app} from '#/lexicons'
|
||||||
|
|
||||||
const PAGE_SIZE = 30
|
const PAGE_SIZE = 30
|
||||||
type RQPageParam = string | undefined
|
type RQPageParam = string | undefined
|
||||||
@@ -19,22 +18,22 @@ const RQKEY_ROOT = 'post-reposted-by'
|
|||||||
export const RQKEY = (resolvedUri: string) => [RQKEY_ROOT, resolvedUri]
|
export const RQKEY = (resolvedUri: string) => [RQKEY_ROOT, resolvedUri]
|
||||||
|
|
||||||
export function usePostRepostedByQuery(resolvedUri: string | undefined) {
|
export function usePostRepostedByQuery(resolvedUri: string | undefined) {
|
||||||
const agent = useAgent()
|
const client = useAppviewClient()
|
||||||
return useInfiniteQuery<
|
return useInfiniteQuery<
|
||||||
AppBskyFeedGetRepostedBy.OutputSchema,
|
app.bsky.feed.getRepostedBy.$OutputBody,
|
||||||
Error,
|
Error,
|
||||||
InfiniteData<AppBskyFeedGetRepostedBy.OutputSchema>,
|
InfiniteData<app.bsky.feed.getRepostedBy.$OutputBody>,
|
||||||
QueryKey,
|
QueryKey,
|
||||||
RQPageParam
|
RQPageParam
|
||||||
>({
|
>({
|
||||||
queryKey: RQKEY(resolvedUri || ''),
|
queryKey: RQKEY(resolvedUri || ''),
|
||||||
async queryFn({pageParam}: {pageParam: RQPageParam}) {
|
async queryFn({pageParam}: {pageParam: RQPageParam}) {
|
||||||
const res = await agent.getRepostedBy({
|
return await client.call(app.bsky.feed.getRepostedBy, {
|
||||||
uri: resolvedUri || '',
|
// the enabled flag prevents this from running until resolvedUri is set
|
||||||
|
uri: (resolvedUri || '') as AtUriString,
|
||||||
limit: PAGE_SIZE,
|
limit: PAGE_SIZE,
|
||||||
cursor: pageParam,
|
cursor: pageParam,
|
||||||
})
|
})
|
||||||
return res.data
|
|
||||||
},
|
},
|
||||||
initialPageParam: undefined,
|
initialPageParam: undefined,
|
||||||
getNextPageParam: lastPage => lastPage.cursor,
|
getNextPageParam: lastPage => lastPage.cursor,
|
||||||
@@ -47,7 +46,7 @@ export function* findAllProfilesInQueryData(
|
|||||||
did: string,
|
did: string,
|
||||||
): Generator<AppBskyActorDefs.ProfileView, void> {
|
): Generator<AppBskyActorDefs.ProfileView, void> {
|
||||||
const queryDatas = queryClient.getQueriesData<
|
const queryDatas = queryClient.getQueriesData<
|
||||||
InfiniteData<AppBskyFeedGetRepostedBy.OutputSchema>
|
InfiniteData<app.bsky.feed.getRepostedBy.$OutputBody>
|
||||||
>({
|
>({
|
||||||
queryKey: [RQKEY_ROOT],
|
queryKey: [RQKEY_ROOT],
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user