diff --git a/api/util.ts b/api/util.ts --- a/api/util.ts +++ b/api/util.ts @@ -56,3 +56,17 @@ ...overrides, }); }; + +export const getThreadDepth = (opts: { + anchor: string; + depth: number; + maxThreadDepth?: number; + bigThreadUris: Set; + bigThreadDepth?: number; +}): number => { + let max = opts.maxThreadDepth; + if (opts.bigThreadUris.has(opts.anchor) && opts.bigThreadDepth) { + max = opts.bigThreadDepth; + } + return max ? Math.min(max, opts.depth) : opts.depth; +}; diff --git a/api/so/sprk/feed/getCrosspostThread.ts b/api/so/sprk/feed/getCrosspostThread.ts --- a/api/so/sprk/feed/getCrosspostThread.ts +++ b/api/so/sprk/feed/getCrosspostThread.ts @@ -23,6 +23,7 @@ import { ATPROTO_REPO_REV, createHydrateCtxFromAuth, + getThreadDepth, resHeaders, } from "../../../util.ts"; @@ -75,7 +76,13 @@ const result = await ctx.dataplane.crosspostThread.getThread( anchor, params.parentHeight, - getDepth(ctx, anchor, params), + getThreadDepth({ + anchor, + depth: params.depth, + maxThreadDepth: ctx.cfg.maxThreadDepth, + bigThreadUris: ctx.cfg.bigThreadUris, + bigThreadDepth: ctx.cfg.bigThreadDepth, + }), params.sort, ); const visibleItems = params.hydrateCtx.includeTakedowns @@ -211,14 +218,6 @@ indexedAt: item.indexedAt, }, }; -}; - -const getDepth = (ctx: Context, anchor: string, params: Params) => { - let maxDepth = ctx.cfg.maxThreadDepth; - if (ctx.cfg.bigThreadUris.has(anchor) && ctx.cfg.bigThreadDepth) { - maxDepth = ctx.cfg.bigThreadDepth; - } - return maxDepth ? Math.min(maxDepth, params.depth) : params.depth; }; const parseThreadCursor = (cursor?: string): number => { diff --git a/api/so/sprk/feed/getPostThread.ts b/api/so/sprk/feed/getPostThread.ts --- a/api/so/sprk/feed/getPostThread.ts +++ b/api/so/sprk/feed/getPostThread.ts @@ -20,6 +20,7 @@ import { ATPROTO_REPO_REV, createHydrateCtxFromAuth, + getThreadDepth, resHeaders, } from "../../../util.ts"; @@ -70,7 +71,13 @@ const res = await ctx.dataplane.threads.getThread( anchor, params.parentHeight, - getDepth(ctx, anchor, params), + getThreadDepth({ + anchor, + depth: params.depth, + maxThreadDepth: ctx.cfg.maxThreadDepth, + bigThreadUris: ctx.cfg.bigThreadUris, + bigThreadDepth: ctx.cfg.bigThreadDepth, + }), ); return { anchor, @@ -103,7 +110,13 @@ ) => { const { ctx, params, skeleton, hydration } = inputs; const thread = ctx.views.thread(skeleton, hydration, { - depth: getDepth(ctx, skeleton.anchor, params), + depth: getThreadDepth({ + anchor: skeleton.anchor, + depth: params.depth, + maxThreadDepth: ctx.cfg.maxThreadDepth, + bigThreadUris: ctx.cfg.bigThreadUris, + bigThreadDepth: ctx.cfg.bigThreadDepth, + }), }); if (isNotFoundPost(thread)) { // @TODO technically this could be returned as a NotFoundPost based on lexicon @@ -127,12 +140,4 @@ type Skeleton = { anchor: string; uris: string[]; -}; - -const getDepth = (ctx: Context, anchor: string, params: Params) => { - let maxDepth = ctx.cfg.maxThreadDepth; - if (ctx.cfg.bigThreadUris.has(anchor) && ctx.cfg.bigThreadDepth) { - maxDepth = ctx.cfg.bigThreadDepth; - } - return maxDepth ? Math.min(maxDepth, params.depth) : params.depth; };