From b1b341d3b8a947b2a07325bfa07f2060c01cadcd Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 18 May 2024 15:29:12 -0500 Subject: [PATCH] Insert media URL into text --- src/controllers/api/statuses.ts | 27 +++++++++++++-------------- src/db/unattached-media.ts | 10 ++++++++-- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/controllers/api/statuses.ts b/src/controllers/api/statuses.ts index f7a603f..e620b93 100644 --- a/src/controllers/api/statuses.ts +++ b/src/controllers/api/statuses.ts @@ -91,21 +91,14 @@ const createStatusController: AppController = async (c) => { tags.push(['subject', data.spoiler_text]); } - const viewerPubkey = await c.get('signer')?.getPublicKey(); + const media = data.media_ids?.length ? await getUnattachedMediaByIds(kysely, data.media_ids) : []; - if (data.media_ids?.length) { - const media = await getUnattachedMediaByIds(kysely, data.media_ids) - .then((media) => media.filter(({ pubkey }) => pubkey === viewerPubkey)) - .then((media) => - media.map(({ data }) => { - const tags: string[][] = JSON.parse(data); - const values: string[] = tags.map((tag) => tag.join(' ')); - return ['imeta', ...values]; - }) - ); + const imeta: string[][] = media.map(({ data }) => { + const values: string[] = data.map((tag) => tag.join(' ')); + return ['imeta', ...values]; + }); - tags.push(...media); - } + tags.push(...imeta); const pubkeys = new Set(); @@ -137,9 +130,15 @@ const createStatusController: AppController = async (c) => { tags.push(['t', match[1]]); } + const mediaUrls: string[] = media + .map(({ data }) => data.find(([name]) => name === 'url')?.[1]) + .filter((url): url is string => Boolean(url)); + + const mediaCompat: string = mediaUrls.length ? ['', '', ...mediaUrls].join('\n') : ''; + const event = await createEvent({ kind: 1, - content, + content: content + mediaCompat, tags, }, c); diff --git a/src/db/unattached-media.ts b/src/db/unattached-media.ts index 0ab46b6..0e0aeea 100644 --- a/src/db/unattached-media.ts +++ b/src/db/unattached-media.ts @@ -51,11 +51,17 @@ async function deleteUnattachedMediaByUrl(url: string) { } /** Get unattached media by IDs. */ -async function getUnattachedMediaByIds(kysely: Kysely, ids: string[]) { +async function getUnattachedMediaByIds(kysely: Kysely, ids: string[]): Promise { if (!ids.length) return []; - return await selectUnattachedMediaQuery(kysely) + + const results = await selectUnattachedMediaQuery(kysely) .where('id', 'in', ids) .execute(); + + return results.map((row) => ({ + ...row, + data: JSON.parse(row.data), + })); } /** Delete rows as an event with media is being created. */