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 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
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
}
132 changes: 132 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,132 @@
import { sql } from 'kysely';
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 actorsViewerFollows = db.db
.selectFrom('follow')
.where('creator', '=', viewer)
.select('subjectDid')

/**
* 20 most liked accounts that aren't already followed by the viewer, ARE
* the viewer, or are the actor
*/
const mostLikedAccounts = db.db
.selectFrom(
db.db
.selectFrom('like')
.where('creator', '=', actorDid)
.select(sql`split_part(subject, '/', 3)`.as('subjectDid'))
estrattonbailey marked this conversation as resolved.
Show resolved Hide resolved
.as('likes')
)
.select('likes.subjectDid as did')
.select(qb => qb.fn.count('likes.subjectDid').as('count'))
.where('likes.subjectDid', 'not in', actorsViewerFollows)
.where('likes.subjectDid', 'not in', [actorDid, viewer])
estrattonbailey marked this conversation as resolved.
Show resolved Hide resolved
.groupBy('likes.subjectDid')
.orderBy('count', 'desc')
.limit(20)

/**
* 20 most liked accounts as actor results
estrattonbailey marked this conversation as resolved.
Show resolved Hide resolved
*/
const actors = await db.db
.selectFrom('actor')
.selectAll()
.innerJoin(mostLikedAccounts.as('liked'), 'actor.did', 'liked.did')
.orderBy('liked.count', 'desc')
.execute() // TODO should return max 20 right?

if (actors.length < MAX_RESULTS_LENGTH) {
// backfill with popular accounts followed by actor
const actorsActorFollows = db.db
.selectFrom('follow')
.selectAll()
.where('creator', '=', actorDid)
.where('subjectDid', '!=', viewer)
.where('subjectDid', 'not in', actorsViewerFollows)
.if(
actors.length > 0,
qb => qb.where('subjectDid', 'not in', actors.map((a) => a.did))
)
const mostPopularAccountsActorFollows = db.db
.selectFrom('profile_agg')
.select(['did', 'followersCount'])
.innerJoin(
estrattonbailey marked this conversation as resolved.
Show resolved Hide resolved
actorsActorFollows.as('follows'),
'follows.subjectDid',
'profile_agg.did',
)
.orderBy('followersCount', 'desc')
.limit(20)
const mostPopularActors = await db.db
.selectFrom('actor')
.selectAll()
.innerJoin(
mostPopularAccountsActorFollows.as('popularFollows'),
estrattonbailey marked this conversation as resolved.
Show resolved Hide resolved
'actor.did',
'popularFollows.did',
)
.orderBy('popularFollows.followersCount', 'desc')
.execute()

actors.push(...mostPopularActors)
}

if (actors.length < MAX_RESULTS_LENGTH) {
// backfill with suggested_follow table
const additional = await db.db
.selectFrom('actor')
.innerJoin('suggested_follow', 'actor.did', 'suggested_follow.did')
.where(
'actor.did',
'not in',
// exclude any we already have
actors.map((a) => a.did).concat([actorDid, viewer]),
)
// and aren't already followed by viewer
.where('actor.did', 'not in', actorsViewerFollows)
.selectAll()
.execute()

actors.push(...additional)
}

// resolve all profiles, this handles blocks/mutes etc
const suggestions = (
await actorService.views.hydrateProfiles(actors, viewer)
).filter((account) => {
estrattonbailey marked this conversation as resolved.
Show resolved Hide resolved
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