Skip to content

Commit

Permalink
Suggestions skeleton impl (#2403)
Browse files Browse the repository at this point in the history
* schema

* codegen

* add skeletonActor def

* add optional viewer param

* codegen

* use suggestions skelete for getSuggestions

* make move route to unspecced

* use unspecced skeleton actor

* update agent call
  • Loading branch information
dholms authored Apr 16, 2024
1 parent 45981de commit cc090d2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
41 changes: 28 additions & 13 deletions packages/bsky/src/api/app/bsky/actor/getSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Views } from '../../../../views'
import { DataPlaneClient } from '../../../../data-plane'
import { parseString } from '../../../../hydration/util'
import { resHeaders } from '../../../util'
import AtpAgent from '@atproto/api'

export default function (server: Server, ctx: AppContext) {
const getSuggestions = createPipeline(
Expand Down Expand Up @@ -43,21 +44,34 @@ const skeleton = async (input: {
}): Promise<Skeleton> => {
const { ctx, params } = input
const viewer = params.hydrateCtx.viewer
// @NOTE for appview swap moving to rkey-based cursors which are somewhat permissive, should not hard-break pagination
const suggestions = await ctx.dataplane.getFollowSuggestions({
actorDid: viewer ?? undefined,
cursor: params.cursor,
limit: params.limit,
})
let dids = suggestions.dids
if (viewer !== null) {
const follows = await ctx.dataplane.getActorFollowsActors({
actorDid: viewer,
targetDids: dids,
if (ctx.suggestionsAgent) {
const res =
await ctx.suggestionsAgent.api.app.bsky.unspecced.getSuggestionsSkeleton({
viewer: viewer ?? undefined,
limit: params.limit,
cursor: params.cursor,
})
return {
dids: res.data.actors.map((a) => a.did),
cursor: res.data.cursor,
}
} else {
// @NOTE for appview swap moving to rkey-based cursors which are somewhat permissive, should not hard-break pagination
const suggestions = await ctx.dataplane.getFollowSuggestions({
actorDid: viewer ?? undefined,
cursor: params.cursor,
limit: params.limit,
})
dids = dids.filter((did, i) => !follows.uris[i] && did !== viewer)
let dids = suggestions.dids
if (viewer !== null) {
const follows = await ctx.dataplane.getActorFollowsActors({
actorDid: viewer,
targetDids: dids,
})
dids = dids.filter((did, i) => !follows.uris[i] && did !== viewer)
}
return { dids, cursor: parseString(suggestions.cursor) }
}
return { dids, cursor: parseString(suggestions.cursor) }
}

const hydration = async (input: {
Expand Down Expand Up @@ -101,6 +115,7 @@ const presentation = (input: {
}

type Context = {
suggestionsAgent: AtpAgent | undefined
dataplane: DataPlaneClient
hydrator: Hydrator
views: Views
Expand Down
14 changes: 14 additions & 0 deletions packages/bsky/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface ServerConfigValues {
courierHttpVersion?: '1.1' | '2'
courierIgnoreBadTls?: boolean
searchUrl?: string
suggestionsUrl?: string
suggestionsApiKey?: string
cdnUrl?: string
blobRateLimitBypassKey?: string
blobRateLimitBypassHostname?: string
Expand Down Expand Up @@ -55,6 +57,8 @@ export class ServerConfig {
process.env.BSKY_SEARCH_URL ||
process.env.BSKY_SEARCH_ENDPOINT ||
undefined
const suggestionsUrl = process.env.BSKY_SUGGESTIONS_URL || undefined
const suggestionsApiKey = process.env.BSKY_SUGGESTIONS_API_KEY || undefined
let dataplaneUrls = overrides?.dataplaneUrls
dataplaneUrls ??= process.env.BSKY_DATAPLANE_URLS
? process.env.BSKY_DATAPLANE_URLS.split(',')
Expand Down Expand Up @@ -104,6 +108,8 @@ export class ServerConfig {
dataplaneHttpVersion,
dataplaneIgnoreBadTls,
searchUrl,
suggestionsUrl,
suggestionsApiKey,
didPlcUrl,
labelsFromIssuerDids,
handleResolveNameservers,
Expand Down Expand Up @@ -206,6 +212,14 @@ export class ServerConfig {
return this.cfg.searchUrl
}

get suggestionsUrl() {
return this.cfg.suggestionsUrl
}

get suggestionsApiKey() {
return this.cfg.suggestionsApiKey
}

get cdnUrl() {
return this.cfg.cdnUrl
}
Expand Down
5 changes: 5 additions & 0 deletions packages/bsky/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class AppContext {
cfg: ServerConfig
dataplane: DataPlaneClient
searchAgent: AtpAgent | undefined
suggestionsAgent: AtpAgent | undefined
hydrator: Hydrator
views: Views
signingKey: Keypair
Expand All @@ -46,6 +47,10 @@ export class AppContext {
return this.opts.searchAgent
}

get suggestionsAgent(): AtpAgent | undefined {
return this.opts.suggestionsAgent
}

get hydrator(): Hydrator {
return this.opts.hydrator
}
Expand Down
12 changes: 12 additions & 0 deletions packages/bsky/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ export class BskyAppView {
const searchAgent = config.searchUrl
? new AtpAgent({ service: config.searchUrl })
: undefined

const suggestionsAgent = config.suggestionsUrl
? new AtpAgent({ service: config.suggestionsUrl })
: undefined
if (suggestionsAgent && config.suggestionsApiKey) {
suggestionsAgent.api.setHeader(
'authorization',
`Bearer ${config.suggestionsApiKey}`,
)
}

const dataplane = createDataPlaneClient(config.dataplaneUrls, {
httpVersion: config.dataplaneHttpVersion,
rejectUnauthorized: !config.dataplaneIgnoreBadTls,
Expand Down Expand Up @@ -107,6 +118,7 @@ export class BskyAppView {
cfg: config,
dataplane,
searchAgent,
suggestionsAgent,
hydrator,
views,
signingKey,
Expand Down

0 comments on commit cc090d2

Please sign in to comment.