Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155import { type $Typed, type AppBskyGraphFollow, type AppBskyGraphGetFollows, type AtpAgent, type ComAtprotoRepoApplyWrites, type ComAtprotoRepoStrongRef,} from '@atproto/api'import {TID} from '@atproto/common-web'import chunk from 'lodash.chunk'
import {until} from '#/lib/async/until'import {type ComputedBrandConfig} from '#/lib/community/types'import {logger} from '#/logger'import {saveLabelers} from '#/state/session/agent-config'
export async function bulkWriteFollows( agent: AtpAgent, dids: string[], via?: ComAtprotoRepoStrongRef.Main,) { const session = agent.session
if (!session) { throw new Error(`bulkWriteFollows failed: no session`) }
const followRecords: $Typed<AppBskyGraphFollow.Record>[] = dids.map(did => { return { $type: 'app.bsky.graph.follow', subject: did, createdAt: new Date().toISOString(), via, } })
const followWrites: $Typed<ComAtprotoRepoApplyWrites.Create>[] = followRecords.map(r => ({ $type: 'com.atproto.repo.applyWrites#create', collection: 'app.bsky.graph.follow', rkey: TID.nextStr(), value: r, }))
const chunks = chunk(followWrites, 50) for (const chunk of chunks) { await agent.com.atproto.repo.applyWrites({ repo: session.did, writes: chunk, }) } await whenFollowsIndexed(agent, session.did, res => !!res.data.follows.length)
const followUris = new Map<string, string>() for (const r of followWrites) { followUris.set( r.value.subject as string, `at://${session.did}/app.bsky.graph.follow/${r.rkey}`, ) } return followUris}
/** * Resolves which starter pack URI to use during onboarding. * Prefers the active starter pack (from a link click) over the community config default. */export function resolveStarterPackUri( activeStarterPackUri: string | undefined, brandConfig: ComputedBrandConfig,): string | undefined { return activeStarterPackUri || brandConfig.onboarding.starterPack || undefined}
/** * Builds the deduplicated list of DIDs to auto-follow during onboarding. * Merges hardcoded community DIDs, brand config auto-follow DIDs, * and starter pack list member DIDs. */export function resolveFollowDids( hardcodedDids: string[], brandConfig: ComputedBrandConfig, starterPackMemberDids: string[],): string[] { return [ ...new Set([ ...hardcodedDids, ...brandConfig.onboarding.autoFollowDids, ...starterPackMemberDids, ]), ]}
export function resolveModerationServiceDids( brandConfig: ComputedBrandConfig,): string[] { return [...new Set((brandConfig.services.moderation || []).filter(Boolean))]}
export async function subscribeToBrandModerationServices( agent: AtpAgent, accountDid: string | undefined, brandConfig: ComputedBrandConfig,) { const dids = resolveModerationServiceDids(brandConfig) if (!dids.length) return
const results = await Promise.allSettled( dids.map(async did => { await agent.addLabeler(did) return did }), )
const subscribedDids = results .map((result, i) => { if (result.status === 'fulfilled') { return result.value } logger.error('Failed to subscribe to brand moderation service', { did: dids[i], safeMessage: result.reason, }) }) .filter((did): did is string => !!did)
if (!accountDid || !subscribedDids.length) return
try { await saveLabelers( accountDid, agent.labelers.length ? [...agent.labelers] : subscribedDids, ) } catch (e) { logger.error('Failed to cache brand moderation services', {safeMessage: e}) }}
async function whenFollowsIndexed( agent: AtpAgent, actor: string, fn: (res: AppBskyGraphGetFollows.Response) => boolean,) { await until( 5, // 5 tries 1e3, // 1s delay between tries fn, () => agent.app.bsky.graph.getFollows({ actor, limit: 1, }), )}