-
Notifications
You must be signed in to change notification settings - Fork 403
/
repost.ts
152 lines (141 loc) · 3.95 KB
/
repost.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { Selectable } from 'kysely'
import { CID } from 'multiformats/cid'
import { AtUri, normalizeDatetimeAlways } from '@atproto/syntax'
import * as Repost from '../../../lexicon/types/app/bsky/feed/repost'
import * as lex from '../../../lexicon/lexicons'
import { DatabaseSchema, DatabaseSchemaType } from '../../../db/database-schema'
import RecordProcessor from '../processor'
import { PrimaryDatabase } from '../../../db'
import { countAll, excluded } from '../../../db/util'
import { BackgroundQueue } from '../../../background'
import { NotificationServer } from '../../../notifications'
const lexId = lex.ids.AppBskyFeedRepost
type IndexedRepost = Selectable<DatabaseSchemaType['repost']>
const insertFn = async (
db: DatabaseSchema,
uri: AtUri,
cid: CID,
obj: Repost.Record,
timestamp: string,
): Promise<IndexedRepost | null> => {
const repost = {
uri: uri.toString(),
cid: cid.toString(),
creator: uri.host,
subject: obj.subject.uri,
subjectCid: obj.subject.cid,
createdAt: normalizeDatetimeAlways(obj.createdAt),
indexedAt: timestamp,
}
const [inserted] = await Promise.all([
db
.insertInto('repost')
.values(repost)
.onConflict((oc) => oc.doNothing())
.returningAll()
.executeTakeFirst(),
db
.insertInto('feed_item')
.values({
type: 'repost',
uri: repost.uri,
cid: repost.cid,
postUri: repost.subject,
originatorDid: repost.creator,
sortAt:
repost.indexedAt < repost.createdAt
? repost.indexedAt
: repost.createdAt,
})
.onConflict((oc) => oc.doNothing())
.executeTakeFirst(),
])
return inserted || null
}
const findDuplicate = async (
db: DatabaseSchema,
uri: AtUri,
obj: Repost.Record,
): Promise<AtUri | null> => {
const found = await db
.selectFrom('repost')
.where('creator', '=', uri.host)
.where('subject', '=', obj.subject.uri)
.selectAll()
.executeTakeFirst()
return found ? new AtUri(found.uri) : null
}
const notifsForInsert = (obj: IndexedRepost) => {
const subjectUri = new AtUri(obj.subject)
// prevent self-notifications
const isSelf = subjectUri.host === obj.creator
return isSelf
? []
: [
{
did: subjectUri.host,
author: obj.creator,
recordUri: obj.uri,
recordCid: obj.cid,
reason: 'repost' as const,
reasonSubject: subjectUri.toString(),
sortAt: obj.sortAt,
},
]
}
const deleteFn = async (
db: DatabaseSchema,
uri: AtUri,
): Promise<IndexedRepost | null> => {
const uriStr = uri.toString()
const [deleted] = await Promise.all([
db
.deleteFrom('repost')
.where('uri', '=', uriStr)
.returningAll()
.executeTakeFirst(),
db.deleteFrom('feed_item').where('uri', '=', uriStr).executeTakeFirst(),
])
return deleted || null
}
const notifsForDelete = (
deleted: IndexedRepost,
replacedBy: IndexedRepost | null,
) => {
const toDelete = replacedBy ? [] : [deleted.uri]
return { notifs: [], toDelete }
}
const updateAggregates = async (db: DatabaseSchema, repost: IndexedRepost) => {
const repostCountQb = db
.insertInto('post_agg')
.values({
uri: repost.subject,
repostCount: db
.selectFrom('repost')
.where('repost.subject', '=', repost.subject)
.select(countAll.as('count')),
})
.onConflict((oc) =>
oc
.column('uri')
.doUpdateSet({ repostCount: excluded(db, 'repostCount') }),
)
await repostCountQb.execute()
}
export type PluginType = RecordProcessor<Repost.Record, IndexedRepost>
export const makePlugin = (
db: PrimaryDatabase,
backgroundQueue: BackgroundQueue,
notifServer?: NotificationServer,
): PluginType => {
return new RecordProcessor(db, backgroundQueue, notifServer, {
lexId,
insertFn,
findDuplicate,
deleteFn,
notifsForInsert,
notifsForDelete,
updateAggregates,
})
}
export default makePlugin