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

add getSuggestedFollowsByActor #1553

Merged
merged 20 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
33 changes: 33 additions & 0 deletions lexicons/app/bsky/graph/getSuggestedFollowsByActor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"lexicon": 1,
"id": "app.bsky.graph.getSuggestedFollowsByActor",
"defs": {
"main": {
"type": "query",
"description": "Get suggested follows related to a given actor.",
"parameters": {
"type": "params",
"required": ["actor"],
"properties": {
"actor": { "type": "string", "format": "at-identifier" }
}
},
"output": {
"encoding": "application/json",
"schema": {
"type": "object",
"required": ["suggestions"],
"properties": {
"suggestions": {
"type": "array",
"items": {
"type": "ref",
"ref": "app.bsky.actor.defs#profileView"
}
}
}
}
}
}
}
}
18 changes: 18 additions & 0 deletions packages/api/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ import * as AppBskyGraphGetList from './types/app/bsky/graph/getList'
import * as AppBskyGraphGetListMutes from './types/app/bsky/graph/getListMutes'
import * as AppBskyGraphGetLists from './types/app/bsky/graph/getLists'
import * as AppBskyGraphGetMutes from './types/app/bsky/graph/getMutes'
import * as AppBskyGraphGetSuggestedFollowsByActor from './types/app/bsky/graph/getSuggestedFollowsByActor'
import * as AppBskyGraphList from './types/app/bsky/graph/list'
import * as AppBskyGraphListitem from './types/app/bsky/graph/listitem'
import * as AppBskyGraphMuteActor from './types/app/bsky/graph/muteActor'
Expand Down Expand Up @@ -235,6 +236,7 @@ export * as AppBskyGraphGetList from './types/app/bsky/graph/getList'
export * as AppBskyGraphGetListMutes from './types/app/bsky/graph/getListMutes'
export * as AppBskyGraphGetLists from './types/app/bsky/graph/getLists'
export * as AppBskyGraphGetMutes from './types/app/bsky/graph/getMutes'
export * as AppBskyGraphGetSuggestedFollowsByActor from './types/app/bsky/graph/getSuggestedFollowsByActor'
export * as AppBskyGraphList from './types/app/bsky/graph/list'
export * as AppBskyGraphListitem from './types/app/bsky/graph/listitem'
export * as AppBskyGraphMuteActor from './types/app/bsky/graph/muteActor'
Expand Down Expand Up @@ -1718,6 +1720,22 @@ export class GraphNS {
})
}

getSuggestedFollowsByActor(
params?: AppBskyGraphGetSuggestedFollowsByActor.QueryParams,
opts?: AppBskyGraphGetSuggestedFollowsByActor.CallOptions,
): Promise<AppBskyGraphGetSuggestedFollowsByActor.Response> {
return this._service.xrpc
.call(
'app.bsky.graph.getSuggestedFollowsByActor',
params,
undefined,
opts,
)
.catch((e) => {
throw AppBskyGraphGetSuggestedFollowsByActor.toKnownErr(e)
})
}

