mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Remove unattached_media table, replace with LRUCache, fix media upload order problem
This commit is contained in:
parent
85000cd00e
commit
8efd6fbb20
10 changed files with 100 additions and 139 deletions
17
src/DittoUploads.ts
Normal file
17
src/DittoUploads.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
import { LRUCache } from 'lru-cache';
|
||||||
|
|
||||||
|
import { Time } from '@/utils/time.ts';
|
||||||
|
|
||||||
|
export interface DittoUpload {
|
||||||
|
id: string;
|
||||||
|
pubkey: string;
|
||||||
|
description?: string;
|
||||||
|
url: string;
|
||||||
|
tags: string[][];
|
||||||
|
uploadedAt: Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const dittoUploads = new LRUCache<string, DittoUpload>({
|
||||||
|
max: 1000,
|
||||||
|
ttl: Time.minutes(15),
|
||||||
|
});
|
||||||
|
|
@ -5,7 +5,7 @@ import { fileSchema } from '@/schema.ts';
|
||||||
import { parseBody } from '@/utils/api.ts';
|
import { parseBody } from '@/utils/api.ts';
|
||||||
import { renderAttachment } from '@/views/mastodon/attachments.ts';
|
import { renderAttachment } from '@/views/mastodon/attachments.ts';
|
||||||
import { uploadFile } from '@/utils/upload.ts';
|
import { uploadFile } from '@/utils/upload.ts';
|
||||||
import { setMediaDescription } from '@/db/unattached-media.ts';
|
import { dittoUploads } from '@/DittoUploads.ts';
|
||||||
|
|
||||||
const mediaBodySchema = z.object({
|
const mediaBodySchema = z.object({
|
||||||
file: fileSchema,
|
file: fileSchema,
|
||||||
|
|
@ -39,19 +39,24 @@ const mediaController: AppController = async (c) => {
|
||||||
|
|
||||||
const updateMediaController: AppController = async (c) => {
|
const updateMediaController: AppController = async (c) => {
|
||||||
const result = mediaUpdateSchema.safeParse(await parseBody(c.req.raw));
|
const result = mediaUpdateSchema.safeParse(await parseBody(c.req.raw));
|
||||||
|
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
return c.json({ error: 'Bad request.', schema: result.error }, 422);
|
return c.json({ error: 'Bad request.', schema: result.error }, 422);
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
const { description } = result.data;
|
const id = c.req.param('id');
|
||||||
if (!await setMediaDescription(c.req.param('id'), description)) {
|
const { description } = result.data;
|
||||||
return c.json({ error: 'File with specified ID not found.' }, 404);
|
const upload = dittoUploads.get(id);
|
||||||
}
|
|
||||||
} catch (e) {
|
if (!upload) {
|
||||||
console.error(e);
|
return c.json({ error: 'File with specified ID not found.' }, 404);
|
||||||
return c.json({ error: 'Failed to set media description.' }, 500);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dittoUploads.set(id, {
|
||||||
|
...upload,
|
||||||
|
description,
|
||||||
|
});
|
||||||
|
|
||||||
return c.json({ message: 'ok' }, 200);
|
return c.json({ message: 'ok' }, 200);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { HTTPException } from '@hono/hono/http-exception';
|
||||||
import { NostrEvent, NSchema as n } from '@nostrify/nostrify';
|
import { NostrEvent, NSchema as n } from '@nostrify/nostrify';
|
||||||
import ISO6391 from 'iso-639-1';
|
import ISO6391 from 'iso-639-1';
|
||||||
import 'linkify-plugin-hashtag';
|
import 'linkify-plugin-hashtag';
|
||||||
|
|
@ -6,22 +7,22 @@ import { nip19 } from 'nostr-tools';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
import { type AppController } from '@/app.ts';
|
import { type AppController } from '@/app.ts';
|
||||||
import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts';
|
|
||||||
import { addTag, deleteTag } from '@/utils/tags.ts';
|
|
||||||
import { asyncReplaceAll } from '@/utils/text.ts';
|
|
||||||
import { Conf } from '@/config.ts';
|
import { Conf } from '@/config.ts';
|
||||||
import { DittoDB } from '@/db/DittoDB.ts';
|
import { DittoDB } from '@/db/DittoDB.ts';
|
||||||
|
import { DittoUpload, dittoUploads } from '@/DittoUploads.ts';
|
||||||
import { DittoEvent } from '@/interfaces/DittoEvent.ts';
|
import { DittoEvent } from '@/interfaces/DittoEvent.ts';
|
||||||
import { getAncestors, getAuthor, getDescendants, getEvent } from '@/queries.ts';
|
import { getAncestors, getAuthor, getDescendants, getEvent } from '@/queries.ts';
|
||||||
import { getUnattachedMediaByIds } from '@/db/unattached-media.ts';
|
import { addTag, deleteTag } from '@/utils/tags.ts';
|
||||||
|
import { asyncReplaceAll } from '@/utils/text.ts';
|
||||||
import { lookupPubkey } from '@/utils/lookup.ts';
|
import { lookupPubkey } from '@/utils/lookup.ts';
|
||||||
import { renderEventAccounts } from '@/views.ts';
|
|
||||||
import { renderReblog, renderStatus } from '@/views/mastodon/statuses.ts';
|
|
||||||
import { Storages } from '@/storages.ts';
|
import { Storages } from '@/storages.ts';
|
||||||
import { hydrateEvents, purifyEvent } from '@/storages/hydrate.ts';
|
import { hydrateEvents, purifyEvent } from '@/storages/hydrate.ts';
|
||||||
import { createEvent, paginated, paginatedList, parseBody, updateListEvent } from '@/utils/api.ts';
|
import { createEvent, paginated, paginatedList, parseBody, updateListEvent } from '@/utils/api.ts';
|
||||||
import { getInvoice, getLnurl } from '@/utils/lnurl.ts';
|
import { getInvoice, getLnurl } from '@/utils/lnurl.ts';
|
||||||
import { getZapSplits } from '@/utils/zap-split.ts';
|
import { getZapSplits } from '@/utils/zap-split.ts';
|
||||||
|
import { renderEventAccounts } from '@/views.ts';
|
||||||
|
import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts';
|
||||||
|
import { renderReblog, renderStatus } from '@/views/mastodon/statuses.ts';
|
||||||
|
|
||||||
const createStatusSchema = z.object({
|
const createStatusSchema = z.object({
|
||||||
in_reply_to_id: n.id().nullish(),
|
in_reply_to_id: n.id().nullish(),
|
||||||
|
|
@ -63,7 +64,6 @@ const statusController: AppController = async (c) => {
|
||||||
const createStatusController: AppController = async (c) => {
|
const createStatusController: AppController = async (c) => {
|
||||||
const body = await parseBody(c.req.raw);
|
const body = await parseBody(c.req.raw);
|
||||||
const result = createStatusSchema.safeParse(body);
|
const result = createStatusSchema.safeParse(body);
|
||||||
const { kysely } = await DittoDB.getInstance();
|
|
||||||
const store = c.get('store');
|
const store = c.get('store');
|
||||||
|
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
|
|
@ -112,10 +112,18 @@ const createStatusController: AppController = async (c) => {
|
||||||
tags.push(['l', data.language, 'ISO-639-1']);
|
tags.push(['l', data.language, 'ISO-639-1']);
|
||||||
}
|
}
|
||||||
|
|
||||||
const media = data.media_ids?.length ? await getUnattachedMediaByIds(kysely, data.media_ids) : [];
|
const media: DittoUpload[] = (data.media_ids ?? []).map((id) => {
|
||||||
|
const upload = dittoUploads.get(id);
|
||||||
|
|
||||||
const imeta: string[][] = media.map(({ data }) => {
|
if (!upload) {
|
||||||
const values: string[] = data.map((tag) => tag.join(' '));
|
throw new HTTPException(422, { message: 'Uploaded attachment is no longer available.' });
|
||||||
|
}
|
||||||
|
|
||||||
|
return upload;
|
||||||
|
});
|
||||||
|
|
||||||
|
const imeta: string[][] = media.map(({ tags }) => {
|
||||||
|
const values: string[] = tags.map((tag) => tag.join(' '));
|
||||||
return ['imeta', ...values];
|
return ['imeta', ...values];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -165,7 +173,7 @@ const createStatusController: AppController = async (c) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const mediaUrls: string[] = media
|
const mediaUrls: string[] = media
|
||||||
.map(({ data }) => data.find(([name]) => name === 'url')?.[1])
|
.map(({ url }) => url)
|
||||||
.filter((url): url is string => Boolean(url));
|
.filter((url): url is string => Boolean(url));
|
||||||
|
|
||||||
const quoteCompat = data.quote_id ? `\n\nnostr:${nip19.noteEncode(data.quote_id)}` : '';
|
const quoteCompat = data.quote_id ? `\n\nnostr:${nip19.noteEncode(data.quote_id)}` : '';
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
export interface DittoTables {
|
export interface DittoTables {
|
||||||
nip46_tokens: NIP46TokenRow;
|
nip46_tokens: NIP46TokenRow;
|
||||||
unattached_media: UnattachedMediaRow;
|
|
||||||
author_stats: AuthorStatsRow;
|
author_stats: AuthorStatsRow;
|
||||||
event_stats: EventStatsRow;
|
event_stats: EventStatsRow;
|
||||||
pubkey_domains: PubkeyDomainRow;
|
pubkey_domains: PubkeyDomainRow;
|
||||||
|
|
@ -33,14 +32,6 @@ interface NIP46TokenRow {
|
||||||
connected_at: Date;
|
connected_at: Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface UnattachedMediaRow {
|
|
||||||
id: string;
|
|
||||||
pubkey: string;
|
|
||||||
url: string;
|
|
||||||
data: string;
|
|
||||||
uploaded_at: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface PubkeyDomainRow {
|
interface PubkeyDomainRow {
|
||||||
pubkey: string;
|
pubkey: string;
|
||||||
domain: string;
|
domain: string;
|
||||||
|
|
|
||||||
34
src/db/migrations/031_rm_unattached_media.ts
Normal file
34
src/db/migrations/031_rm_unattached_media.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
import { Kysely } from 'kysely';
|
||||||
|
|
||||||
|
export async function up(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema.dropTable('unattached_media').execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema
|
||||||
|
.createTable('unattached_media')
|
||||||
|
.addColumn('id', 'text', (c) => c.primaryKey())
|
||||||
|
.addColumn('pubkey', 'text', (c) => c.notNull())
|
||||||
|
.addColumn('url', 'text', (c) => c.notNull())
|
||||||
|
.addColumn('data', 'text', (c) => c.notNull())
|
||||||
|
.addColumn('uploaded_at', 'bigint', (c) => c.notNull())
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex('unattached_media_id')
|
||||||
|
.on('unattached_media')
|
||||||
|
.column('id')
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex('unattached_media_pubkey')
|
||||||
|
.on('unattached_media')
|
||||||
|
.column('pubkey')
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex('unattached_media_url')
|
||||||
|
.on('unattached_media')
|
||||||
|
.column('url')
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
@ -1,88 +0,0 @@
|
||||||
import { Kysely } from 'kysely';
|
|
||||||
|
|
||||||
import { DittoDB } from '@/db/DittoDB.ts';
|
|
||||||
import { DittoTables } from '@/db/DittoTables.ts';
|
|
||||||
|
|
||||||
interface UnattachedMedia {
|
|
||||||
id: string;
|
|
||||||
pubkey: string;
|
|
||||||
url: string;
|
|
||||||
/** NIP-94 tags. */
|
|
||||||
data: string[][];
|
|
||||||
uploaded_at: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Add unattached media into the database. */
|
|
||||||
async function insertUnattachedMedia(media: UnattachedMedia) {
|
|
||||||
const { kysely } = await DittoDB.getInstance();
|
|
||||||
await kysely.insertInto('unattached_media')
|
|
||||||
.values({ ...media, data: JSON.stringify(media.data) })
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
return media;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Select query for unattached media. */
|
|
||||||
function selectUnattachedMediaQuery(kysely: Kysely<DittoTables>) {
|
|
||||||
return kysely.selectFrom('unattached_media')
|
|
||||||
.select([
|
|
||||||
'unattached_media.id',
|
|
||||||
'unattached_media.pubkey',
|
|
||||||
'unattached_media.url',
|
|
||||||
'unattached_media.data',
|
|
||||||
'unattached_media.uploaded_at',
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Delete unattached media by URL. */
|
|
||||||
async function deleteUnattachedMediaByUrl(url: string) {
|
|
||||||
const { kysely } = await DittoDB.getInstance();
|
|
||||||
return kysely.deleteFrom('unattached_media')
|
|
||||||
.where('url', '=', url)
|
|
||||||
.execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Get unattached media by IDs. */
|
|
||||||
async function getUnattachedMediaByIds(kysely: Kysely<DittoTables>, ids: string[]): Promise<UnattachedMedia[]> {
|
|
||||||
if (!ids.length) return [];
|
|
||||||
|
|
||||||
const results = await selectUnattachedMediaQuery(kysely)
|
|
||||||
.where('id', 'in', ids)
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
return results.map((row) => ({
|
|
||||||
...row,
|
|
||||||
data: JSON.parse(row.data),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
async function setMediaDescription(id: string, desc = '') {
|
|
||||||
const { kysely } = await DittoDB.getInstance();
|
|
||||||
const existing = await selectUnattachedMediaQuery(kysely).where('id', '=', id).executeTakeFirst();
|
|
||||||
if (!existing) return false;
|
|
||||||
const parsed = (await JSON.parse(existing.data) as string[][]).filter((itm) => itm[0] !== 'alt');
|
|
||||||
parsed.push(['alt', desc]);
|
|
||||||
await kysely.updateTable('unattached_media')
|
|
||||||
.set({ data: JSON.stringify(parsed) })
|
|
||||||
.execute();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Delete rows as an event with media is being created. */
|
|
||||||
async function deleteAttachedMedia(pubkey: string, urls: string[]): Promise<void> {
|
|
||||||
if (!urls.length) return;
|
|
||||||
const { kysely } = await DittoDB.getInstance();
|
|
||||||
await kysely.deleteFrom('unattached_media')
|
|
||||||
.where('pubkey', '=', pubkey)
|
|
||||||
.where('url', 'in', urls)
|
|
||||||
.execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
export {
|
|
||||||
deleteAttachedMedia,
|
|
||||||
deleteUnattachedMediaByUrl,
|
|
||||||
getUnattachedMediaByIds,
|
|
||||||
insertUnattachedMedia,
|
|
||||||
setMediaDescription,
|
|
||||||
type UnattachedMedia,
|
|
||||||
};
|
|
||||||
|
|
@ -6,7 +6,6 @@ import { z } from 'zod';
|
||||||
|
|
||||||
import { Conf } from '@/config.ts';
|
import { Conf } from '@/config.ts';
|
||||||
import { DittoDB } from '@/db/DittoDB.ts';
|
import { DittoDB } from '@/db/DittoDB.ts';
|
||||||
import { deleteAttachedMedia } from '@/db/unattached-media.ts';
|
|
||||||
import { DittoEvent } from '@/interfaces/DittoEvent.ts';
|
import { DittoEvent } from '@/interfaces/DittoEvent.ts';
|
||||||
import { pipelineEventsCounter, policyEventsCounter } from '@/metrics.ts';
|
import { pipelineEventsCounter, policyEventsCounter } from '@/metrics.ts';
|
||||||
import { RelayError } from '@/RelayError.ts';
|
import { RelayError } from '@/RelayError.ts';
|
||||||
|
|
@ -61,7 +60,6 @@ async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise<void
|
||||||
handleZaps(kysely, event),
|
handleZaps(kysely, event),
|
||||||
parseMetadata(event, signal),
|
parseMetadata(event, signal),
|
||||||
generateSetEvents(event),
|
generateSetEvents(event),
|
||||||
processMedia(event),
|
|
||||||
streamOut(event),
|
streamOut(event),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
@ -164,14 +162,6 @@ async function parseMetadata(event: NostrEvent, signal: AbortSignal): Promise<vo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Delete unattached media entries that are attached to the event. */
|
|
||||||
function processMedia({ tags, pubkey, user }: DittoEvent) {
|
|
||||||
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. */
|
||||||
function isFresh(event: NostrEvent): boolean {
|
function isFresh(event: NostrEvent): boolean {
|
||||||
return eventAge(event) < Time.seconds(10);
|
return eventAge(event) < Time.seconds(10);
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import { HTTPException } from '@hono/hono/http-exception';
|
||||||
|
|
||||||
import { AppContext } from '@/app.ts';
|
import { AppContext } from '@/app.ts';
|
||||||
import { Conf } from '@/config.ts';
|
import { Conf } from '@/config.ts';
|
||||||
import { insertUnattachedMedia, UnattachedMedia } from '@/db/unattached-media.ts';
|
import { DittoUpload, dittoUploads } from '@/DittoUploads.ts';
|
||||||
|
|
||||||
interface FileMeta {
|
interface FileMeta {
|
||||||
pubkey: string;
|
pubkey: string;
|
||||||
|
|
@ -15,7 +15,7 @@ export async function uploadFile(
|
||||||
file: File,
|
file: File,
|
||||||
meta: FileMeta,
|
meta: FileMeta,
|
||||||
signal?: AbortSignal,
|
signal?: AbortSignal,
|
||||||
): Promise<UnattachedMedia> {
|
): Promise<DittoUpload> {
|
||||||
const uploader = c.get('uploader');
|
const uploader = c.get('uploader');
|
||||||
if (!uploader) {
|
if (!uploader) {
|
||||||
throw new HTTPException(500, {
|
throw new HTTPException(500, {
|
||||||
|
|
@ -36,11 +36,15 @@ export async function uploadFile(
|
||||||
tags.push(['alt', description]);
|
tags.push(['alt', description]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return insertUnattachedMedia({
|
const upload = {
|
||||||
id: crypto.randomUUID(),
|
id: crypto.randomUUID(),
|
||||||
pubkey,
|
|
||||||
url,
|
url,
|
||||||
data: tags,
|
tags,
|
||||||
uploaded_at: Date.now(),
|
pubkey,
|
||||||
});
|
uploadedAt: new Date(),
|
||||||
|
};
|
||||||
|
|
||||||
|
dittoUploads.set(upload.id, upload);
|
||||||
|
|
||||||
|
return upload;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,9 @@ import { getUrlMediaType } from '@/utils/media.ts';
|
||||||
|
|
||||||
/** Render Mastodon media attachment. */
|
/** Render Mastodon media attachment. */
|
||||||
function renderAttachment(
|
function renderAttachment(
|
||||||
media: { id?: string; data: string[][] },
|
media: { id?: string; tags: string[][] },
|
||||||
): (MastodonAttachment & { cid?: string }) | undefined {
|
): (MastodonAttachment & { cid?: string }) | undefined {
|
||||||
const { id, data: tags } = media;
|
const { id, tags } = media;
|
||||||
|
|
||||||
const url = tags.find(([name]) => name === 'url')?.[1];
|
const url = tags.find(([name]) => name === 'url')?.[1];
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -125,9 +125,9 @@ async function renderStatus(event: DittoEvent, opts: RenderStatusOpts): Promise<
|
||||||
pinned: Boolean(pinEvent),
|
pinned: Boolean(pinEvent),
|
||||||
reblog: null,
|
reblog: null,
|
||||||
application: null,
|
application: null,
|
||||||
media_attachments: media.map((m) => renderAttachment({ data: m })).filter((m): m is MastodonAttachment =>
|
media_attachments: media
|
||||||
Boolean(m)
|
.map((m) => renderAttachment({ tags: m }))
|
||||||
),
|
.filter((m): m is MastodonAttachment => Boolean(m)),
|
||||||
mentions,
|
mentions,
|
||||||
tags: [],
|
tags: [],
|
||||||
emojis: renderEmojis(event),
|
emojis: renderEmojis(event),
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue