media: pukey --> pubkey, fix adding media tags to event

This commit is contained in:
Alex Gleason 2023-09-09 20:12:47 -05:00
parent 0b867afd8e
commit cf9a754b02
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
6 changed files with 42 additions and 23 deletions

View File

@ -30,8 +30,8 @@ const mediaController: AppController = async (c) => {
const url = new URL(`/ipfs/${cid}`, Conf.mediaDomain).toString(); const url = new URL(`/ipfs/${cid}`, Conf.mediaDomain).toString();
await insertUnattachedMedia({ const media = await insertUnattachedMedia({
pukey: c.get('pubkey')!, pubkey: c.get('pubkey')!,
url, url,
data: { data: {
name: file.name, name: file.name,
@ -42,7 +42,7 @@ const mediaController: AppController = async (c) => {
}); });
return c.json({ return c.json({
id: cid, id: media.id,
type: getAttachmentType(file.type), type: getAttachmentType(file.type),
url, url,
preview_url: url, preview_url: url,

View File

@ -1,10 +1,10 @@
import { type AppController } from '@/app.ts'; import { type AppController } from '@/app.ts';
import { Conf } from '@/config.ts';
import { type Event, ISO6391, z } from '@/deps.ts'; import { type Event, ISO6391, z } from '@/deps.ts';
import { getAncestors, getDescendants, getEvent } from '@/queries.ts'; import { getAncestors, getDescendants, getEvent } from '@/queries.ts';
import { toStatus } from '@/transformers/nostr-to-mastoapi.ts'; import { toStatus } from '@/transformers/nostr-to-mastoapi.ts';
import { createEvent, paginationSchema, parseBody } from '@/utils/web.ts'; import { createEvent, paginationSchema, parseBody } from '@/utils/web.ts';
import { renderEventAccounts } from '@/views.ts'; import { renderEventAccounts } from '@/views.ts';
import { getUnattachedMediaByIds } from '@/db/unattached-media.ts';
const createStatusSchema = z.object({ const createStatusSchema = z.object({
in_reply_to_id: z.string().regex(/[0-9a-f]{64}/).nullish(), in_reply_to_id: z.string().regex(/[0-9a-f]{64}/).nullish(),
@ -69,9 +69,12 @@ const createStatusController: AppController = async (c) => {
tags.push(['subject', data.spoiler_text]); tags.push(['subject', data.spoiler_text]);
} }
for (const cid of data.media_ids ?? []) { if (data.media_ids?.length) {
const url = new URL(`/ipfs/${cid}`, Conf.mediaDomain).toString(); const media = await getUnattachedMediaByIds(data.media_ids)
tags.push(['media', url]); .then((media) => media.filter(({ pubkey }) => pubkey === c.get('pubkey')))
.then((media) => media.map(({ url, data }) => ['media', url, data]));
tags.push(...media);
} }
const event = await createEvent({ const event = await createEvent({

View File

@ -49,7 +49,7 @@ interface RelayRow {
interface UnattachedMediaRow { interface UnattachedMediaRow {
id: string; id: string;
pukey: string; pubkey: string;
url: string; url: string;
data: string; data: string;
uploaded_at: Date; uploaded_at: Date;

View File

@ -4,7 +4,7 @@ export async function up(db: Kysely<any>): Promise<void> {
await db.schema await db.schema
.createTable('unattached_media') .createTable('unattached_media')
.addColumn('id', 'text', (c) => c.primaryKey()) .addColumn('id', 'text', (c) => c.primaryKey())
.addColumn('pukey', 'text', (c) => c.notNull()) .addColumn('pubkey', 'text', (c) => c.notNull())
.addColumn('url', 'text', (c) => c.notNull()) .addColumn('url', 'text', (c) => c.notNull())
.addColumn('data', 'text', (c) => c.notNull()) .addColumn('data', 'text', (c) => c.notNull())
.addColumn('uploaded_at', 'datetime', (c) => c.notNull().defaultTo(sql`CURRENT_TIMESTAMP`)) .addColumn('uploaded_at', 'datetime', (c) => c.notNull().defaultTo(sql`CURRENT_TIMESTAMP`))
@ -17,9 +17,9 @@ export async function up(db: Kysely<any>): Promise<void> {
.execute(); .execute();
await db.schema await db.schema
.createIndex('unattached_media_pukey') .createIndex('unattached_media_pubkey')
.on('unattached_media') .on('unattached_media')
.column('pukey') .column('pubkey')
.execute(); .execute();
await db.schema await db.schema

View File

@ -3,7 +3,7 @@ import { uuid62 } from '@/deps.ts';
interface UnattachedMedia { interface UnattachedMedia {
id: string; id: string;
pukey: string; pubkey: string;
url: string; url: string;
data: { data: {
name?: string; name?: string;
@ -16,15 +16,18 @@ interface UnattachedMedia {
uploaded_at: Date; uploaded_at: Date;
} }
function insertUnattachedMedia(media: Omit<UnattachedMedia, 'id' | 'uploaded_at'>) { async function insertUnattachedMedia(media: Omit<UnattachedMedia, 'id' | 'uploaded_at'>) {
return db.insertInto('unattached_media') const result = {
.values({ id: uuid62.v4(),
id: uuid62.v4(), uploaded_at: new Date(),
uploaded_at: new Date(), ...media,
...media, };
data: JSON.stringify(media.data),
}) await db.insertInto('unattached_media')
.values({ ...result, data: JSON.stringify(media.data) })
.execute(); .execute();
return result;
} }
/** Find attachments that exist but aren't attached to any events. */ /** Find attachments that exist but aren't attached to any events. */
@ -32,7 +35,7 @@ function getUnattachedMedia(until: Date) {
return db.selectFrom('unattached_media') return db.selectFrom('unattached_media')
.select([ .select([
'unattached_media.id', 'unattached_media.id',
'unattached_media.pukey', 'unattached_media.pubkey',
'unattached_media.url', 'unattached_media.url',
'unattached_media.data', 'unattached_media.data',
'unattached_media.uploaded_at', 'unattached_media.uploaded_at',
@ -48,4 +51,17 @@ function deleteUnattachedMediaByUrl(url: string) {
.execute(); .execute();
} }
export { deleteUnattachedMediaByUrl, getUnattachedMedia, insertUnattachedMedia }; function getUnattachedMediaByIds(ids: string[]) {
return db.selectFrom('unattached_media')
.select([
'unattached_media.id',
'unattached_media.pubkey',
'unattached_media.url',
'unattached_media.data',
'unattached_media.uploaded_at',
])
.where('id', 'in', ids)
.execute();
}
export { deleteUnattachedMediaByUrl, getUnattachedMedia, getUnattachedMediaByIds, insertUnattachedMedia };

View File

@ -143,7 +143,7 @@ async function toStatus(event: Event<1>, viewerPubkey?: string) {
const media = event.tags const media = event.tags
.filter((tag) => tag[0] === 'media') .filter((tag) => tag[0] === 'media')
.map((tag) => ({ url: tag[1], mimeType: tag[2] || undefined })); .map(([_, url]) => ({ url }));
return { return {
id: event.id, id: event.id,