migrate runtime call sites to lex clients and sdk actions
Phase 3 tasks 3-7 (parallel wave): composer/post pipeline on pdsClient/appviewClient with structural+instance blob guards and a golden CID fixture test; chat Convo/EventBus/queries on the dedicated chat client; preferences sugar to SDK actions on the PDS client; remaining state/queries producers (usePostThread unspecced flip, video scoped-token clients, notifications, starter packs, lists) to client.call; UI runtime sweep (AtUri from @atproto/syntax, moderation fns from @bsky.app/sdk/moderation, SDK RichText, ozone reason tokens, guard rewrites via #/types/bsky). Intermediate checkpoint (hooks skipped): ~114 typecheck errors remain in cross-boundary consumer files, resolved by the type-only codemod next. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
+24
-28
@@ -1,23 +1,21 @@
|
||||
import {
|
||||
AppBskyFeedDefs,
|
||||
type AppBskyFeedGetAuthorFeed as GetAuthorFeed,
|
||||
} from '@atproto/api'
|
||||
import {type Client} from '@atproto/lex-client'
|
||||
|
||||
import {type SessionAgent} from '#/state/session'
|
||||
import {app} from '#/lexicons'
|
||||
import * as bsky from '#/types/bsky'
|
||||
import {type FeedAPI, type FeedAPIResponse} from './types'
|
||||
|
||||
export class AuthorFeedAPI implements FeedAPI {
|
||||
agent: SessionAgent
|
||||
_params: GetAuthorFeed.QueryParams
|
||||
client: Client
|
||||
_params: app.bsky.feed.getAuthorFeed.$Params
|
||||
|
||||
constructor({
|
||||
agent,
|
||||
client,
|
||||
feedParams,
|
||||
}: {
|
||||
agent: SessionAgent
|
||||
feedParams: GetAuthorFeed.QueryParams
|
||||
client: Client
|
||||
feedParams: app.bsky.feed.getAuthorFeed.$Params
|
||||
}) {
|
||||
this.agent = agent
|
||||
this.client = client
|
||||
this._params = feedParams
|
||||
}
|
||||
|
||||
@@ -27,12 +25,12 @@ export class AuthorFeedAPI implements FeedAPI {
|
||||
return params
|
||||
}
|
||||
|
||||
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
||||
const res = await this.agent.getAuthorFeed({
|
||||
async peekLatest(): Promise<app.bsky.feed.defs.FeedViewPost> {
|
||||
const res = await this.client.call(app.bsky.feed.getAuthorFeed, {
|
||||
...this.params,
|
||||
limit: 1,
|
||||
})
|
||||
return res.data.feed[0]
|
||||
return res.feed[0]
|
||||
}
|
||||
|
||||
async fetch({
|
||||
@@ -42,28 +40,26 @@ export class AuthorFeedAPI implements FeedAPI {
|
||||
cursor: string | undefined
|
||||
limit: number
|
||||
}): Promise<FeedAPIResponse> {
|
||||
const res = await this.agent.getAuthorFeed({
|
||||
const res = await this.client.call(app.bsky.feed.getAuthorFeed, {
|
||||
...this.params,
|
||||
cursor,
|
||||
limit,
|
||||
})
|
||||
if (res.success) {
|
||||
return {
|
||||
cursor: res.data.cursor,
|
||||
feed: this._filter(res.data.feed),
|
||||
}
|
||||
}
|
||||
return {
|
||||
feed: [],
|
||||
cursor: res.cursor,
|
||||
feed: this._filter(res.feed),
|
||||
}
|
||||
}
|
||||
|
||||
_filter(feed: AppBskyFeedDefs.FeedViewPost[]) {
|
||||
_filter(feed: app.bsky.feed.defs.FeedViewPost[]) {
|
||||
if (this.params.filter === 'posts_and_author_threads') {
|
||||
return feed.filter(post => {
|
||||
const isReply = post.reply
|
||||
const isRepost = AppBskyFeedDefs.isReasonRepost(post.reason)
|
||||
const isPin = AppBskyFeedDefs.isReasonPin(post.reason)
|
||||
const isRepost = bsky.isType(
|
||||
app.bsky.feed.defs.reasonRepost,
|
||||
post.reason,
|
||||
)
|
||||
const isPin = bsky.isType(app.bsky.feed.defs.reasonPin, post.reason)
|
||||
if (!isReply) return true
|
||||
if (isRepost || isPin) return true
|
||||
return isReply && isAuthorReplyChain(this.params.actor, post, feed)
|
||||
@@ -76,15 +72,15 @@ export class AuthorFeedAPI implements FeedAPI {
|
||||
|
||||
function isAuthorReplyChain(
|
||||
actor: string,
|
||||
post: AppBskyFeedDefs.FeedViewPost,
|
||||
posts: AppBskyFeedDefs.FeedViewPost[],
|
||||
post: app.bsky.feed.defs.FeedViewPost,
|
||||
posts: app.bsky.feed.defs.FeedViewPost[],
|
||||
): boolean {
|
||||
// current post is by a different user (shouldn't happen)
|
||||
if (post.post.author.did !== actor) return false
|
||||
|
||||
const replyParent = post.reply?.parent
|
||||
|
||||
if (AppBskyFeedDefs.isPostView(replyParent)) {
|
||||
if (bsky.isType(app.bsky.feed.defs.postView, replyParent)) {
|
||||
// reply parent is by a different user
|
||||
if (replyParent.author.did !== actor) return false
|
||||
|
||||
|
||||
+67
-52
@@ -1,47 +1,53 @@
|
||||
import {
|
||||
type AppBskyFeedDefs,
|
||||
type AppBskyFeedGetFeed as GetCustomFeed,
|
||||
AtpAgent,
|
||||
jsonStringToLex,
|
||||
} from '@atproto/api'
|
||||
import {lexParse} from '@atproto/lex'
|
||||
import {Client} from '@atproto/lex-client'
|
||||
|
||||
import {
|
||||
getAppLanguageAsContentLanguage,
|
||||
getContentLanguages,
|
||||
} from '#/state/preferences/languages'
|
||||
import {type SessionAgent} from '#/state/session'
|
||||
import {app} from '#/lexicons'
|
||||
import {type FeedAPI, type FeedAPIResponse} from './types'
|
||||
import {createBskyTopicsHeader, isBlueskyOwnedFeed} from './utils'
|
||||
|
||||
/**
|
||||
* Input params for {@link CustomFeedAPI}. The generated `$Params` type reflects
|
||||
* post-parse output, where `limit` (which has a lexicon default) is required;
|
||||
* callers supply only `feed` and let `limit`/`cursor` come from `fetch`.
|
||||
*/
|
||||
type CustomFeedParams = {feed: app.bsky.feed.getFeed.$Params['feed']} & Partial<
|
||||
Omit<app.bsky.feed.getFeed.$Params, 'feed'>
|
||||
>
|
||||
|
||||
export class CustomFeedAPI implements FeedAPI {
|
||||
agent: SessionAgent
|
||||
params: GetCustomFeed.QueryParams
|
||||
client: Client
|
||||
params: CustomFeedParams
|
||||
userInterests?: string
|
||||
|
||||
constructor({
|
||||
agent,
|
||||
client,
|
||||
feedParams,
|
||||
userInterests,
|
||||
}: {
|
||||
agent: SessionAgent
|
||||
feedParams: GetCustomFeed.QueryParams
|
||||
client: Client
|
||||
feedParams: CustomFeedParams
|
||||
userInterests?: string
|
||||
}) {
|
||||
this.agent = agent
|
||||
this.client = client
|
||||
this.params = feedParams
|
||||
this.userInterests = userInterests
|
||||
}
|
||||
|
||||
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
||||
async peekLatest(): Promise<app.bsky.feed.defs.FeedViewPost> {
|
||||
const contentLangs = getContentLanguages().join(',')
|
||||
const res = await this.agent.app.bsky.feed.getFeed(
|
||||
const res = await this.client.call(
|
||||
app.bsky.feed.getFeed,
|
||||
{
|
||||
...this.params,
|
||||
limit: 1,
|
||||
},
|
||||
{headers: {'Accept-Language': contentLangs}},
|
||||
)
|
||||
return res.data.feed[0]
|
||||
return res.feed[0]
|
||||
}
|
||||
|
||||
async fetch({
|
||||
@@ -52,41 +58,49 @@ export class CustomFeedAPI implements FeedAPI {
|
||||
limit: number
|
||||
}): Promise<FeedAPIResponse> {
|
||||
const contentLangs = getContentLanguages().join(',')
|
||||
const agent = this.agent
|
||||
const isBlueskyOwned = isBlueskyOwnedFeed(this.params.feed)
|
||||
|
||||
const res = agent.did
|
||||
? await this.agent.app.bsky.feed.getFeed(
|
||||
{
|
||||
...this.params,
|
||||
cursor,
|
||||
limit,
|
||||
let feed: app.bsky.feed.defs.FeedViewPost[]
|
||||
let resCursor: string | undefined
|
||||
|
||||
if (this.client.did) {
|
||||
const res = await this.client.call(
|
||||
app.bsky.feed.getFeed,
|
||||
{
|
||||
...this.params,
|
||||
cursor,
|
||||
limit,
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
...(isBlueskyOwned
|
||||
? createBskyTopicsHeader(this.userInterests)
|
||||
: {}),
|
||||
'Accept-Language': contentLangs,
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
...(isBlueskyOwned
|
||||
? createBskyTopicsHeader(this.userInterests)
|
||||
: {}),
|
||||
'Accept-Language': contentLangs,
|
||||
},
|
||||
},
|
||||
)
|
||||
: await loggedOutFetch({...this.params, cursor, limit})
|
||||
if (res.success) {
|
||||
// NOTE
|
||||
// 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 {
|
||||
cursor: res.data.feed.length ? res.data.cursor : undefined,
|
||||
feed: res.data.feed,
|
||||
},
|
||||
)
|
||||
feed = res.feed
|
||||
resCursor = res.cursor
|
||||
} else {
|
||||
const res = await loggedOutFetch({...this.params, cursor, limit})
|
||||
if (!res.success) {
|
||||
return {feed: []}
|
||||
}
|
||||
feed = res.data.feed
|
||||
resCursor = res.data.cursor
|
||||
}
|
||||
|
||||
// NOTE
|
||||
// some custom feeds fail to enforce the pagination limit
|
||||
// so we manually truncate here
|
||||
// -prf
|
||||
if (feed.length > limit) {
|
||||
feed = feed.slice(0, limit)
|
||||
}
|
||||
return {
|
||||
feed: [],
|
||||
cursor: feed.length ? resCursor : undefined,
|
||||
feed,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -106,15 +120,16 @@ async function loggedOutFetch({
|
||||
feed: string
|
||||
limit: number
|
||||
cursor?: string
|
||||
}) {
|
||||
}): Promise<{success: boolean; data: app.bsky.feed.getFeed.$OutputBody}> {
|
||||
let contentLangs = getAppLanguageAsContentLanguage()
|
||||
|
||||
/**
|
||||
* Copied from our root `Agent` class
|
||||
* @see https://github.com/bluesky-social/atproto/blob/60df3fc652b00cdff71dd9235d98a7a4bb828f05/packages/api/src/agent.ts#L120
|
||||
/*
|
||||
* Copied from our root `Agent` class. The global (`;redact`-suffixed) app
|
||||
* labelers are kept on the lex `Client` static (synced in
|
||||
* `#/state/session/moderation`), replacing the old `AtpAgent.appLabelers`.
|
||||
*/
|
||||
const labelersHeader = {
|
||||
'atproto-accept-labelers': AtpAgent.appLabelers
|
||||
'atproto-accept-labelers': Client.appLabelers
|
||||
.map(l => `${l};redact`)
|
||||
.join(', '),
|
||||
}
|
||||
@@ -130,7 +145,7 @@ async function loggedOutFetch({
|
||||
},
|
||||
)
|
||||
let data = res.ok
|
||||
? (jsonStringToLex(await res.text()) as GetCustomFeed.OutputSchema)
|
||||
? (lexParse(await res.text()) as app.bsky.feed.getFeed.$OutputBody)
|
||||
: null
|
||||
if (data?.feed?.length) {
|
||||
return {
|
||||
@@ -147,7 +162,7 @@ async function loggedOutFetch({
|
||||
{method: 'GET', headers: {'Accept-Language': '', ...labelersHeader}},
|
||||
)
|
||||
data = res.ok
|
||||
? (jsonStringToLex(await res.text()) as GetCustomFeed.OutputSchema)
|
||||
? (lexParse(await res.text()) as app.bsky.feed.getFeed.$OutputBody)
|
||||
: null
|
||||
if (data?.feed?.length) {
|
||||
return {
|
||||
|
||||
@@ -1,21 +1,28 @@
|
||||
import {type AppBskyFeedDefs} from '@atproto/api'
|
||||
import {type Client} from '@atproto/lex-client'
|
||||
|
||||
import {DEMO_FEED} from '#/lib/demo'
|
||||
import {type SessionAgent} from '#/state/session'
|
||||
import {type app} from '#/lexicons'
|
||||
import {toLex} from '#/types/bsky'
|
||||
import {type FeedAPI, type FeedAPIResponse} from './types'
|
||||
|
||||
export class DemoFeedAPI implements FeedAPI {
|
||||
agent: SessionAgent
|
||||
client: Client
|
||||
|
||||
constructor({agent}: {agent: SessionAgent}) {
|
||||
this.agent = agent
|
||||
constructor({client}: {client: Client}) {
|
||||
this.client = client
|
||||
}
|
||||
|
||||
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
||||
return DEMO_FEED.feed[0]
|
||||
async peekLatest(): Promise<app.bsky.feed.defs.FeedViewPost> {
|
||||
/*
|
||||
* TODO(phase4): drop this cast once `#/lib/demo` (DEMO_FEED) sources its
|
||||
* feed items from `#/lexicons`. Its records are still typed against the old
|
||||
* `@atproto/api` FeedViewPost, which does not assign to the branded lexicon
|
||||
* type this method must return.
|
||||
*/
|
||||
return toLex(DEMO_FEED.feed[0])
|
||||
}
|
||||
|
||||
async fetch(): Promise<FeedAPIResponse> {
|
||||
return DEMO_FEED
|
||||
return toLex(DEMO_FEED)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import {type AppBskyFeedDefs} from '@atproto/api'
|
||||
import {type Client} from '@atproto/lex-client'
|
||||
|
||||
import {type SessionAgent} from '#/state/session'
|
||||
import {app} from '#/lexicons'
|
||||
import {type FeedAPI, type FeedAPIResponse} from './types'
|
||||
|
||||
export class FollowingFeedAPI implements FeedAPI {
|
||||
agent: SessionAgent
|
||||
client: Client
|
||||
|
||||
constructor({agent}: {agent: SessionAgent}) {
|
||||
this.agent = agent
|
||||
constructor({client}: {client: Client}) {
|
||||
this.client = client
|
||||
}
|
||||
|
||||
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
||||
const res = await this.agent.getTimeline({
|
||||
async peekLatest(): Promise<app.bsky.feed.defs.FeedViewPost> {
|
||||
const res = await this.client.call(app.bsky.feed.getTimeline, {
|
||||
limit: 1,
|
||||
})
|
||||
return res.data.feed[0]
|
||||
return res.feed[0]
|
||||
}
|
||||
|
||||
async fetch({
|
||||
@@ -24,18 +24,13 @@ export class FollowingFeedAPI implements FeedAPI {
|
||||
cursor: string | undefined
|
||||
limit: number
|
||||
}): Promise<FeedAPIResponse> {
|
||||
const res = await this.agent.getTimeline({
|
||||
const res = await this.client.call(app.bsky.feed.getTimeline, {
|
||||
cursor,
|
||||
limit,
|
||||
})
|
||||
if (res.success) {
|
||||
return {
|
||||
cursor: res.data.cursor,
|
||||
feed: res.data.feed,
|
||||
}
|
||||
}
|
||||
return {
|
||||
feed: [],
|
||||
cursor: res.cursor,
|
||||
feed: res.feed,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+21
-16
@@ -1,7 +1,8 @@
|
||||
import {type AppBskyFeedDefs} from '@atproto/api'
|
||||
import {type Client} from '@atproto/lex-client'
|
||||
import {type AtUriString} from '@atproto/syntax'
|
||||
|
||||
import {PROD_DEFAULT_FEED} from '#/lib/constants'
|
||||
import {type SessionAgent} from '#/state/session'
|
||||
import {type app} from '#/lexicons'
|
||||
import {CustomFeedAPI} from './custom'
|
||||
import {FollowingFeedAPI} from './following'
|
||||
import {type FeedAPI, type FeedAPIResponse} from './types'
|
||||
@@ -14,7 +15,11 @@ import {type FeedAPI, type FeedAPIResponse} from './types'
|
||||
// we use this fallback marker post to drive this instead. see Feed.tsx
|
||||
// for the usage.
|
||||
// -prf
|
||||
export const FALLBACK_MARKER_POST: AppBskyFeedDefs.FeedViewPost = {
|
||||
/*
|
||||
* A sentinel post whose fields intentionally violate the branded lexicon
|
||||
* formats (`uri`, `did`, `indexedAt`), so it is asserted into the view type.
|
||||
*/
|
||||
export const FALLBACK_MARKER_POST: app.bsky.feed.defs.FeedViewPost = {
|
||||
post: {
|
||||
uri: 'fallback-marker-post',
|
||||
cid: 'fake',
|
||||
@@ -25,10 +30,10 @@ export const FALLBACK_MARKER_POST: AppBskyFeedDefs.FeedViewPost = {
|
||||
},
|
||||
indexedAt: new Date().toISOString(),
|
||||
},
|
||||
}
|
||||
} as unknown as app.bsky.feed.defs.FeedViewPost
|
||||
|
||||
export class HomeFeedAPI implements FeedAPI {
|
||||
agent: SessionAgent
|
||||
client: Client
|
||||
following: FollowingFeedAPI
|
||||
discover: CustomFeedAPI
|
||||
usingDiscover = false
|
||||
@@ -37,32 +42,32 @@ export class HomeFeedAPI implements FeedAPI {
|
||||
|
||||
constructor({
|
||||
userInterests,
|
||||
agent,
|
||||
client,
|
||||
}: {
|
||||
userInterests?: string
|
||||
agent: SessionAgent
|
||||
client: Client
|
||||
}) {
|
||||
this.agent = agent
|
||||
this.following = new FollowingFeedAPI({agent})
|
||||
this.client = client
|
||||
this.following = new FollowingFeedAPI({client})
|
||||
this.discover = new CustomFeedAPI({
|
||||
agent,
|
||||
feedParams: {feed: PROD_DEFAULT_FEED('whats-hot')},
|
||||
client,
|
||||
feedParams: {feed: PROD_DEFAULT_FEED('whats-hot') as AtUriString},
|
||||
})
|
||||
this.userInterests = userInterests
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.following = new FollowingFeedAPI({agent: this.agent})
|
||||
this.following = new FollowingFeedAPI({client: this.client})
|
||||
this.discover = new CustomFeedAPI({
|
||||
agent: this.agent,
|
||||
feedParams: {feed: PROD_DEFAULT_FEED('whats-hot')},
|
||||
client: this.client,
|
||||
feedParams: {feed: PROD_DEFAULT_FEED('whats-hot') as AtUriString},
|
||||
userInterests: this.userInterests,
|
||||
})
|
||||
this.usingDiscover = false
|
||||
this.itemCursor = 0
|
||||
}
|
||||
|
||||
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
||||
async peekLatest(): Promise<app.bsky.feed.defs.FeedViewPost> {
|
||||
if (this.usingDiscover) {
|
||||
return this.discover.peekLatest()
|
||||
}
|
||||
@@ -81,7 +86,7 @@ export class HomeFeedAPI implements FeedAPI {
|
||||
}
|
||||
|
||||
let returnCursor
|
||||
let posts: AppBskyFeedDefs.FeedViewPost[] = []
|
||||
let posts: app.bsky.feed.defs.FeedViewPost[] = []
|
||||
|
||||
if (!this.usingDiscover) {
|
||||
const res = await this.following.fetch({cursor, limit})
|
||||
|
||||
+16
-24
@@ -1,32 +1,29 @@
|
||||
import {
|
||||
type AppBskyFeedDefs,
|
||||
type AppBskyFeedGetActorLikes as GetActorLikes,
|
||||
} from '@atproto/api'
|
||||
import {type Client} from '@atproto/lex-client'
|
||||
|
||||
import {type SessionAgent} from '#/state/session'
|
||||
import {app} from '#/lexicons'
|
||||
import {type FeedAPI, type FeedAPIResponse} from './types'
|
||||
|
||||
export class LikesFeedAPI implements FeedAPI {
|
||||
agent: SessionAgent
|
||||
params: GetActorLikes.QueryParams
|
||||
client: Client
|
||||
params: app.bsky.feed.getActorLikes.$Params
|
||||
|
||||
constructor({
|
||||
agent,
|
||||
client,
|
||||
feedParams,
|
||||
}: {
|
||||
agent: SessionAgent
|
||||
feedParams: GetActorLikes.QueryParams
|
||||
client: Client
|
||||
feedParams: app.bsky.feed.getActorLikes.$Params
|
||||
}) {
|
||||
this.agent = agent
|
||||
this.client = client
|
||||
this.params = feedParams
|
||||
}
|
||||
|
||||
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
||||
const res = await this.agent.getActorLikes({
|
||||
async peekLatest(): Promise<app.bsky.feed.defs.FeedViewPost> {
|
||||
const res = await this.client.call(app.bsky.feed.getActorLikes, {
|
||||
...this.params,
|
||||
limit: 1,
|
||||
})
|
||||
return res.data.feed[0]
|
||||
return res.feed[0]
|
||||
}
|
||||
|
||||
async fetch({
|
||||
@@ -36,21 +33,16 @@ export class LikesFeedAPI implements FeedAPI {
|
||||
cursor: string | undefined
|
||||
limit: number
|
||||
}): Promise<FeedAPIResponse> {
|
||||
const res = await this.agent.getActorLikes({
|
||||
const res = await this.client.call(app.bsky.feed.getActorLikes, {
|
||||
...this.params,
|
||||
cursor,
|
||||
limit,
|
||||
})
|
||||
if (res.success) {
|
||||
// HACKFIX: the API incorrectly returns a cursor when there are no items -sfn
|
||||
const isEmptyPage = res.data.feed.length === 0
|
||||
return {
|
||||
cursor: isEmptyPage ? undefined : res.data.cursor,
|
||||
feed: res.data.feed,
|
||||
}
|
||||
}
|
||||
// HACKFIX: the API incorrectly returns a cursor when there are no items -sfn
|
||||
const isEmptyPage = res.feed.length === 0
|
||||
return {
|
||||
feed: [],
|
||||
cursor: isEmptyPage ? undefined : res.cursor,
|
||||
feed: res.feed,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+14
-22
@@ -1,32 +1,29 @@
|
||||
import {
|
||||
type Agent,
|
||||
type AppBskyFeedDefs,
|
||||
type AppBskyFeedGetListFeed as GetListFeed,
|
||||
} from '@atproto/api'
|
||||
import {type Client} from '@atproto/lex-client'
|
||||
|
||||
import {app} from '#/lexicons'
|
||||
import {type FeedAPI, type FeedAPIResponse} from './types'
|
||||
|
||||
export class ListFeedAPI implements FeedAPI {
|
||||
agent: Agent
|
||||
params: GetListFeed.QueryParams
|
||||
client: Client
|
||||
params: app.bsky.feed.getListFeed.$Params
|
||||
|
||||
constructor({
|
||||
agent,
|
||||
client,
|
||||
feedParams,
|
||||
}: {
|
||||
agent: Agent
|
||||
feedParams: GetListFeed.QueryParams
|
||||
client: Client
|
||||
feedParams: app.bsky.feed.getListFeed.$Params
|
||||
}) {
|
||||
this.agent = agent
|
||||
this.client = client
|
||||
this.params = feedParams
|
||||
}
|
||||
|
||||
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
||||
const res = await this.agent.app.bsky.feed.getListFeed({
|
||||
async peekLatest(): Promise<app.bsky.feed.defs.FeedViewPost> {
|
||||
const res = await this.client.call(app.bsky.feed.getListFeed, {
|
||||
...this.params,
|
||||
limit: 1,
|
||||
})
|
||||
return res.data.feed[0]
|
||||
return res.feed[0]
|
||||
}
|
||||
|
||||
async fetch({
|
||||
@@ -36,19 +33,14 @@ export class ListFeedAPI implements FeedAPI {
|
||||
cursor: string | undefined
|
||||
limit: number
|
||||
}): Promise<FeedAPIResponse> {
|
||||
const res = await this.agent.app.bsky.feed.getListFeed({
|
||||
const res = await this.client.call(app.bsky.feed.getListFeed, {
|
||||
...this.params,
|
||||
cursor,
|
||||
limit,
|
||||
})
|
||||
if (res.success) {
|
||||
return {
|
||||
cursor: res.data.cursor,
|
||||
feed: res.data.feed,
|
||||
}
|
||||
}
|
||||
return {
|
||||
feed: [],
|
||||
cursor: res.cursor,
|
||||
feed: res.feed,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+70
-44
@@ -1,4 +1,5 @@
|
||||
import {type AppBskyFeedDefs, type AppBskyFeedGetTimeline} from '@atproto/api'
|
||||
import {type Client} from '@atproto/lex-client'
|
||||
import {type AtUriString} from '@atproto/syntax'
|
||||
import shuffle from 'lodash.shuffle'
|
||||
|
||||
import {bundleAsync} from '#/lib/async/bundle'
|
||||
@@ -6,7 +7,8 @@ import {timeout} from '#/lib/async/timeout'
|
||||
import {feedUriToHref} from '#/lib/strings/url-helpers'
|
||||
import {getContentLanguages} from '#/state/preferences/languages'
|
||||
import {type FeedParams} from '#/state/queries/post-feed'
|
||||
import {type SessionAgent} from '#/state/session'
|
||||
import {app} from '#/lexicons'
|
||||
import {toLex} from '#/types/bsky'
|
||||
import {FeedTuner} from '../feed-manip'
|
||||
import {type FeedTunerFn} from '../feed-manip'
|
||||
import {
|
||||
@@ -19,9 +21,21 @@ import {createBskyTopicsHeader, isBlueskyOwnedFeed} from './utils'
|
||||
const REQUEST_WAIT_MS = 500 // 500ms
|
||||
const POST_AGE_CUTOFF = 60e3 * 60 * 24 // 24hours
|
||||
|
||||
/**
|
||||
* Internal result shape for a single feed page fetch. Lex `client.call`
|
||||
* returns the response body directly (throwing on error), so we no longer
|
||||
* carry the old `{success, headers, data}` wrapper - `success` here just
|
||||
* distinguishes an empty/errored fetch from a populated one.
|
||||
*/
|
||||
type FeedPage = {
|
||||
success: boolean
|
||||
cursor?: string
|
||||
feed: app.bsky.feed.defs.FeedViewPost[]
|
||||
}
|
||||
|
||||
export class MergeFeedAPI implements FeedAPI {
|
||||
userInterests?: string
|
||||
agent: SessionAgent
|
||||
client: Client
|
||||
params: FeedParams
|
||||
feedTuners: FeedTunerFn[]
|
||||
following: MergeFeedSource_Following
|
||||
@@ -31,29 +45,29 @@ export class MergeFeedAPI implements FeedAPI {
|
||||
sampleCursor = 0
|
||||
|
||||
constructor({
|
||||
agent,
|
||||
client,
|
||||
feedParams,
|
||||
feedTuners,
|
||||
userInterests,
|
||||
}: {
|
||||
agent: SessionAgent
|
||||
client: Client
|
||||
feedParams: FeedParams
|
||||
feedTuners: FeedTunerFn[]
|
||||
userInterests?: string
|
||||
}) {
|
||||
this.agent = agent
|
||||
this.client = client
|
||||
this.params = feedParams
|
||||
this.feedTuners = feedTuners
|
||||
this.userInterests = userInterests
|
||||
this.following = new MergeFeedSource_Following({
|
||||
agent: this.agent,
|
||||
client: this.client,
|
||||
feedTuners: this.feedTuners,
|
||||
})
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.following = new MergeFeedSource_Following({
|
||||
agent: this.agent,
|
||||
client: this.client,
|
||||
feedTuners: this.feedTuners,
|
||||
})
|
||||
this.customFeeds = []
|
||||
@@ -65,7 +79,7 @@ export class MergeFeedAPI implements FeedAPI {
|
||||
this.params.mergeFeedSources.map(
|
||||
feedUri =>
|
||||
new MergeFeedSource_Custom({
|
||||
agent: this.agent,
|
||||
client: this.client,
|
||||
feedUri,
|
||||
feedTuners: this.feedTuners,
|
||||
userInterests: this.userInterests,
|
||||
@@ -77,11 +91,11 @@ export class MergeFeedAPI implements FeedAPI {
|
||||
}
|
||||
}
|
||||
|
||||
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
||||
const res = await this.agent.getTimeline({
|
||||
async peekLatest(): Promise<app.bsky.feed.defs.FeedViewPost> {
|
||||
const res = await this.client.call(app.bsky.feed.getTimeline, {
|
||||
limit: 1,
|
||||
})
|
||||
return res.data.feed[0]
|
||||
return res.feed[0]
|
||||
}
|
||||
|
||||
async fetch({
|
||||
@@ -124,7 +138,7 @@ export class MergeFeedAPI implements FeedAPI {
|
||||
await Promise.all(promises)
|
||||
|
||||
// assemble a response by sampling from feeds with content
|
||||
const posts: AppBskyFeedDefs.FeedViewPost[] = []
|
||||
const posts: app.bsky.feed.defs.FeedViewPost[] = []
|
||||
while (posts.length < limit) {
|
||||
let slice = this.sampleItem()
|
||||
if (slice[0]) {
|
||||
@@ -172,21 +186,21 @@ export class MergeFeedAPI implements FeedAPI {
|
||||
}
|
||||
|
||||
class MergeFeedSource {
|
||||
agent: SessionAgent
|
||||
client: Client
|
||||
feedTuners: FeedTunerFn[]
|
||||
sourceInfo: ReasonFeedSource | undefined
|
||||
cursor: string | undefined = undefined
|
||||
queue: AppBskyFeedDefs.FeedViewPost[] = []
|
||||
queue: app.bsky.feed.defs.FeedViewPost[] = []
|
||||
hasMore = true
|
||||
|
||||
constructor({
|
||||
agent,
|
||||
client,
|
||||
feedTuners,
|
||||
}: {
|
||||
agent: SessionAgent
|
||||
client: Client
|
||||
feedTuners: FeedTunerFn[]
|
||||
}) {
|
||||
this.agent = agent
|
||||
this.client = client
|
||||
this.feedTuners = feedTuners
|
||||
}
|
||||
|
||||
@@ -198,7 +212,7 @@ class MergeFeedSource {
|
||||
return this.hasMore && this.queue.length === 0
|
||||
}
|
||||
|
||||
take(n: number): AppBskyFeedDefs.FeedViewPost[] {
|
||||
take(n: number): app.bsky.feed.defs.FeedViewPost[] {
|
||||
return this.queue.splice(0, n)
|
||||
}
|
||||
|
||||
@@ -209,9 +223,9 @@ class MergeFeedSource {
|
||||
_fetchNextInner = bundleAsync(async (n: number) => {
|
||||
const res = await this._getFeed(this.cursor, n)
|
||||
if (res.success) {
|
||||
this.cursor = res.data.cursor
|
||||
if (res.data.feed.length) {
|
||||
this.queue = this.queue.concat(res.data.feed)
|
||||
this.cursor = res.cursor
|
||||
if (res.feed.length) {
|
||||
this.queue = this.queue.concat(res.feed)
|
||||
} else {
|
||||
this.hasMore = false
|
||||
}
|
||||
@@ -223,7 +237,7 @@ class MergeFeedSource {
|
||||
protected _getFeed(
|
||||
_cursor: string | undefined,
|
||||
_limit: number,
|
||||
): Promise<AppBskyFeedGetTimeline.Response> {
|
||||
): Promise<FeedPage> {
|
||||
throw new Error('Must be overridden')
|
||||
}
|
||||
}
|
||||
@@ -238,39 +252,51 @@ class MergeFeedSource_Following extends MergeFeedSource {
|
||||
protected async _getFeed(
|
||||
cursor: string | undefined,
|
||||
limit: number,
|
||||
): Promise<AppBskyFeedGetTimeline.Response> {
|
||||
const res = await this.agent.getTimeline({cursor, limit})
|
||||
): Promise<FeedPage> {
|
||||
const res = await this.client.call(app.bsky.feed.getTimeline, {
|
||||
cursor,
|
||||
limit,
|
||||
})
|
||||
// run the tuner pre-emptively to ensure better mixing
|
||||
const slices = this.tuner.tune(res.data.feed, {
|
||||
const slices = this.tuner.tune(res.feed, {
|
||||
dryRun: false,
|
||||
})
|
||||
res.data.feed = slices.map(slice => slice._feedPost)
|
||||
return res
|
||||
return {
|
||||
success: true,
|
||||
cursor: res.cursor,
|
||||
/*
|
||||
* TODO(phase4): drop the toLex once `#/lib/api/feed-manip` (FeedTuner)
|
||||
* flips its FeedViewPost source from `@atproto/api` to `#/lexicons`. Its
|
||||
* `_feedPost` is still the old-world view type, which does not assign to
|
||||
* the branded lexicon type this page carries.
|
||||
*/
|
||||
feed: slices.map(slice => toLex(slice._feedPost)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MergeFeedSource_Custom extends MergeFeedSource {
|
||||
agent: SessionAgent
|
||||
client: Client
|
||||
minDate: Date
|
||||
feedUri: string
|
||||
userInterests?: string
|
||||
|
||||
constructor({
|
||||
agent,
|
||||
client,
|
||||
feedUri,
|
||||
feedTuners,
|
||||
userInterests,
|
||||
}: {
|
||||
agent: SessionAgent
|
||||
client: Client
|
||||
feedUri: string
|
||||
feedTuners: FeedTunerFn[]
|
||||
userInterests?: string
|
||||
}) {
|
||||
super({
|
||||
agent,
|
||||
client,
|
||||
feedTuners,
|
||||
})
|
||||
this.agent = agent
|
||||
this.client = client
|
||||
this.feedUri = feedUri
|
||||
this.userInterests = userInterests
|
||||
this.sourceInfo = {
|
||||
@@ -284,15 +310,16 @@ class MergeFeedSource_Custom extends MergeFeedSource {
|
||||
protected async _getFeed(
|
||||
cursor: string | undefined,
|
||||
limit: number,
|
||||
): Promise<AppBskyFeedGetTimeline.Response> {
|
||||
): Promise<FeedPage> {
|
||||
try {
|
||||
const contentLangs = getContentLanguages().join(',')
|
||||
const isBlueskyOwned = isBlueskyOwnedFeed(this.feedUri)
|
||||
const res = await this.agent.app.bsky.feed.getFeed(
|
||||
const res = await this.client.call(
|
||||
app.bsky.feed.getFeed,
|
||||
{
|
||||
cursor,
|
||||
limit,
|
||||
feed: this.feedUri,
|
||||
feed: this.feedUri as AtUriString,
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
@@ -303,26 +330,25 @@ class MergeFeedSource_Custom extends MergeFeedSource {
|
||||
},
|
||||
},
|
||||
)
|
||||
let feed = res.feed
|
||||
// NOTE
|
||||
// some custom feeds fail to enforce the pagination limit
|
||||
// so we manually truncate here
|
||||
// -prf
|
||||
if (limit && res.data.feed.length > limit) {
|
||||
res.data.feed = res.data.feed.slice(0, limit)
|
||||
if (limit && feed.length > limit) {
|
||||
feed = feed.slice(0, limit)
|
||||
}
|
||||
// filter out older posts
|
||||
res.data.feed = res.data.feed.filter(
|
||||
post => new Date(post.post.indexedAt) > this.minDate,
|
||||
)
|
||||
feed = feed.filter(post => new Date(post.post.indexedAt) > this.minDate)
|
||||
// attach source info
|
||||
for (const post of res.data.feed) {
|
||||
for (const post of feed) {
|
||||
// @ts-ignore
|
||||
post.__source = this.sourceInfo
|
||||
}
|
||||
return res
|
||||
return {success: true, cursor: res.cursor, feed}
|
||||
} catch {
|
||||
// dont bubble custom-feed errors
|
||||
return {success: false, headers: {}, data: {feed: []}}
|
||||
return {success: false, feed: []}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+13
-21
@@ -1,25 +1,22 @@
|
||||
import {
|
||||
type Agent,
|
||||
type AppBskyFeedDefs,
|
||||
type AppBskyFeedGetPosts,
|
||||
} from '@atproto/api'
|
||||
import {type Client} from '@atproto/lex-client'
|
||||
|
||||
import {logger} from '#/logger'
|
||||
import {app} from '#/lexicons'
|
||||
import {type FeedAPI, type FeedAPIResponse} from './types'
|
||||
|
||||
export class PostListFeedAPI implements FeedAPI {
|
||||
agent: Agent
|
||||
params: AppBskyFeedGetPosts.QueryParams
|
||||
peek: AppBskyFeedDefs.FeedViewPost | null = null
|
||||
client: Client
|
||||
params: app.bsky.feed.getPosts.$Params
|
||||
peek: app.bsky.feed.defs.FeedViewPost | null = null
|
||||
|
||||
constructor({
|
||||
agent,
|
||||
client,
|
||||
feedParams,
|
||||
}: {
|
||||
agent: Agent
|
||||
feedParams: AppBskyFeedGetPosts.QueryParams
|
||||
client: Client
|
||||
feedParams: app.bsky.feed.getPosts.$Params
|
||||
}) {
|
||||
this.agent = agent
|
||||
this.client = client
|
||||
if (feedParams.uris.length > 25) {
|
||||
logger.warn(
|
||||
`Too many URIs provided - expected 25, got ${feedParams.uris.length}`,
|
||||
@@ -30,23 +27,18 @@ export class PostListFeedAPI implements FeedAPI {
|
||||
}
|
||||
}
|
||||
|
||||
async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> {
|
||||
async peekLatest(): Promise<app.bsky.feed.defs.FeedViewPost> {
|
||||
if (this.peek) return this.peek
|
||||
throw new Error('Has not fetched yet')
|
||||
}
|
||||
|
||||
async fetch({}: {}): Promise<FeedAPIResponse> {
|
||||
const res = await this.agent.app.bsky.feed.getPosts({
|
||||
const res = await this.client.call(app.bsky.feed.getPosts, {
|
||||
...this.params,
|
||||
})
|
||||
if (res.success) {
|
||||
this.peek = {post: res.data.posts[0]}
|
||||
return {
|
||||
feed: res.data.posts.map(post => ({post})),
|
||||
}
|
||||
}
|
||||
this.peek = {post: res.posts[0]}
|
||||
return {
|
||||
feed: [],
|
||||
feed: res.posts.map(post => ({post})),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import {type AppBskyFeedDefs} from '@atproto/api'
|
||||
import {type app} from '#/lexicons'
|
||||
|
||||
export interface FeedAPIResponse {
|
||||
cursor?: string
|
||||
feed: AppBskyFeedDefs.FeedViewPost[]
|
||||
feed: app.bsky.feed.defs.FeedViewPost[]
|
||||
}
|
||||
|
||||
export interface FeedAPI {
|
||||
peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost>
|
||||
peekLatest(): Promise<app.bsky.feed.defs.FeedViewPost>
|
||||
fetch({
|
||||
cursor,
|
||||
limit,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import {AtUri} from '@atproto/api'
|
||||
import {AtUri} from '@atproto/syntax'
|
||||
|
||||
import {BSKY_FEED_OWNER_DIDS} from '#/lib/constants'
|
||||
import {type UsePreferencesQueryResponse} from '#/state/queries/preferences'
|
||||
|
||||
Reference in New Issue
Block a user