-
Notifications
You must be signed in to change notification settings - Fork 643
/
Copy pathblock.ts
89 lines (80 loc) · 2.28 KB
/
block.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
86
87
88
89
import { Selectable } from 'kysely'
import { AtUri, normalizeDatetimeAlways } from '@atproto/syntax'
import { CID } from 'multiformats/cid'
import * as Block from '../../../lexicon/types/app/bsky/graph/block'
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.AppBskyGraphBlock
type IndexedBlock = Selectable<DatabaseSchemaType['actor_block']>
const insertFn = async (
db: DatabaseSchema,
uri: AtUri,
cid: CID,
obj: Block.Record,
timestamp: string,
): Promise<IndexedBlock | null> => {
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: Block.Record,
): Promise<AtUri | null> => {
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<IndexedBlock | null> => {
const deleted = await db
.deleteFrom('actor_block')
.where('uri', '=', uri.toString())
.returningAll()
.executeTakeFirst()
return deleted || null
}
const notifsForDelete = () => {
return { notifs: [], toDelete: [] }
}
export type PluginType = RecordProcessor<Block.Record, IndexedBlock>
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