-
Notifications
You must be signed in to change notification settings - Fork 604
/
Copy pathfollow.ts
135 lines (125 loc) · 3.45 KB
/
follow.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { Selectable } from 'kysely'
import { AtUri, normalizeDatetimeAlways } from '@atproto/syntax'
import { CID } from 'multiformats/cid'
import * as Follow from '../../../../lexicon/types/app/bsky/graph/follow'
import * as lex from '../../../../lexicon/lexicons'
import RecordProcessor from '../processor'
import { Database } from '../../db'
import { countAll, excluded } from '../../db/util'
import { DatabaseSchema, DatabaseSchemaType } from '../../db/database-schema'
import { BackgroundQueue } from '../../background'
const lexId = lex.ids.AppBskyGraphFollow
type IndexedFollow = Selectable<DatabaseSchemaType['follow']>
const insertFn = async (
db: DatabaseSchema,
uri: AtUri,
cid: CID,
obj: Follow.Record,
timestamp: string,
): Promise<IndexedFollow | null> => {
const inserted = await db
.insertInto('follow')
.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: Follow.Record,
): Promise<AtUri | null> => {
const found = await db
.selectFrom('follow')
.where('creator', '=', uri.host)
.where('subjectDid', '=', obj.subject)
.selectAll()
.executeTakeFirst()
return found ? new AtUri(found.uri) : null
}
const notifsForInsert = (obj: IndexedFollow) => {
return [
{
did: obj.subjectDid,
author: obj.creator,
recordUri: obj.uri,
recordCid: obj.cid,
reason: 'follow' as const,
reasonSubject: null,
sortAt: obj.sortAt,
},
]
}
const deleteFn = async (
db: DatabaseSchema,
uri: AtUri,
): Promise<IndexedFollow | null> => {
const deleted = await db
.deleteFrom('follow')
.where('uri', '=', uri.toString())
.returningAll()
.executeTakeFirst()
return deleted || null
}
const notifsForDelete = (
deleted: IndexedFollow,
replacedBy: IndexedFollow | null,
) => {
const toDelete = replacedBy ? [] : [deleted.uri]
return { notifs: [], toDelete }
}
const updateAggregates = async (db: DatabaseSchema, follow: IndexedFollow) => {
const followersCountQb = db
.insertInto('profile_agg')
.values({
did: follow.subjectDid,
followersCount: db
.selectFrom('follow')
.where('follow.subjectDid', '=', follow.subjectDid)
.select(countAll.as('count')),
})
.onConflict((oc) =>
oc.column('did').doUpdateSet({
followersCount: excluded(db, 'followersCount'),
}),
)
const followsCountQb = db
.insertInto('profile_agg')
.values({
did: follow.creator,
followsCount: db
.selectFrom('follow')
.where('follow.creator', '=', follow.creator)
.select(countAll.as('count')),
})
.onConflict((oc) =>
oc.column('did').doUpdateSet({
followsCount: excluded(db, 'followsCount'),
}),
)
await Promise.all([followersCountQb.execute(), followsCountQb.execute()])
}
export type PluginType = RecordProcessor<Follow.Record, IndexedFollow>
export const makePlugin = (
db: Database,
background: BackgroundQueue,
): PluginType => {
return new RecordProcessor(db, background, {
lexId,
insertFn,
findDuplicate,
deleteFn,
notifsForInsert,
notifsForDelete,
updateAggregates,
})
}
export default makePlugin