pipeline: delete unattached-media rows when authoring an event

This commit is contained in:
Alex Gleason 2023-09-09 22:27:16 -05:00
parent 0d343fa190
commit b9476ccbd6
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 19 additions and 0 deletions

View File

@ -59,7 +59,16 @@ function getUnattachedMediaByIds(ids: string[]) {
.execute(); .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 { export {
deleteAttachedMedia,
deleteUnattachedMediaByUrl, deleteUnattachedMediaByUrl,
getUnattachedMedia, getUnattachedMedia,
getUnattachedMediaByIds, getUnattachedMediaByIds,

View File

@ -1,6 +1,7 @@
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import * as eventsDB from '@/db/events.ts'; import * as eventsDB from '@/db/events.ts';
import { addRelays } from '@/db/relays.ts'; import { addRelays } from '@/db/relays.ts';
import { deleteAttachedMedia } from '@/db/unattached-media.ts';
import { findUser } from '@/db/users.ts'; import { findUser } from '@/db/users.ts';
import { type Event, LRUCache } from '@/deps.ts'; import { type Event, LRUCache } from '@/deps.ts';
import { isEphemeralKind } from '@/kinds.ts'; import { isEphemeralKind } from '@/kinds.ts';
@ -27,6 +28,7 @@ async function handleEvent(event: Event): Promise<void> {
processDeletions(event), processDeletions(event),
trackRelays(event), trackRelays(event),
trackHashtags(event), trackHashtags(event),
processMedia(event, data),
streamOut(event, data), streamOut(event, data),
broadcast(event, data), broadcast(event, data),
]); ]);
@ -120,6 +122,14 @@ function trackRelays(event: Event) {
return addRelays([...relays]); 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. */ /** Determine if the event is being received in a timely manner. */
const isFresh = (event: Event): boolean => eventAge(event) < Time.seconds(10); const isFresh = (event: Event): boolean => eventAge(event) < Time.seconds(10);