muteActor(
data?: AppBskyGraphMuteActor.InputSchema,
opts?: AppBskyGraphMuteActor.CallOptions,
Expand Down
38 changes: 38 additions & 0 deletions packages/api/src/client/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6079,6 +6079,42 @@ export const schemaDict = {
},
},
},
AppBskyGraphGetSuggestedFollowsByActor: {
lexicon: 1,
id: 'app.bsky.graph.getSuggestedFollowsByActor',
defs: {
main: {
type: 'query',
description: 'Get suggested follows related to a given actor.',
parameters: {
type: 'params',
required: ['actor'],
properties: {
actor: {
type: 'string',
format: 'at-identifier',
},
},
},
output: {
encoding: 'application/json',
schema: {
type: 'object',
required: ['suggestions'],
properties: {
suggestions: {
type: 'array',
items: {
type: 'ref',
ref: 'lex:app.bsky.actor.defs#profileView',
},
},
},
},
},
},
},
},
AppBskyGraphList: {
lexicon: 1,
id: 'app.bsky.graph.list',
Expand Down Expand Up @@ -6790,6 +6826,8 @@ export const ids = {
AppBskyGraphGetListMutes: 'app.bsky.graph.getListMutes',
AppBskyGraphGetLists: 'app.bsky.graph.getLists',
AppBskyGraphGetMutes: 'app.bsky.graph.getMutes',
AppBskyGraphGetSuggestedFollowsByActor:
'app.bsky.graph.getSuggestedFollowsByActor',
AppBskyGraphList: 'app.bsky.graph.list',
AppBskyGraphListitem: 'app.bsky.graph.listitem',
AppBskyGraphMuteActor: 'app.bsky.graph.muteActor',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* GENERATED CODE - DO NOT MODIFY
*/
import { Headers, XRPCError } from '@atproto/xrpc'
import { ValidationResult, BlobRef } from '@atproto/lexicon'
import { isObj, hasProp } from '../../../../util'
import { lexicons } from '../../../../lexicons'
import { CID } from 'multiformats/cid'
import * as AppBskyActorDefs from '../actor/defs'

export interface QueryParams {
actor: string
}

export type InputSchema = undefined

export interface OutputSchema {
suggestions: AppBskyActorDefs.ProfileView[]
[k: string]: unknown
}

export interface CallOptions {
headers?: Headers
}

export interface Response {
success: boolean
headers: Headers
data: OutputSchema
}

export function toKnownErr(e: any) {
if (e instanceof XRPCError) {
}
return e
}
122 changes: 122 additions & 0 deletions packages/bsky/src/api/app/bsky/graph/getSuggestedFollowsByActor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { Server } from '../../../../lexicon'
import AppContext from '../../../../context'
import { InvalidRequestError } from '@atproto/xrpc-server'

const MAX_RESULTS_LENGTH = 10

export default function (server: Server, ctx: AppContext) {
server.app.bsky.graph.getSuggestedFollowsByActor({
auth: ctx.authVerifier,
handler: async ({ auth, params }) => {
const { actor } = params
const viewer = auth.credentials.did

const db = ctx.db.getReplica()
const actorService = ctx.services.actor(db)
const actorDid = await actorService.getActorDid(actor)

if (!actorDid) {
throw new InvalidRequestError('Actor not found')
}

const likes = await db.db
estrattonbailey marked this conversation as resolved.
Show resolved Hide resolved
.selectFrom('like')
.select(['subject', 'creator'])
.where('creator', '=', actorDid)
.limit(1000)
.execute()

let suggestions: Awaited<
ReturnType<typeof actorService.views.hydrateProfiles>
> = []

if (likes.length >= 100) {
// get posts to get their authors
const posts = await db.db
.selectFrom('post')
.where(
'post.uri',
'in',
likes.map((l) => l.subject),
)
.select(['creator', 'uri'])
.execute()

const authorDIDs = Object.values(posts).map((p) => p.creator)
const authorDIDsExcludingActorAndViewer = authorDIDs.filter(
(did) => did !== actorDid && did !== viewer,
)

const authorsMappedByMostCommon =
authorDIDsExcludingActorAndViewer.reduce((acc, did) => {
acc[did] = (acc[did] || 0) + 1
return acc
}, {} as Record<string, number>)
const authorsSortedByMostCommon = Object.entries(
authorsMappedByMostCommon,
)
.sort((a, b) => b[1] - a[1])
.slice(0, 20) // take top 20 most common

// get the profiles of the authors
const authors = await db.db
.selectFrom('actor')
.where(
'actor.did',
'in',
authorsSortedByMostCommon.map((a) => a[0]),
)
.selectAll()
.execute()
const sortedAuthors = authorsSortedByMostCommon
.map(([did]) => authors.find((a) => a.did === did))
.filter(Boolean) as typeof authors

const actors = sortedAuthors

if (suggestions.length < MAX_RESULTS_LENGTH) {
// backfill with suggested_follow table
const additional = await db.db
.selectFrom('suggested_follow')
.select('did')
.where(
'suggested_follow.did',
'not in',
// exclude any we already have
authorDIDsExcludingActorAndViewer.concat([actorDid, viewer]),
)
.execute()
const additionalActors = await db.db
estrattonbailey marked this conversation as resolved.
Show resolved Hide resolved
.selectFrom('actor')
.where(
'actor.did',
'in',
additional.map((a) => a.did),
)
.selectAll()
.execute()

actors.push(...additionalActors)
}

// this handles blocks/mutes etc
suggestions = (
await actorService.views.hydrateProfiles(actors, viewer)
estrattonbailey marked this conversation as resolved.
Show resolved Hide resolved
).filter((account) => {
return (
!account.viewer?.muted &&
!account.viewer?.blocking &&
!account.viewer?.blockedBy
)
})
}

return {
encoding: 'application/json',
body: {
suggestions: suggestions.slice(0, MAX_RESULTS_LENGTH),
},
}
},
})
}
2 changes: 2 additions & 0 deletions packages/bsky/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import muteActor from './app/bsky/graph/muteActor'
import unmuteActor from './app/bsky/graph/unmuteActor'
import muteActorList from './app/bsky/graph/muteActorList'
import unmuteActorList from './app/bsky/graph/unmuteActorList'
import getSuggestedFollowsByActor from './app/bsky/graph/getSuggestedFollowsByActor'
import searchActors from './app/bsky/actor/searchActors'
import searchActorsTypeahead from './app/bsky/actor/searchActorsTypeahead'
import getSuggestions from './app/bsky/actor/getSuggestions'
Expand Down Expand Up @@ -85,6 +86,7 @@ export default function (server: Server, ctx: AppContext) {
unmuteActor(server, ctx)
muteActorList(server, ctx)
unmuteActorList(server, ctx)
getSuggestedFollowsByActor(server, ctx)
searchActors(server, ctx)
searchActorsTypeahead(server, ctx)
getSuggestions(server, ctx)
Expand Down
12 changes: 12 additions & 0 deletions packages/bsky/src/lexicon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ import * as AppBskyGraphGetList from './types/app/bsky/graph/getList'
import * as AppBskyGraphGetListMutes from './types/app/bsky/graph/getListMutes'
import * as AppBskyGraphGetLists from './types/app/bsky/graph/getLists'
import * as AppBskyGraphGetMutes from './types/app/bsky/graph/getMutes'
import * as AppBskyGraphGetSuggestedFollowsByActor from './types/app/bsky/graph/getSuggestedFollowsByActor'
import * as AppBskyGraphMuteActor from './types/app/bsky/graph/muteActor'
import * as AppBskyGraphMuteActorList from './types/app/bsky/graph/muteActorList'
import * as AppBskyGraphUnmuteActor from './types/app/bsky/graph/unmuteActor'
Expand Down Expand Up @@ -1261,6 +1262,17 @@ export class GraphNS {
return this._server.xrpc.method(nsid, cfg)
}

getSuggestedFollowsByActor<AV extends AuthVerifier>(
cfg: ConfigOf<
AV,
AppBskyGraphGetSuggestedFollowsByActor.Handler<ExtractAuth<AV>>,
AppBskyGraphGetSuggestedFollowsByActor.HandlerReqCtx<ExtractAuth<AV>>
>,
) {
const nsid = 'app.bsky.graph.getSuggestedFollowsByActor' // @ts-ignore
return this._server.xrpc.method(nsid, cfg)
}

muteActor<AV extends AuthVerifier>(
cfg: ConfigOf<
AV,
Expand Down
38 changes: 38 additions & 0 deletions packages/bsky/src/lexicon/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6079,6 +6079,42 @@ export const schemaDict = {
},
},
},
AppBskyGraphGetSuggestedFollowsByActor: {
lexicon: 1,
id: 'app.bsky.graph.getSuggestedFollowsByActor',
defs: {
main: {
type: 'query',
description: 'Get suggested follows related to a given actor.',
parameters: {
type: 'params',
required: ['actor'],
properties: {
actor: {
type: 'string',
format: 'at-identifier',
},
},
},
output: {
encoding: 'application/json',
schema: {
type: 'object',
required: ['suggestions'],
properties: {
suggestions: {
type: 'array',
items: {
type: 'ref',
ref: 'lex:app.bsky.actor.defs#profileView',
},
},
},
},
},
},
},
},
AppBskyGraphList: {
lexicon: 1,
id: 'app.bsky.graph.list',
Expand Down Expand Up @@ -6790,6 +6826,8 @@ export const ids = {
AppBskyGraphGetListMutes: 'app.bsky.graph.getListMutes',
AppBskyGraphGetLists: 'app.bsky.graph.getLists',
AppBskyGraphGetMutes: 'app.bsky.graph.getMutes',
AppBskyGraphGetSuggestedFollowsByActor:
'app.bsky.graph.getSuggestedFollowsByActor',
AppBskyGraphList: 'app.bsky.graph.list',
AppBskyGraphListitem: 'app.bsky.graph.listitem',
AppBskyGraphMuteActor: 'app.bsky.graph.muteActor',
Expand Down
Loading