Insert media URL into text

This commit is contained in:
Alex Gleason 2024-05-18 15:29:12 -05:00
parent 91ea4577f1
commit b1b341d3b8
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 21 additions and 16 deletions

View File

@ -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(' '));
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<string>();
@ -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);

View File

@ -51,11 +51,17 @@ async function deleteUnattachedMediaByUrl(url: string) {
}
/** Get unattached media by IDs. */
async function getUnattachedMediaByIds(kysely: Kysely<DittoTables>, ids: string[]) {
async function getUnattachedMediaByIds(kysely: Kysely<DittoTables>, ids: string[]): Promise<UnattachedMedia[]> {
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. */