denonbu-feed/src/subscription.ts

43 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-05-10 14:56:30 +00:00
import {
OutputSchema as RepoEvent,
isCommit,
} from './lexicon/types/com/atproto/sync/subscribeRepos'
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
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
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,
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
.deleteFrom('post')
2023-05-10 14:56:30 +00:00
.where('uri', 'in', postsToDelete)
.execute()
}
if (postsToCreate.length > 0) {
await this.db
.insertInto('post')
2023-05-10 14:56:30 +00:00
.values(postsToCreate)
.onConflict((oc) => oc.doNothing())
.execute()
}
}
}