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:
+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: []}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user