Merge branch 'main' into zap-notification

This commit is contained in:
P. Reis 2024-09-02 09:54:45 -03:00
commit 8e8f8767b4
5 changed files with 62 additions and 5 deletions

View file

@ -29,6 +29,18 @@ const importUsers = async (
const notes = new Set<string>(); const notes = new Set<string>();
const { profilesOnly = false } = opts || {}; const { profilesOnly = false } = opts || {};
const put = async (event: NostrEvent) => {
try {
await doEvent(event);
} catch (error) {
if (error.message.includes('violates unique constraint')) {
console.warn(`Skipping existing event ${event.id}...`);
} else {
console.error(error);
}
}
};
await Promise.all(relays.map(async (relay) => { await Promise.all(relays.map(async (relay) => {
if (!relay.startsWith('wss://')) console.error(`Invalid relay url ${relay}`); if (!relay.startsWith('wss://')) console.error(`Invalid relay url ${relay}`);
const conn = new NRelay1(relay); const conn = new NRelay1(relay);
@ -49,7 +61,7 @@ const importUsers = async (
if (kind === 1 && !notes.has(event.id)) { if (kind === 1 && !notes.has(event.id)) {
// add the event to eventsDB only if it has not been found already. // add the event to eventsDB only if it has not been found already.
notes.add(event.id); notes.add(event.id);
await doEvent(event); await put(event);
return; return;
} }
@ -64,7 +76,7 @@ const importUsers = async (
for (const user in profiles) { for (const user in profiles) {
const profile = profiles[user]; const profile = profiles[user];
for (const kind in profile) { for (const kind in profile) {
await doEvent(profile[kind]); await put(profile[kind]);
} }
let name = user; let name = user;

View file

@ -53,7 +53,7 @@ import {
instanceV2Controller, instanceV2Controller,
} from '@/controllers/api/instance.ts'; } from '@/controllers/api/instance.ts';
import { markersController, updateMarkersController } from '@/controllers/api/markers.ts'; import { markersController, updateMarkersController } from '@/controllers/api/markers.ts';
import { mediaController } from '@/controllers/api/media.ts'; import { mediaController, updateMediaController } from '@/controllers/api/media.ts';
import { mutesController } from '@/controllers/api/mutes.ts'; import { mutesController } from '@/controllers/api/mutes.ts';
import { notificationsController } from '@/controllers/api/notifications.ts'; import { notificationsController } from '@/controllers/api/notifications.ts';
import { createTokenController, oauthAuthorizeController, oauthController } from '@/controllers/api/oauth.ts'; import { createTokenController, oauthAuthorizeController, oauthController } from '@/controllers/api/oauth.ts';
@ -226,6 +226,10 @@ app.delete('/api/v1/statuses/:id{[0-9a-f]{64}}', requireSigner, deleteStatusCont
app.get('/api/v1/pleroma/statuses/:id{[0-9a-f]{64}}/quotes', quotesController); app.get('/api/v1/pleroma/statuses/:id{[0-9a-f]{64}}/quotes', quotesController);
app.post('/api/v1/media', mediaController); app.post('/api/v1/media', mediaController);
app.put(
'/api/v1/media/:id{[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}}',
updateMediaController,
);
app.post('/api/v2/media', mediaController); app.post('/api/v2/media', mediaController);
app.get('/api/v1/timelines/home', requireSigner, homeTimelineController); app.get('/api/v1/timelines/home', requireSigner, homeTimelineController);

View file

@ -5,6 +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';
const mediaBodySchema = z.object({ const mediaBodySchema = z.object({
file: fileSchema, file: fileSchema,
@ -13,6 +14,10 @@ const mediaBodySchema = z.object({
focus: z.string().optional(), focus: z.string().optional(),
}); });
const mediaUpdateSchema = z.object({
description: z.string(),
});
const mediaController: AppController = async (c) => { const mediaController: AppController = async (c) => {
const pubkey = await c.get('signer')?.getPublicKey()!; const pubkey = await c.get('signer')?.getPublicKey()!;
const result = mediaBodySchema.safeParse(await parseBody(c.req.raw)); const result = mediaBodySchema.safeParse(await parseBody(c.req.raw));
@ -32,4 +37,22 @@ const mediaController: AppController = async (c) => {
} }
}; };
export { mediaController }; const updateMediaController: AppController = async (c) => {
const result = mediaUpdateSchema.safeParse(await parseBody(c.req.raw));
if (!result.success) {
return c.json({ error: 'Bad request.', schema: result.error }, 422);
}
try {
const { description } = result.data;
if (!await setMediaDescription(c.req.param('id'), description)) {
return c.json({ error: 'File with specified ID not found.' }, 404);
}
} catch (e) {
console.error(e);
return c.json({ error: 'Failed to set media description.' }, 500);
}
return c.json({ message: 'ok' }, 200);
};
export { mediaController, updateMediaController };

View file

@ -56,6 +56,18 @@ async function getUnattachedMediaByIds(kysely: Kysely<DittoTables>, ids: string[
})); }));
} }
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. */ /** Delete rows as an event with media is being created. */
async function deleteAttachedMedia(pubkey: string, urls: string[]): Promise<void> { async function deleteAttachedMedia(pubkey: string, urls: string[]): Promise<void> {
if (!urls.length) return; if (!urls.length) return;
@ -71,5 +83,6 @@ export {
deleteUnattachedMediaByUrl, deleteUnattachedMediaByUrl,
getUnattachedMediaByIds, getUnattachedMediaByIds,
insertUnattachedMedia, insertUnattachedMedia,
setMediaDescription,
type UnattachedMedia, type UnattachedMedia,
}; };

View file

@ -84,7 +84,12 @@ async function renderStatus(event: DittoEvent, opts: RenderStatusOpts): Promise<
const imeta: string[][][] = event.tags const imeta: string[][][] = event.tags
.filter(([name]) => name === 'imeta') .filter(([name]) => name === 'imeta')
.map(([_, ...entries]) => entries.map((entry) => entry.split(' '))); .map(([_, ...entries]) =>
entries.map((entry) => {
const split = entry.split(' ');
return [split[0], split.splice(1).join(' ')];
})
);
const media = imeta.length ? imeta : getMediaLinks(links); const media = imeta.length ? imeta : getMediaLinks(links);