Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114import {type Client, type XrpcRequestParams} from '@atproto/lex'
import {app} from '#/lexicons'import * as bsky from '#/types/bsky'import {type FeedAPI, type FeedAPIResponse} from './types'
type GetAuthorFeedParams = XrpcRequestParams< typeof app.bsky.feed.getAuthorFeed.main>
export class AuthorFeedAPI implements FeedAPI { client: Client _params: GetAuthorFeedParams
constructor({ client, feedParams, }: { client: Client feedParams: GetAuthorFeedParams }) { this.client = client this._params = feedParams }
get params() { const params = {...this._params} params.includePins = params.filter === 'posts_and_author_threads' return params }
async peekLatest(): Promise<app.bsky.feed.defs.FeedViewPost> { const data = await this.client.call(app.bsky.feed.getAuthorFeed, { ...this.params, limit: 1, }) return data.feed[0] }
async fetch({ cursor, limit, }: { cursor: string | undefined limit: number }): Promise<FeedAPIResponse> { /* * 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, cursor, limit, }) return { cursor: data.cursor, feed: this._filter(data.feed), } }
_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 = 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) }) }
return feed }}
function isAuthorReplyChain( actor: string, 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 (bsky.isType(app.bsky.feed.defs.postView, replyParent)) { // reply parent is by a different user if (replyParent.author.did !== actor) return false
// A top-level post that matches the parent of the current post. const parentPost = posts.find(p => p.post.uri === replyParent.uri)
/* * Either we haven't fetched the parent at the top level, or the only * record we have is on feedItem.reply.parent, which we've already checked * above. */ if (!parentPost) return true
// Walk up to parent return isAuthorReplyChain(actor, parentPost, posts) }
// Just default to showing it return true}