import { Cid, getBlobCidString } from '@atproto/lex' import { AtUri } from '@atproto/syntax' import { app } from '../../../../lexicons/index.js' import { BackgroundQueue } from '../../background.js' import { DatabaseSchema, DatabaseSchemaType } from '../../db/database-schema.js' import { Database } from '../../db/index.js' import { RecordProcessor } from '../processor.js' type IndexedProfile = DatabaseSchemaType['profile'] const insertFn = async ( db: DatabaseSchema, uri: AtUri, cid: Cid, obj: app.bsky.actor.profile.Main, timestamp: string, ): Promise => { if (uri.rkey !== 'self') return null const inserted = await db .insertInto('profile') .values({ uri: uri.toString(), cid: cid.toString(), creator: uri.host, displayName: obj.displayName, description: obj.description, avatarCid: getBlobCidString(obj.avatar), bannerCid: getBlobCidString(obj.banner), joinedViaStarterPackUri: obj.joinedViaStarterPack?.uri, createdAt: obj.createdAt ?? new Date().toISOString(), indexedAt: timestamp, }) .onConflict((oc) => oc.doNothing()) .returningAll() .executeTakeFirst() return inserted || null } const findDuplicate = async (): Promise => { return null } const notifsForInsert = (obj: IndexedProfile) => { if (!obj.joinedViaStarterPackUri) return [] const starterPackUri = new AtUri(obj.joinedViaStarterPackUri) return [ { did: starterPackUri.host, author: obj.creator, recordUri: obj.uri, recordCid: obj.cid, reason: 'starterpack-joined' as const, reasonSubject: obj.joinedViaStarterPackUri, sortAt: obj.indexedAt, }, ] } const deleteFn = async ( db: DatabaseSchema, uri: AtUri, ): Promise => { const deleted = await db .deleteFrom('profile') .where('uri', '=', uri.toString()) .returningAll() .executeTakeFirst() return deleted || null } const notifsForDelete = () => { return { notifs: [], toDelete: [] } } export type PluginType = ReturnType export const makePlugin = (db: Database, background: BackgroundQueue) => { return new RecordProcessor(db, background, { schema: app.bsky.actor.profile.main, insertFn, findDuplicate, deleteFn, notifsForInsert, notifsForDelete, }) } export default makePlugin