Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update data source for getSuggestedFollowsByActor #2630

Merged
merged 20 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lexicons/app/bsky/unspecced/getSuggestionsSkeleton.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
"maximum": 100,
"default": 50
},
"cursor": { "type": "string" }
"cursor": { "type": "string" },
"relative_to_did": {
estrattonbailey marked this conversation as resolved.
Show resolved Hide resolved
"type": "string",
"format": "did",
"description": "DID of the account to get suggestions relative to. If not provided, suggestions will be based on the viewer."
}
}
},
"output": {
Expand Down
6 changes: 6 additions & 0 deletions packages/api/src/client/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8712,6 +8712,12 @@ export const schemaDict = {
cursor: {
type: 'string',
},
relative_to_did: {
type: 'string',
format: 'did',
description:
'DID of the account to get suggestions relative to. If not provided, suggestions will be based on the viewer.',
},
},
},
output: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export interface QueryParams {
viewer?: string
limit?: number
cursor?: string
/** DID of the account to get suggestions relative to. If not provided, suggestions will be based on the viewer. */
relative_to_did?: string
}

export type InputSchema = undefined
Expand Down
1 change: 1 addition & 0 deletions packages/bsky/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"pino": "^8.21.0",
"pino-http": "^8.2.1",
"sharp": "^0.32.6",
"statsig-node": "^5.23.1",
"structured-headers": "^1.0.1",
"typed-emitter": "^2.1.0",
"uint8arrays": "3.0.0"
Expand Down
68 changes: 53 additions & 15 deletions packages/bsky/src/api/app/bsky/graph/getSuggestedFollowsByActor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { mapDefined } from '@atproto/common'
import { mapDefined, noUndefinedVals } from '@atproto/common'
import { InvalidRequestError } from '@atproto/xrpc-server'
import { Statsig } from 'statsig-node'
import AtpAgent from '@atproto/api'
import { Server } from '../../../../lexicon'
import { QueryParams } from '../../../../lexicon/types/app/bsky/graph/getSuggestedFollowsByActor'
import AppContext from '../../../../context'
Expand All @@ -13,6 +15,7 @@ import {
import { HydrateCtx, Hydrator } from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
import { resHeaders } from '../../../util'
import { didToStatsigUser, gates } from '../../../../util/statsig'

export default function (server: Server, ctx: AppContext) {
const getSuggestedFollowsByActor = createPipeline(
Expand All @@ -27,14 +30,27 @@ export default function (server: Server, ctx: AppContext) {
const viewer = auth.credentials.iss
const labelers = ctx.reqLabelers(req)
const hydrateCtx = await ctx.hydrator.createContext({ labelers, viewer })
const result = await getSuggestedFollowsByActor(
{ ...params, hydrateCtx: hydrateCtx.copy({ viewer }) },
ctx,
)
const headers = noUndefinedVals({
'accept-language': req.headers['accept-language'],
'x-bsky-topics': Array.isArray(req.headers['x-bsky-topics'])
? req.headers['x-bsky-topics'].join(',')
: req.headers['x-bsky-topics'],
})
const { headers: resultHeaders, ...result } =
await getSuggestedFollowsByActor(
{ ...params, hydrateCtx: hydrateCtx.copy({ viewer }), headers },
ctx,
)
const responseHeaders = noUndefinedVals({
'content-language': resultHeaders?.['content-language'],
})
return {
encoding: 'application/json',
body: result,
headers: resHeaders({ labelers: hydrateCtx.labelers }),
headers: {
...responseHeaders,
...resHeaders({ labelers: hydrateCtx.labelers }),
},
}
estrattonbailey marked this conversation as resolved.
Show resolved Hide resolved
},
})
Expand All @@ -46,13 +62,32 @@ const skeleton = async (input: SkeletonFnInput<Context, Params>) => {
if (!relativeToDid) {
throw new InvalidRequestError('Actor not found')
}
const { dids, cursor } = await ctx.hydrator.dataplane.getFollowSuggestions({
actorDid: params.hydrateCtx.viewer,
relativeToDid,
})
return {
suggestedDids: dids,
cursor: cursor || undefined,

const statsigUser = await didToStatsigUser(params.hydrateCtx.viewer)
if (
ctx.suggestionsAgent &&
Statsig.checkGateSync(statsigUser, gates.newSuggestedFollowsByActor)
) {
estrattonbailey marked this conversation as resolved.
Show resolved Hide resolved
const res =
await ctx.suggestionsAgent.api.app.bsky.unspecced.getSuggestionsSkeleton(
{
viewer: params.hydrateCtx.viewer ?? undefined,
relative_to_did: relativeToDid,
},
{ headers: params.headers },
)
return {
suggestedDids: res.data.actors.map((a) => a.did),
headers: res.headers,
}
} else {
const { dids } = await ctx.hydrator.dataplane.getFollowSuggestions({
actorDid: params.hydrateCtx.viewer,
relativeToDid,
})
return {
suggestedDids: dids,
}
}
}

Expand Down Expand Up @@ -80,22 +115,25 @@ const presentation = (
input: PresentationFnInput<Context, Params, SkeletonState>,
) => {
const { ctx, hydration, skeleton } = input
const { suggestedDids } = skeleton
const { suggestedDids, headers } = skeleton
const suggestions = mapDefined(suggestedDids, (did) =>
ctx.views.profileDetailed(did, hydration),
)
return { suggestions }
return { suggestions, headers }
}

type Context = {
hydrator: Hydrator
views: Views
suggestionsAgent: AtpAgent | undefined
}

type Params = QueryParams & {
hydrateCtx: HydrateCtx & { viewer: string }
headers: Record<string, string>
}

type SkeletonState = {
suggestedDids: string[]
headers?: Record<string, string>
}
14 changes: 14 additions & 0 deletions packages/bsky/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export interface ServerConfigValues {
labelsFromIssuerDids?: string[]
// misc/dev
blobCacheLocation?: string
statsigKey?: string
statsigEnv?: string
}

export class ServerConfig {
Expand Down Expand Up @@ -102,6 +104,8 @@ export class ServerConfig {
assert(modServiceDid)
assert(dataplaneUrls.length)
assert(dataplaneHttpVersion === '1.1' || dataplaneHttpVersion === '2')
const statsigKey = process.env.BSKY_STATSIG_KEY || undefined
const statsigEnv = process.env.BSKY_STATSIG_ENV || 'development'
return new ServerConfig({
version,
debugMode,
Expand Down Expand Up @@ -132,6 +136,8 @@ export class ServerConfig {
blobRateLimitBypassHostname,
adminPasswords,
modServiceDid,
statsigKey,
statsigEnv,
...stripUndefineds(overrides ?? {}),
})
}
Expand Down Expand Up @@ -264,6 +270,14 @@ export class ServerConfig {
get blobCacheLocation() {
return this.cfg.blobCacheLocation
}

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

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

function stripUndefineds(
Expand Down
7 changes: 7 additions & 0 deletions packages/bsky/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import compression from 'compression'
import AtpAgent from '@atproto/api'
import { IdResolver } from '@atproto/identity'
import { DAY, SECOND } from '@atproto/common'
import { Statsig } from 'statsig-node'
import API, { health, wellKnown, blobResolver } from './api'
import * as error from './error'
import { loggerMiddleware } from './logger'
Expand Down Expand Up @@ -116,6 +117,12 @@ export class BskyAppView {
adminPasses: config.adminPasswords,
})

if (config.statsigKey) {
Statsig.initialize(config.statsigKey, {
environment: { tier: config.statsigEnv },
}).catch((_) => {})
estrattonbailey marked this conversation as resolved.
Show resolved Hide resolved
}

const ctx = new AppContext({
cfg: config,
dataplane,
Expand Down
6 changes: 6 additions & 0 deletions packages/bsky/src/lexicon/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8712,6 +8712,12 @@ export const schemaDict = {
cursor: {
type: 'string',
},
relative_to_did: {
type: 'string',
format: 'did',
description:
'DID of the account to get suggestions relative to. If not provided, suggestions will be based on the viewer.',
},
},
},
output: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface QueryParams {
viewer?: string
limit: number
cursor?: string
/** DID of the account to get suggestions relative to. If not provided, suggestions will be based on the viewer. */
relative_to_did?: string
}

export type InputSchema = undefined
Expand Down
12 changes: 12 additions & 0 deletions packages/bsky/src/util/statsig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { sha256Hex } from '@atproto/crypto'

export async function didToStatsigUser(did: string) {
const userID = await sha256Hex(did)
return {
userID,
}
}

export const gates = {
newSuggestedFollowsByActor: 'new_sugg_foll_by_actor',
}
estrattonbailey marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions packages/crypto/src/sha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as noble from '@noble/hashes/sha256'
import * as uint8arrays from 'uint8arrays'

// takes either bytes of utf8 input
// @TODO this can be sync
export const sha256 = async (
input: Uint8Array | string,
): Promise<Uint8Array> => {
Expand All @@ -10,6 +11,7 @@ export const sha256 = async (
return noble.sha256(bytes)
}

// @TODO this can be sync
export const sha256Hex = async (
input: Uint8Array | string,
): Promise<string> => {
Expand Down
6 changes: 6 additions & 0 deletions packages/ozone/src/lexicon/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8712,6 +8712,12 @@ export const schemaDict = {
cursor: {
type: 'string',
},
relative_to_did: {
type: 'string',
format: 'did',
description:
'DID of the account to get suggestions relative to. If not provided, suggestions will be based on the viewer.',
},
},
},
output: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface QueryParams {
viewer?: string
limit: number
cursor?: string
/** DID of the account to get suggestions relative to. If not provided, suggestions will be based on the viewer. */
relative_to_did?: string
}

export type InputSchema = undefined
Expand Down
6 changes: 6 additions & 0 deletions packages/pds/src/lexicon/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8712,6 +8712,12 @@ export const schemaDict = {
cursor: {
type: 'string',
},
relative_to_did: {
type: 'string',
format: 'did',
description:
'DID of the account to get suggestions relative to. If not provided, suggestions will be based on the viewer.',
},
},
},
output: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface QueryParams {
viewer?: string
limit: number
cursor?: string
/** DID of the account to get suggestions relative to. If not provided, suggestions will be based on the viewer. */
relative_to_did?: string
}

export type InputSchema = undefined
Expand Down
Loading
Loading