[READ ONLY MIRROR] Spark Social AppView Server
github.com/sprksocial/server
atproto
deno
hono
lexicon
1import { CID } from "multiformats/cid";
2import { AtUri } from "@atp/syntax";
3import * as lex from "../../../lex/lexicons.ts";
4import * as Profile from "../../../lex/types/so/sprk/actor/profile.ts";
5import { BackgroundQueue } from "../../background.ts";
6import { Database } from "../../db/index.ts";
7import { ProfileDocument } from "../../db/models.ts";
8import { RecordProcessor } from "../processor.ts";
9
10const lexId = lex.ids.SoSprkActorProfile;
11type IndexedProfile = ProfileDocument;
12
13const insertFn = async (
14 db: Database,
15 uri: AtUri,
16 cid: CID,
17 obj: Profile.Record,
18 timestamp: string,
19): Promise<IndexedProfile | null> => {
20 if (uri.rkey !== "self") return null;
21
22 const profile = {
23 uri: uri.toString(),
24 cid: cid.toString(),
25 authorDid: uri.host,
26 displayName: obj?.displayName,
27 description: obj?.description,
28 avatar: obj?.avatar,
29 banner: obj?.banner,
30 pinnedPost: obj?.pinnedPost,
31 createdAt: obj?.createdAt || new Date().toISOString(),
32 indexedAt: timestamp,
33 };
34
35 const insertedProfile = await db.models.Profile.findOneAndUpdate(
36 { uri: profile.uri },
37 { $set: profile },
38 { upsert: true, new: true },
39 );
40 return insertedProfile;
41};
42
43const findDuplicate = (): AtUri | null => {
44 return null;
45};
46
47const notifsForInsert = () => {
48 return [];
49};
50
51const deleteFn = async (
52 db: Database,
53 uri: AtUri,
54): Promise<IndexedProfile | null> => {
55 const deleted = await db.models.Profile.findOneAndDelete({
56 uri: uri.toString(),
57 });
58 return deleted;
59};
60
61const notifsForDelete = () => {
62 return { notifs: [], toDelete: [] };
63};
64
65export type PluginType = RecordProcessor<Profile.Record, IndexedProfile>;
66
67export const makePlugin = (
68 db: Database,
69 background: BackgroundQueue,
70): PluginType => {
71 return new RecordProcessor(db, background, {
72 lexId,
73 insertFn,
74 findDuplicate,
75 deleteFn,
76 notifsForInsert,
77 notifsForDelete,
78 });
79};
80
81export default makePlugin;