pipeline: process kind 5 deletion events

This commit is contained in:
Alex Gleason 2023-09-05 21:24:59 -05:00
parent 22ddc7b1a8
commit e2b88d57d9
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 30 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import { isEphemeralKind } from '@/kinds.ts';
import * as mixer from '@/mixer.ts';
import { isLocallyFollowed } from '@/queries.ts';
import { Sub } from '@/subs.ts';
import { getTagSet } from '@/tags.ts';
import { trends } from '@/trends.ts';
import { eventAge, isRelay, nostrDate, Time } from '@/utils.ts';
@ -22,6 +23,7 @@ async function handleEvent(event: Event): Promise<void> {
await Promise.all([
storeEvent(event, data),
processDeletions(event),
trackRelays(event),
trackHashtags(event),
streamOut(event, data),
@ -67,6 +69,20 @@ async function storeEvent(event: Event, data: EventData): Promise<void> {
}
}
/** Query to-be-deleted events, ensure their pubkey matches, then delete them from the database. */
async function processDeletions(event: Event): Promise<void> {
if (event.kind === 5) {
const ids = getTagSet(event.tags, 'e');
const events = await eventsDB.getFilters([{ ids: [...ids] }]);
const deleteIds = events
.filter(({ pubkey, id }) => pubkey === event.pubkey && ids.has(id))
.map((event) => event.id);
await eventsDB.deleteFilters([{ ids: deleteIds }]);
}
}
/** Track whenever a hashtag is used, for processing trending tags. */
function trackHashtags(event: Event): void {
const date = nostrDate(event.created_at);

14
src/tags.ts Normal file
View File

@ -0,0 +1,14 @@
/** Get the values for a tag in a `Set`. */
function getTagSet(tags: string[][], tagName: string): Set<string> {
const set = new Set<string>();
tags.forEach((tag) => {
if (tag[0] === tagName) {
set.add(tag[1]);
}
});
return set;
}
export { getTagSet };