forked from bluesky-social/feed-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscription.ts
82 lines (74 loc) · 2.31 KB
/
subscription.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
import {
OutputSchema as RepoEvent,
isCommit,
} from './lexicon/types/com/atproto/sync/subscribeRepos'
import { FirehoseSubscriptionBase, getOpsByType } from './util/subscription'
const matchText: string[] = [
'physics of life',
'living physics',
'theoretical biology',
'physics of behavior',
'physics of living matter',
]
const matchPatterns: RegExp[] = [
/(^|[\s\W])livingmatter($|[\W\s])/im,
/(^|[\s\W])livingphysics($|[\W\s])/im,
]
// Include high profile POL users here to always include their posts
const matchUsers: string[] = [
'did:plc:veib4xwtlli5baydbzbxqtvh',
//
]
// Exclude posts from these users
const bannedUsers: string[] = [
//
]
export class FirehoseSubscription extends FirehoseSubscriptionBase {
async handleEvent(evt: RepoEvent) {
if (!isCommit(evt)) return
const ops = await getOpsByType(evt)
// This logs the text of every post off the firehose.
// Just for fun :)
// Delete before actually using
//for (const post of ops.posts.creates) {
// console.log(post.record.text)
//}
const postsToDelete = ops.posts.deletes.map((del) => del.uri)
const postsToCreate = ops.posts.creates
.filter((create) => {
// only alf-related posts
// return create.record.text.toLowerCase().includes('alf')
const txt = create.record.text.toLowerCase()
return (
(matchText.some((term) => txt.includes(term)) ||
matchPatterns.some((pattern) => pattern.test(txt)) ||
matchUsers.includes(create.author)) &&
!bannedUsers.includes(create.author)
)
})
.map((create) => {
// map POL-related posts to a db row
console.log(`Found post by ${create.author}: ${create.record.text}`)
return {
uri: create.uri,
cid: create.cid,
replyParent: create.record?.reply?.parent.uri ?? null,
replyRoot: create.record?.reply?.root.uri ?? null,
indexedAt: new Date().toISOString(),
}
})
if (postsToDelete.length > 0) {
await this.db
.deleteFrom('post')
.where('uri', 'in', postsToDelete)
.execute()
}
if (postsToCreate.length > 0) {
await this.db
.insertInto('post')
.values(postsToCreate)
.onConflict((oc) => oc.doNothing())
.execute()
}
}
}