|
| 1 | +import { sql } from 'kysely' |
| 2 | +import { Server } from '../../../../lexicon' |
| 3 | +import AppContext from '../../../../context' |
| 4 | +import { InvalidRequestError } from '@atproto/xrpc-server' |
| 5 | +import { Database } from '../../../../db' |
| 6 | +import { ActorService } from '../../../../services/actor' |
| 7 | + |
| 8 | +const RESULT_LENGTH = 10 |
| 9 | + |
| 10 | +export default function (server: Server, ctx: AppContext) { |
| 11 | + server.app.bsky.graph.getSuggestedFollowsByActor({ |
| 12 | + auth: ctx.authVerifier, |
| 13 | + handler: async ({ auth, params }) => { |
| 14 | + const { actor } = params |
| 15 | + const viewer = auth.credentials.did |
| 16 | + |
| 17 | + const db = ctx.db.getReplica() |
| 18 | + const actorService = ctx.services.actor(db) |
| 19 | + const actorDid = await actorService.getActorDid(actor) |
| 20 | + |
| 21 | + if (!actorDid) { |
| 22 | + throw new InvalidRequestError('Actor not found') |
| 23 | + } |
| 24 | + |
| 25 | + const skeleton = await getSkeleton( |
| 26 | + { |
| 27 | + actor: actorDid, |
| 28 | + viewer, |
| 29 | + }, |
| 30 | + { |
| 31 | + db, |
| 32 | + actorService, |
| 33 | + }, |
| 34 | + ) |
| 35 | + const hydrationState = await actorService.views.profileDetailHydration( |
| 36 | + skeleton.map((a) => a.did), |
| 37 | + { viewer }, |
| 38 | + ) |
| 39 | + const presentationState = actorService.views.profileDetailPresentation( |
| 40 | + skeleton.map((a) => a.did), |
| 41 | + hydrationState, |
| 42 | + { viewer }, |
| 43 | + ) |
| 44 | + const suggestions = Object.values(presentationState).filter((profile) => { |
| 45 | + return ( |
| 46 | + !profile.viewer?.muted && |
| 47 | + !profile.viewer?.mutedByList && |
| 48 | + !profile.viewer?.blocking && |
| 49 | + !profile.viewer?.blockedBy |
| 50 | + ) |
| 51 | + }) |
| 52 | + |
| 53 | + return { |
| 54 | + encoding: 'application/json', |
| 55 | + body: { suggestions }, |
| 56 | + } |
| 57 | + }, |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +async function getSkeleton( |
| 62 | + params: { |
| 63 | + actor: string |
| 64 | + viewer: string |
| 65 | + }, |
| 66 | + ctx: { |
| 67 | + db: Database |
| 68 | + actorService: ActorService |
| 69 | + }, |
| 70 | +): Promise<{ did: string }[]> { |
| 71 | + const actorsViewerFollows = ctx.db.db |
| 72 | + .selectFrom('follow') |
| 73 | + .where('creator', '=', params.viewer) |
| 74 | + .select('subjectDid') |
| 75 | + const mostLikedAccounts = await ctx.db.db |
| 76 | + .selectFrom( |
| 77 | + ctx.db.db |
| 78 | + .selectFrom('like') |
| 79 | + .where('creator', '=', params.actor) |
| 80 | + .select(sql`split_part(subject, '/', 3)`.as('subjectDid')) |
| 81 | + .limit(1000) // limit to 1000 |
| 82 | + .as('likes'), |
| 83 | + ) |
| 84 | + .select('likes.subjectDid as did') |
| 85 | + .select((qb) => qb.fn.count('likes.subjectDid').as('count')) |
| 86 | + .where('likes.subjectDid', 'not in', actorsViewerFollows) |
| 87 | + .where('likes.subjectDid', 'not in', [params.actor, params.viewer]) |
| 88 | + .groupBy('likes.subjectDid') |
| 89 | + .orderBy('count', 'desc') |
| 90 | + .limit(RESULT_LENGTH) |
| 91 | + .execute() |
| 92 | + const resultDids = mostLikedAccounts.map((a) => ({ did: a.did })) as { |
| 93 | + did: string |
| 94 | + }[] |
| 95 | + |
| 96 | + if (resultDids.length < RESULT_LENGTH) { |
| 97 | + // backfill with popular accounts followed by actor |
| 98 | + const mostPopularAccountsActorFollows = await ctx.db.db |
| 99 | + .selectFrom('follow') |
| 100 | + .innerJoin('profile_agg', 'follow.subjectDid', 'profile_agg.did') |
| 101 | + .select('follow.subjectDid as did') |
| 102 | + .where('follow.creator', '=', params.actor) |
| 103 | + .where('follow.subjectDid', '!=', params.viewer) |
| 104 | + .where('follow.subjectDid', 'not in', actorsViewerFollows) |
| 105 | + .if(resultDids.length > 0, (qb) => |
| 106 | + qb.where( |
| 107 | + 'subjectDid', |
| 108 | + 'not in', |
| 109 | + resultDids.map((a) => a.did), |
| 110 | + ), |
| 111 | + ) |
| 112 | + .orderBy('profile_agg.followersCount', 'desc') |
| 113 | + .limit(RESULT_LENGTH) |
| 114 | + .execute() |
| 115 | + |
| 116 | + resultDids.push(...mostPopularAccountsActorFollows) |
| 117 | + } |
| 118 | + |
| 119 | + if (resultDids.length < RESULT_LENGTH) { |
| 120 | + // backfill with suggested_follow table |
| 121 | + const additional = await ctx.db.db |
| 122 | + .selectFrom('suggested_follow') |
| 123 | + .where( |
| 124 | + 'did', |
| 125 | + 'not in', |
| 126 | + // exclude any we already have |
| 127 | + resultDids.map((a) => a.did).concat([params.actor, params.viewer]), |
| 128 | + ) |
| 129 | + // and aren't already followed by viewer |
| 130 | + .where('did', 'not in', actorsViewerFollows) |
| 131 | + .selectAll() |
| 132 | + .execute() |
| 133 | + |
| 134 | + resultDids.push(...additional) |
| 135 | + } |
| 136 | + |
| 137 | + return resultDids |
| 138 | +} |
0 commit comments