[READ ONLY MIRROR] Spark Social AppView Server
github.com/sprksocial/server
atproto
deno
hono
lexicon
1import { Cid } from "@atp/lex";
2import { AtUri, normalizeDatetimeAlways } from "@atp/syntax";
3import * as so from "../../../lex/so.ts";
4import { BackgroundQueue } from "../../background.ts";
5import { Database } from "../../db/index.ts";
6import { BlockDocument } from "../../db/models.ts";
7import { RecordProcessor } from "../processor.ts";
8
9const schema = so.sprk.graph.block.main;
10type BlockRecord = so.sprk.graph.block.Main;
11type IndexedBlock = BlockDocument;
12
13const insertFn = async (
14 db: Database,
15 uri: AtUri,
16 cid: Cid,
17 obj: BlockRecord,
18 timestamp: string,
19): Promise<IndexedBlock | null> => {
20 const block = {
21 uri: uri.toString(),
22 cid: cid.toString(),
23 authorDid: uri.host,
24 subject: obj.subject,
25 createdAt: normalizeDatetimeAlways(obj.createdAt),
26 indexedAt: timestamp,
27 };
28
29 const insertedBlock = await db.models.Block.findOneAndUpdate(
30 { uri: block.uri },
31 { $set: block },
32 { upsert: true, returnDocument: "after" },
33 );
34 return insertedBlock;
35};
36
37const findDuplicate = async (
38 db: Database,
39 uri: AtUri,
40 obj: BlockRecord,
41): Promise<AtUri | null> => {
42 const found = await db.models.Block.findOne({
43 authorDid: uri.host,
44 subject: obj.subject,
45 }).select("uri").lean();
46 return found ? new AtUri(found.uri) : null;
47};
48
49const notifsForInsert = () => {
50 return [];
51};
52
53const deleteFn = async (
54 db: Database,
55 uri: AtUri,
56): Promise<IndexedBlock | null> => {
57 const deleted = await db.models.Block.findOneAndDelete({
58 uri: uri.toString(),
59 });
60 return deleted;
61};
62
63const notifsForDelete = () => {
64 return { notifs: [], toDelete: [] };
65};
66
67export type PluginType = RecordProcessor<typeof schema, IndexedBlock>;
68
69export const makePlugin = (
70 db: Database,
71 background: BackgroundQueue,
72): PluginType => {
73 return new RecordProcessor(db, background, {
74 schema,
75 insertFn,
76 findDuplicate,
77 deleteFn,
78 notifsForInsert,
79 notifsForDelete,
80 });
81};
82
83export default makePlugin;