import { noUndefinedVals } from '@atproto/common' import { Client } from '@atproto/lex' import { MethodNotImplementedError, Server } from '@atproto/xrpc-server' import { AppContext } from '../../../../context.js' import { HydrateCtx, Hydrator } from '../../../../hydration/hydrator.js' import { app } from '../../../../lexicons/index.js' import { HydrationFn, PresentationFn, RulesFn, SkeletonFn, createPipeline, } from '../../../../pipeline.js' import { Views } from '../../../../views/index.js' export default function (server: Server, ctx: AppContext) { const getTrendingTopics = createPipeline( skeleton, hydration, noBlocksOrMutes, presentation, ) server.add(app.bsky.unspecced.getTrendingTopics, { auth: ctx.authVerifier.standardOptional, handler: async ({ auth, params, req }) => { const viewer = auth.credentials.iss const labelers = ctx.reqLabelers(req) const hydrateCtx = await ctx.hydrator.createContext({ labelers, viewer }) const headers = noUndefinedVals({ 'accept-language': req.headers['accept-language'], }) const result = await getTrendingTopics( { ...params, hydrateCtx, headers, }, ctx, ) return { encoding: 'application/json', body: result, } }, }) } const skeleton: SkeletonFn = async (input) => { const { params, ctx } = input if (!ctx.topicsClient) { // Use 501 instead of 500 as these are not considered retry-able by clients throw new MethodNotImplementedError('Topics agent not available') } return ctx.topicsClient.call( app.bsky.unspecced.getTrendingTopics, { limit: params.limit, viewer: params.hydrateCtx.viewer ?? undefined, }, { headers: params.headers, }, ) } const hydration: HydrationFn = async () => { return {} } const noBlocksOrMutes: RulesFn = (input) => { return input.skeleton } const presentation: PresentationFn< Context, Params, SkeletonState, SkeletonState > = (input) => { return input.skeleton } type Context = { hydrator: Hydrator views: Views topicsClient: Client | undefined } type Params = Omit & { hydrateCtx: HydrateCtx headers: Record } type SkeletonState = app.bsky.unspecced.getTrendingTopics.$OutputBody