-
Notifications
You must be signed in to change notification settings - Fork 403
/
list.ts
85 lines (76 loc) · 2.2 KB
/
list.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Selectable } from 'kysely'
import { AtUri, normalizeDatetimeAlways } from '@atproto/syntax'
import { CID } from 'multiformats/cid'
import * as List from '../../../lexicon/types/app/bsky/graph/list'
import * as lex from '../../../lexicon/lexicons'
import { DatabaseSchema, DatabaseSchemaType } from '../../../db/database-schema'
import RecordProcessor from '../processor'
import { PrimaryDatabase } from '../../../db'
import { BackgroundQueue } from '../../../background'
import { NotificationServer } from '../../../notifications'
const lexId = lex.ids.AppBskyGraphList
type IndexedList = Selectable<DatabaseSchemaType['list']>
const insertFn = async (
db: DatabaseSchema,
uri: AtUri,
cid: CID,
obj: List.Record,
timestamp: string,
): Promise<IndexedList | null> => {
const inserted = await db
.insertInto('list')
.values({
uri: uri.toString(),
cid: cid.toString(),
creator: uri.host,
name: obj.name,
purpose: obj.purpose,
description: obj.description,
descriptionFacets: obj.descriptionFacets
? JSON.stringify(obj.descriptionFacets)
: undefined,
avatarCid: obj.avatar?.ref.toString(),
createdAt: normalizeDatetimeAlways(obj.createdAt),
indexedAt: timestamp,
})
.onConflict((oc) => oc.doNothing())
.returningAll()
.executeTakeFirst()
return inserted || null
}
const findDuplicate = async (): Promise<AtUri | null> => {
return null
}
const notifsForInsert = () => {
return []
}
const deleteFn = async (
db: DatabaseSchema,
uri: AtUri,
): Promise<IndexedList | null> => {
const deleted = await db
.deleteFrom('list')
.where('uri', '=', uri.toString())
.returningAll()
.executeTakeFirst()
return deleted || null
}
const notifsForDelete = () => {
return { notifs: [], toDelete: [] }
}
export type PluginType = RecordProcessor<List.Record, IndexedList>
export const makePlugin = (
db: PrimaryDatabase,
backgroundQueue: BackgroundQueue,
notifServer?: NotificationServer,
): PluginType => {
return new RecordProcessor(db, backgroundQueue, notifServer, {
lexId,
insertFn,
findDuplicate,
deleteFn,
notifsForInsert,
notifsForDelete,
})
}
export default makePlugin