-
Notifications
You must be signed in to change notification settings - Fork 554
/
post.ts
475 lines (452 loc) · 13.5 KB
/
post.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
import { Insertable, Selectable, sql } from 'kysely'
import { CID } from 'multiformats/cid'
import { AtUri, normalizeDatetimeAlways } from '@atproto/syntax'
import { jsonStringToLex } from '@atproto/lexicon'
import {
Record as PostRecord,
ReplyRef,
} from '../../../lexicon/types/app/bsky/feed/post'
import { Record as GateRecord } from '../../../lexicon/types/app/bsky/feed/threadgate'
import { isMain as isEmbedImage } from '../../../lexicon/types/app/bsky/embed/images'
import { isMain as isEmbedExternal } from '../../../lexicon/types/app/bsky/embed/external'
import { isMain as isEmbedRecord } from '../../../lexicon/types/app/bsky/embed/record'
import { isMain as isEmbedRecordWithMedia } from '../../../lexicon/types/app/bsky/embed/recordWithMedia'
import {
isMention,
isLink,
} from '../../../lexicon/types/app/bsky/richtext/facet'
import * as lex from '../../../lexicon/lexicons'
import { DatabaseSchema, DatabaseSchemaType } from '../../../db/database-schema'
import RecordProcessor from '../processor'
import { Notification } from '../../../db/tables/notification'
import { PrimaryDatabase } from '../../../db'
import { countAll, excluded } from '../../../db/util'
import { BackgroundQueue } from '../../../background'
import { getAncestorsAndSelfQb, getDescendentsQb } from '../../util/post'
import { NotificationServer } from '../../../notifications'
import * as feedutil from '../../feed/util'
import { postToThreadgateUri } from '../../feed/util'
type Notif = Insertable<Notification>
type Post = Selectable<DatabaseSchemaType['post']>
type PostEmbedImage = DatabaseSchemaType['post_embed_image']
type PostEmbedExternal = DatabaseSchemaType['post_embed_external']
type PostEmbedRecord = DatabaseSchemaType['post_embed_record']
type PostAncestor = {
uri: string
height: number
}
type PostDescendent = {
uri: string
depth: number
cid: string
creator: string
sortAt: string
}
type IndexedPost = {
post: Post
facets?: { type: 'mention' | 'link'; value: string }[]
embeds?: (PostEmbedImage[] | PostEmbedExternal | PostEmbedRecord)[]
ancestors?: PostAncestor[]
descendents?: PostDescendent[]
}
const lexId = lex.ids.AppBskyFeedPost
const REPLY_NOTIF_DEPTH = 5
const insertFn = async (
db: DatabaseSchema,
uri: AtUri,
cid: CID,
obj: PostRecord,
timestamp: string,
): Promise<IndexedPost | null> => {
const post = {
uri: uri.toString(),
cid: cid.toString(),
creator: uri.host,
text: obj.text,
createdAt: normalizeDatetimeAlways(obj.createdAt),
replyRoot: obj.reply?.root?.uri || null,
replyRootCid: obj.reply?.root?.cid || null,
replyParent: obj.reply?.parent?.uri || null,
replyParentCid: obj.reply?.parent?.cid || null,
langs: obj.langs?.length
? sql<string[]>`${JSON.stringify(obj.langs)}` // sidesteps kysely's array serialization, which is non-jsonb
: null,
tags: obj.tags?.length
? sql<string[]>`${JSON.stringify(obj.tags)}` // sidesteps kysely's array serialization, which is non-jsonb
: null,
indexedAt: timestamp,
}
const [insertedPost] = await Promise.all([
db
.insertInto('post')
.values(post)
.onConflict((oc) => oc.doNothing())
.returningAll()
.executeTakeFirst(),
db
.insertInto('feed_item')
.values({
type: 'post',
uri: post.uri,
cid: post.cid,
postUri: post.uri,
originatorDid: post.creator,
sortAt:
post.indexedAt < post.createdAt ? post.indexedAt : post.createdAt,
})
.onConflict((oc) => oc.doNothing())
.executeTakeFirst(),
])
if (!insertedPost) {
return null // Post already indexed
}
if (obj.reply) {
const { invalidReplyRoot, violatesThreadGate } = await validateReply(
db,
uri.host,
obj.reply,
)
if (invalidReplyRoot || violatesThreadGate) {
Object.assign(insertedPost, { invalidReplyRoot, violatesThreadGate })
await db
.updateTable('post')
.where('uri', '=', post.uri)
.set({ invalidReplyRoot, violatesThreadGate })
.executeTakeFirst()
}
}
const facets = (obj.facets || [])
.flatMap((facet) => facet.features)
.flatMap((feature) => {
if (isMention(feature)) {
return {
type: 'mention' as const,
value: feature.did,
}
}
if (isLink(feature)) {
return {
type: 'link' as const,
value: feature.uri,
}
}
return []
})
// Embed indices
const embeds: (PostEmbedImage[] | PostEmbedExternal | PostEmbedRecord)[] = []
const postEmbeds = separateEmbeds(obj.embed)
for (const postEmbed of postEmbeds) {
if (isEmbedImage(postEmbed)) {
const { images } = postEmbed
const imagesEmbed = images.map((img, i) => ({
postUri: uri.toString(),
position: i,
imageCid: img.image.ref.toString(),
alt: img.alt,
}))
embeds.push(imagesEmbed)
await db.insertInto('post_embed_image').values(imagesEmbed).execute()
} else if (isEmbedExternal(postEmbed)) {
const { external } = postEmbed
const externalEmbed = {
postUri: uri.toString(),
uri: external.uri,
title: external.title,
description: external.description,
thumbCid: external.thumb?.ref.toString() || null,
}
embeds.push(externalEmbed)
await db.insertInto('post_embed_external').values(externalEmbed).execute()
} else if (isEmbedRecord(postEmbed)) {
const { record } = postEmbed
const recordEmbed = {
postUri: uri.toString(),
embedUri: record.uri,
embedCid: record.cid,
}
embeds.push(recordEmbed)
await db.insertInto('post_embed_record').values(recordEmbed).execute()
}
}
const ancestors = await getAncestorsAndSelfQb(db, {
uri: post.uri,
parentHeight: REPLY_NOTIF_DEPTH,
})
.selectFrom('ancestor')
.selectAll()
.execute()
const descendents = await getDescendentsQb(db, {
uri: post.uri,
depth: REPLY_NOTIF_DEPTH,
})
.selectFrom('descendent')
.innerJoin('post', 'post.uri', 'descendent.uri')
.selectAll('descendent')
.select(['cid', 'creator', 'sortAt'])
.execute()
return {
post: insertedPost,
facets,
embeds,
ancestors,
descendents,
}
}
const findDuplicate = async (): Promise<AtUri | null> => {
return null
}
const notifsForInsert = (obj: IndexedPost) => {
const notifs: Notif[] = []
const notified = new Set([obj.post.creator])
const maybeNotify = (notif: Notif) => {
if (!notified.has(notif.did)) {
notified.add(notif.did)
notifs.push(notif)
}
}
for (const facet of obj.facets ?? []) {
if (facet.type === 'mention') {
maybeNotify({
did: facet.value,
reason: 'mention',
author: obj.post.creator,
recordUri: obj.post.uri,
recordCid: obj.post.cid,
sortAt: obj.post.sortAt,
})
}
}
for (const embed of obj.embeds ?? []) {
if ('embedUri' in embed) {
const embedUri = new AtUri(embed.embedUri)
if (embedUri.collection === lex.ids.AppBskyFeedPost) {
maybeNotify({
did: embedUri.host,
reason: 'quote',
reasonSubject: embedUri.toString(),
author: obj.post.creator,
recordUri: obj.post.uri,
recordCid: obj.post.cid,
sortAt: obj.post.sortAt,
})
}
}
}
if (obj.post.violatesThreadGate) {
// don't generate reply notifications when post violates threadgate
return notifs
}
// reply notifications
for (const ancestor of obj.ancestors ?? []) {
if (ancestor.uri === obj.post.uri) continue // no need to notify for own post
if (ancestor.height < REPLY_NOTIF_DEPTH) {
const ancestorUri = new AtUri(ancestor.uri)
maybeNotify({
did: ancestorUri.host,
reason: 'reply',
reasonSubject: ancestorUri.toString(),
author: obj.post.creator,
recordUri: obj.post.uri,
recordCid: obj.post.cid,
sortAt: obj.post.sortAt,
})
}
}
// descendents indicate out-of-order indexing: need to notify
// the current post and upwards.
for (const descendent of obj.descendents ?? []) {
for (const ancestor of obj.ancestors ?? []) {
const totalHeight = descendent.depth + ancestor.height
if (totalHeight < REPLY_NOTIF_DEPTH) {
const ancestorUri = new AtUri(ancestor.uri)
maybeNotify({
did: ancestorUri.host,
reason: 'reply',
reasonSubject: ancestorUri.toString(),
author: descendent.creator,
recordUri: descendent.uri,
recordCid: descendent.cid,
sortAt: descendent.sortAt,
})
}
}
}
return notifs
}
const deleteFn = async (
db: DatabaseSchema,
uri: AtUri,
): Promise<IndexedPost | null> => {
const uriStr = uri.toString()
const [deleted] = await Promise.all([
db
.deleteFrom('post')
.where('uri', '=', uriStr)
.returningAll()
.executeTakeFirst(),
db.deleteFrom('feed_item').where('postUri', '=', uriStr).executeTakeFirst(),
])
const deletedEmbeds: (
| PostEmbedImage[]
| PostEmbedExternal
| PostEmbedRecord
)[] = []
const [deletedImgs, deletedExternals, deletedPosts] = await Promise.all([
db
.deleteFrom('post_embed_image')
.where('postUri', '=', uriStr)
.returningAll()
.execute(),
db
.deleteFrom('post_embed_external')
.where('postUri', '=', uriStr)
.returningAll()
.executeTakeFirst(),
db
.deleteFrom('post_embed_record')
.where('postUri', '=', uriStr)
.returningAll()
.executeTakeFirst(),
])
if (deletedImgs.length) {
deletedEmbeds.push(deletedImgs)
}
if (deletedExternals) {
deletedEmbeds.push(deletedExternals)
}
if (deletedPosts) {
deletedEmbeds.push(deletedPosts)
}
return deleted
? {
post: deleted,
facets: [], // Not used
embeds: deletedEmbeds,
}
: null
}
const notifsForDelete = (
deleted: IndexedPost,
replacedBy: IndexedPost | null,
) => {
const notifs = replacedBy ? notifsForInsert(replacedBy) : []
return {
notifs,
toDelete: [deleted.post.uri],
}
}
const updateAggregates = async (db: DatabaseSchema, postIdx: IndexedPost) => {
const replyCountQb = postIdx.post.replyParent
? db
.insertInto('post_agg')
.values({
uri: postIdx.post.replyParent,
replyCount: db
.selectFrom('post')
.where('post.replyParent', '=', postIdx.post.replyParent)
.where((qb) =>
qb
.where('post.violatesThreadGate', 'is', null)
.orWhere('post.violatesThreadGate', '=', false),
)
.select(countAll.as('count')),
})
.onConflict((oc) =>
oc
.column('uri')
.doUpdateSet({ replyCount: excluded(db, 'replyCount') }),
)
: null
const postsCountQb = db
.insertInto('profile_agg')
.values({
did: postIdx.post.creator,
postsCount: db
.selectFrom('post')
.where('post.creator', '=', postIdx.post.creator)
.select(countAll.as('count')),
})
.onConflict((oc) =>
oc.column('did').doUpdateSet({ postsCount: excluded(db, 'postsCount') }),
)
await Promise.all([replyCountQb?.execute(), postsCountQb.execute()])
}
export type PluginType = RecordProcessor<PostRecord, IndexedPost>
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
function separateEmbeds(embed: PostRecord['embed']) {
if (!embed) {
return []
}
if (isEmbedRecordWithMedia(embed)) {
return [{ $type: lex.ids.AppBskyEmbedRecord, ...embed.record }, embed.media]
}
return [embed]
}
async function validateReply(
db: DatabaseSchema,
creator: string,
reply: ReplyRef,
) {
const replyRefs = await getReplyRefs(db, reply)
// check reply
const invalidReplyRoot =
!replyRefs.parent || feedutil.invalidReplyRoot(reply, replyRefs.parent)
// check interaction
const violatesThreadGate = await feedutil.violatesThreadGate(
db,
creator,
new AtUri(reply.root.uri).hostname,
replyRefs.root?.record ?? null,
replyRefs.gate?.record ?? null,
)
return {
invalidReplyRoot,
violatesThreadGate,
}
}
async function getReplyRefs(db: DatabaseSchema, reply: ReplyRef) {
const replyRoot = reply.root.uri
const replyParent = reply.parent.uri
const replyGate = postToThreadgateUri(replyRoot)
const results = await db
.selectFrom('record')
.where('record.uri', 'in', [replyRoot, replyGate, replyParent])
.leftJoin('post', 'post.uri', 'record.uri')
.selectAll('post')
.select(['record.uri', 'json'])
.execute()
const root = results.find((ref) => ref.uri === replyRoot)
const parent = results.find((ref) => ref.uri === replyParent)
const gate = results.find((ref) => ref.uri === replyGate)
return {
root: root && {
uri: root.uri,
invalidReplyRoot: root.invalidReplyRoot,
record: jsonStringToLex(root.json) as PostRecord,
},
parent: parent && {
uri: parent.uri,
invalidReplyRoot: parent.invalidReplyRoot,
record: jsonStringToLex(parent.json) as PostRecord,
},
gate: gate && {
uri: gate.uri,
record: jsonStringToLex(gate.json) as GateRecord,
},
}
}