import { Selectable } from 'kysely' import { Cid } from '@atproto/lex' import { AtUri, normalizeDatetimeAlways } 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 IndexedBlock = Selectable const insertFn = async ( db: DatabaseSchema, uri: AtUri, cid: Cid, obj: app.bsky.graph.block.Main, timestamp: string, ): Promise => { const inserted = await db .insertInto('actor_block') .values({ uri: uri.toString(), cid: cid.toString(), creator: uri.host, subjectDid: obj.subject, createdAt: normalizeDatetimeAlways(obj.createdAt), indexedAt: timestamp, }) .onConflict((oc) => oc.doNothing()) .returningAll() .executeTakeFirst() return inserted || null } const findDuplicate = async ( db: DatabaseSchema, uri: AtUri, obj: app.bsky.graph.block.Main, ): Promise => { const found = await db .selectFrom('actor_block') .where('creator', '=', uri.host) .where('subjectDid', '=', obj.subject) .selectAll() .executeTakeFirst() return found ? new AtUri(found.uri) : null } const notifsForInsert = () => { return [] } const deleteFn = async ( db: DatabaseSchema, uri: AtUri, ): Promise => { const deleted = await db .deleteFrom('actor_block') .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.graph.block.main, insertFn, findDuplicate, deleteFn, notifsForInsert, notifsForDelete, }) } export default makePlugin