import { Selectable } from 'kysely' import { Cid } from '@atproto/lex' import { AtUri, normalizeDatetimeAlways } from '@atproto/syntax' import { InvalidRequestError } from '@atproto/xrpc-server' 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 IndexedListItem = Selectable const insertFn = async ( db: DatabaseSchema, uri: AtUri, cid: Cid, obj: app.bsky.graph.listitem.Main, timestamp: string, ): Promise => { const listUri = new AtUri(obj.list) if (listUri.hostname !== uri.hostname) { throw new InvalidRequestError( 'Creator of listitem does not match creator of list', ) } const inserted = await db .insertInto('list_item') .values({ uri: uri.toString(), cid: cid.toString(), creator: uri.host, subjectDid: obj.subject, listUri: obj.list, 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.listitem.Main, ): Promise => { const found = await db .selectFrom('list_item') .where('listUri', '=', obj.list) .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('list_item') .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.listitem.main, insertFn, findDuplicate, deleteFn, notifsForInsert, notifsForDelete, }) } export default makePlugin