diff --git a/src/controllers/api/media.ts b/src/controllers/api/media.ts index 1e310e1a..7044042b 100644 --- a/src/controllers/api/media.ts +++ b/src/controllers/api/media.ts @@ -1,11 +1,12 @@ import { AppController } from '@/app.ts'; import { Conf } from '@/config.ts'; -import { insertUnattachedMedia } from '@/db/unattached-media.ts'; +import { getUnattachedMediaForUser, insertUnattachedMedia } from '@/db/unattached-media.ts'; import { z } from '@/deps.ts'; import { fileSchema } from '@/schema.ts'; import { configUploader as uploader } from '@/uploaders/config.ts'; import { parseBody } from '@/utils/web.ts'; import { renderAttachment } from '@/views/attachment.ts'; +import { jsonMediaDataSchema } from '@/schemas/nostr.ts'; const uploadSchema = fileSchema .refine((file) => !!file.type, 'File type is required.') @@ -49,4 +50,25 @@ const mediaController: AppController = async (c) => { } }; -export { mediaController }; +const updateMediaController: AppController = async (c) => { + const pubkey = c.get('pubkey')!; + const mediaId = c.req.param('id'); + + const media = await getUnattachedMediaForUser(pubkey, mediaId); + + if (!media) { + return c.json({ error: 'Media not found.' }, 404); + } + + const result = mediaBodySchema.safeParse(await parseBody(c.req.raw)); + + if (!result.success) { + return c.json({ error: 'Bad request.', schema: result.error }, 422); + } + + const data = jsonMediaDataSchema.parse(media.data); + + +}; + +export { mediaController, updateMediaController }; diff --git a/src/db/unattached-media.ts b/src/db/unattached-media.ts index ae9d8826..c13bc536 100644 --- a/src/db/unattached-media.ts +++ b/src/db/unattached-media.ts @@ -59,6 +59,14 @@ function getUnattachedMediaByIds(ids: string[]) { .execute(); } +/** Get a single media with an ID and a user. */ +function getUnattachedMediaForUser(pubkey: string, id: string) { + return selectUnattachedMediaQuery() + .where('pubkey', '=', pubkey) + .where('id', '=', id) + .executeTakeFirst(); +} + /** Delete rows as an event with media is being created. */ function deleteAttachedMedia(pubkey: string, urls: string[]) { return db.deleteFrom('unattached_media') @@ -73,5 +81,6 @@ export { getUnattachedMedia, getUnattachedMediaByIds, insertUnattachedMedia, + getUnattachedMediaForUser, type UnattachedMedia, };