From b9476ccbd6851e2dd0db4a0e9a85838a5716350c Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 9 Sep 2023 22:27:16 -0500 Subject: [PATCH] pipeline: delete unattached-media rows when authoring an event --- src/db/unattached-media.ts | 9 +++++++++ src/pipeline.ts | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/db/unattached-media.ts b/src/db/unattached-media.ts index 6e77e0d..ae9d882 100644 --- a/src/db/unattached-media.ts +++ b/src/db/unattached-media.ts @@ -59,7 +59,16 @@ function getUnattachedMediaByIds(ids: string[]) { .execute(); } +/** Delete rows as an event with media is being created. */ +function deleteAttachedMedia(pubkey: string, urls: string[]) { + return db.deleteFrom('unattached_media') + .where('pubkey', '=', pubkey) + .where('url', 'in', urls) + .execute(); +} + export { + deleteAttachedMedia, deleteUnattachedMediaByUrl, getUnattachedMedia, getUnattachedMediaByIds, diff --git a/src/pipeline.ts b/src/pipeline.ts index b556817..5a80b00 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -1,6 +1,7 @@ import { Conf } from '@/config.ts'; import * as eventsDB from '@/db/events.ts'; import { addRelays } from '@/db/relays.ts'; +import { deleteAttachedMedia } from '@/db/unattached-media.ts'; import { findUser } from '@/db/users.ts'; import { type Event, LRUCache } from '@/deps.ts'; import { isEphemeralKind } from '@/kinds.ts'; @@ -27,6 +28,7 @@ async function handleEvent(event: Event): Promise { processDeletions(event), trackRelays(event), trackHashtags(event), + processMedia(event, data), streamOut(event, data), broadcast(event, data), ]); @@ -120,6 +122,14 @@ function trackRelays(event: Event) { return addRelays([...relays]); } +/** Delete unattached media entries that are attached to the event. */ +function processMedia({ tags, pubkey }: Event, { user }: EventData) { + if (user) { + const urls = getTagSet(tags, 'media'); + return deleteAttachedMedia(pubkey, [...urls]); + } +} + /** Determine if the event is being received in a timely manner. */ const isFresh = (event: Event): boolean => eventAge(event) < Time.seconds(10);