import { mapDefined } from '@atproto/common' import { AtUriString } from '@atproto/syntax' import { Server } from '@atproto/xrpc-server' import { AppContext } from '../../../../context.js' import { DataPlaneClient } from '../../../../data-plane/index.js' import { FeedItem } from '../../../../hydration/feed.js' import { HydrateCtxWithViewer, HydrationState, Hydrator, } from '../../../../hydration/hydrator.js' import { parseString } from '../../../../hydration/util.js' import { app } from '../../../../lexicons/index.js' import { createPipeline } from '../../../../pipeline.js' import { Views } from '../../../../views/index.js' import { clearlyBadCursor, resHeaders } from '../../../util.js' export default function (server: Server, ctx: AppContext) { const getTimeline = createPipeline( skeleton, hydration, noBlocksOrMutes, presentation, ) server.add(app.bsky.feed.getTimeline, { auth: ctx.authVerifier.standard, opts: { // @TODO remove after grace period has passed, behavior is non-standard. // temporarily added for compat w/ previous version of xrpc-server to avoid breakage of a few specified parties. paramsParseLoose: true, }, handler: async ({ params, auth, req }) => { const viewer = auth.credentials.iss const labelers = ctx.reqLabelers(req) const hydrateCtx = await ctx.hydrator.createContext({ labelers, viewer }) const result = await getTimeline({ ...params, hydrateCtx }, ctx) const repoRev = await ctx.hydrator.actor.getRepoRevSafe(viewer) return { encoding: 'application/json', body: result, headers: resHeaders({ labelers: hydrateCtx.labelers, repoRev }), } }, }) } export const skeleton = async (inputs: { ctx: Context params: Params }): Promise => { const { ctx, params } = inputs if (clearlyBadCursor(params.cursor)) { return { items: [] } } const res = await ctx.dataplane.getTimeline({ actorDid: params.hydrateCtx.viewer, limit: params.limit, cursor: params.cursor, }) return { items: res.items.map((item) => ({ post: { uri: item.uri as AtUriString, cid: item.cid || undefined }, repost: item.repost ? { uri: item.repost as AtUriString, cid: item.repostCid || undefined } : undefined, })), cursor: parseString(res.cursor), } } const hydration = async (inputs: { ctx: Context params: Params skeleton: Skeleton }): Promise => { const { ctx, params, skeleton } = inputs return ctx.hydrator.hydrateFeedItems(skeleton.items, params.hydrateCtx) } const noBlocksOrMutes = (inputs: { ctx: Context skeleton: Skeleton hydration: HydrationState }): Skeleton => { const { ctx, skeleton, hydration } = inputs skeleton.items = skeleton.items.filter((item) => { const bam = ctx.views.feedItemBlocksAndMutes(item, hydration) return ( !bam.authorBlocked && !bam.authorMuted && !bam.originatorBlocked && !bam.originatorMuted && !bam.ancestorAuthorBlocked ) }) return skeleton } const presentation = (inputs: { ctx: Context skeleton: Skeleton hydration: HydrationState }) => { const { ctx, skeleton, hydration } = inputs const feed = mapDefined(skeleton.items, (item) => ctx.views.feedViewPost(item, hydration), ) return { feed, cursor: skeleton.cursor } } type Context = { hydrator: Hydrator views: Views dataplane: DataPlaneClient } type Params = app.bsky.feed.getTimeline.$Params & { hydrateCtx: HydrateCtxWithViewer } type Skeleton = { items: FeedItem[] cursor?: string }