-
Notifications
You must be signed in to change notification settings - Fork 466
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement searchStarterPacks endpoint
- Loading branch information
1 parent
d522841
commit 45e0fc3
Showing
4 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
packages/bsky/src/api/app/bsky/graph/searchStarterPacks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import AppContext from '../../../../context' | ||
import { Server } from '../../../../lexicon' | ||
import { mapDefined } from '@atproto/common' | ||
import { AtpAgent, AtUri } from '@atproto/api' | ||
import { QueryParams } from '../../../../lexicon/types/app/bsky/graph/searchStarterPacks' | ||
import { | ||
HydrationFnInput, | ||
PresentationFnInput, | ||
RulesFnInput, | ||
SkeletonFnInput, | ||
createPipeline, | ||
} from '../../../../pipeline' | ||
import { HydrateCtx, Hydrator } from '../../../../hydration/hydrator' | ||
import { Views } from '../../../../views' | ||
import { DataPlaneClient } from '../../../../data-plane' | ||
import { parseString } from '../../../../hydration/util' | ||
import { resHeaders } from '../../../util' | ||
|
||
export default function (server: Server, ctx: AppContext) { | ||
const searchStarterPacks = createPipeline( | ||
skeleton, | ||
hydration, | ||
noBlocks, | ||
presentation, | ||
) | ||
server.app.bsky.graph.searchStarterPacks({ | ||
auth: ctx.authVerifier.standardOptional, | ||
handler: async ({ auth, params, req }) => { | ||
const { viewer, includeTakedowns } = ctx.authVerifier.parseCreds(auth) | ||
const labelers = ctx.reqLabelers(req) | ||
const hydrateCtx = await ctx.hydrator.createContext({ | ||
viewer, | ||
labelers, | ||
includeTakedowns, | ||
}) | ||
const results = await searchStarterPacks({ ...params, hydrateCtx }, ctx) | ||
return { | ||
encoding: 'application/json', | ||
body: results, | ||
headers: resHeaders({ labelers: hydrateCtx.labelers }), | ||
} | ||
}, | ||
}) | ||
} | ||
|
||
const skeleton = async (inputs: SkeletonFnInput<Context, Params>) => { | ||
const { ctx, params } = inputs | ||
const { q } = params | ||
|
||
if (ctx.searchAgent) { | ||
// @NOTE cursors won't change on appview swap | ||
const { data: res } = | ||
await ctx.searchAgent.app.bsky.unspecced.searchStarterPacksSkeleton({ | ||
q, | ||
cursor: params.cursor, | ||
limit: params.limit, | ||
viewer: params.hydrateCtx.viewer ?? undefined, | ||
}) | ||
return { | ||
uris: res.starterPacks.map(({ uri }) => uri), | ||
cursor: parseString(res.cursor), | ||
} | ||
} | ||
|
||
const res = await ctx.dataplane.searchStarterPacks({ | ||
term: q, | ||
limit: params.limit, | ||
cursor: params.cursor, | ||
}) | ||
return { | ||
uris: res.uris, | ||
cursor: parseString(res.cursor), | ||
} | ||
} | ||
|
||
const hydration = async ( | ||
inputs: HydrationFnInput<Context, Params, Skeleton>, | ||
) => { | ||
const { ctx, params, skeleton } = inputs | ||
return ctx.hydrator.hydrateStarterPacks(skeleton.uris, params.hydrateCtx) | ||
} | ||
|
||
const noBlocks = (inputs: RulesFnInput<Context, Params, Skeleton>) => { | ||
const { ctx, skeleton, hydration } = inputs | ||
skeleton.uris = skeleton.uris.filter( | ||
(uri) => !ctx.views.viewerBlockExists(new AtUri(uri).hostname, hydration), | ||
) | ||
return skeleton | ||
} | ||
|
||
const presentation = ( | ||
inputs: PresentationFnInput<Context, Params, Skeleton>, | ||
) => { | ||
const { ctx, skeleton, hydration } = inputs | ||
const starterPacks = mapDefined(skeleton.uris, (uri) => | ||
ctx.views.starterPackBasic(uri, hydration), | ||
) | ||
return { | ||
starterPacks: starterPacks, | ||
cursor: skeleton.cursor, | ||
} | ||
} | ||
|
||
type Context = { | ||
dataplane: DataPlaneClient | ||
hydrator: Hydrator | ||
views: Views | ||
searchAgent?: AtpAgent | ||
} | ||
|
||
type Params = QueryParams & { hydrateCtx: HydrateCtx } | ||
|
||
type Skeleton = { | ||
uris: string[] | ||
cursor?: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters