2023-05-10 14:56:30 +00:00
|
|
|
import {
|
|
|
|
OutputSchema as RepoEvent,
|
|
|
|
isCommit,
|
|
|
|
} from './lexicon/types/com/atproto/sync/subscribeRepos'
|
2023-05-11 04:02:18 +00:00
|
|
|
import { FirehoseSubscriptionBase, getOpsByType } from './util/subscription'
|
2023-05-10 14:56:30 +00:00
|
|
|
|
|
|
|
export class FirehoseSubscription extends FirehoseSubscriptionBase {
|
|
|
|
async handleEvent(evt: RepoEvent) {
|
|
|
|
if (!isCommit(evt)) return
|
2023-05-11 04:02:18 +00:00
|
|
|
const ops = await getOpsByType(evt)
|
|
|
|
const postsToDelete = ops.posts.deletes.map((del) => del.uri)
|
|
|
|
const postsToCreate = ops.posts.creates
|
2023-05-10 14:56:30 +00:00
|
|
|
.filter((create) => {
|
|
|
|
// only alf-related posts
|
2023-05-11 04:02:18 +00:00
|
|
|
return create.record.text.toLowerCase().includes('alf')
|
2023-05-10 14:56:30 +00:00
|
|
|
})
|
|
|
|
.map((create) => {
|
|
|
|
// map alf-related posts to a db row
|
|
|
|
return {
|
|
|
|
uri: create.uri,
|
|
|
|
cid: create.cid,
|
2023-05-11 04:02:18 +00:00
|
|
|
replyParent: create.record?.reply?.parent.uri ?? null,
|
|
|
|
replyRoot: create.record?.reply?.root.uri ?? null,
|
2023-05-10 14:56:30 +00:00
|
|
|
indexedAt: new Date().toISOString(),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (postsToDelete.length > 0) {
|
|
|
|
await this.db
|
2023-05-11 04:00:44 +00:00
|
|
|
.deleteFrom('post')
|
2023-05-10 14:56:30 +00:00
|
|
|
.where('uri', 'in', postsToDelete)
|
|
|
|
.execute()
|
|
|
|
}
|
|
|
|
if (postsToCreate.length > 0) {
|
|
|
|
await this.db
|
2023-05-11 04:00:44 +00:00
|
|
|
.insertInto('post')
|
2023-05-10 14:56:30 +00:00
|
|
|
.values(postsToCreate)
|
|
|
|
.onConflict((oc) => oc.doNothing())
|
|
|
|
.execute()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|