mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 03:19:46 +00:00
Merge branch 'main' into zap-notification
This commit is contained in:
commit
8e8f8767b4
5 changed files with 62 additions and 5 deletions
|
|
@ -29,6 +29,18 @@ const importUsers = async (
|
|||
const notes = new Set<string>();
|
||||
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) => {
|
||||
if (!relay.startsWith('wss://')) console.error(`Invalid relay url ${relay}`);
|
||||
const conn = new NRelay1(relay);
|
||||
|
|
@ -49,7 +61,7 @@ const importUsers = async (
|
|||
if (kind === 1 && !notes.has(event.id)) {
|
||||
// add the event to eventsDB only if it has not been found already.
|
||||
notes.add(event.id);
|
||||
await doEvent(event);
|
||||
await put(event);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -64,7 +76,7 @@ const importUsers = async (
|
|||
for (const user in profiles) {
|
||||
const profile = profiles[user];
|
||||
for (const kind in profile) {
|
||||
await doEvent(profile[kind]);
|
||||
await put(profile[kind]);
|
||||
}
|
||||
|
||||
let name = user;
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ import {
|
|||
instanceV2Controller,
|
||||
} from '@/controllers/api/instance.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 { notificationsController } from '@/controllers/api/notifications.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.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.get('/api/v1/timelines/home', requireSigner, homeTimelineController);
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import { fileSchema } from '@/schema.ts';
|
|||
import { parseBody } from '@/utils/api.ts';
|
||||
import { renderAttachment } from '@/views/mastodon/attachments.ts';
|
||||
import { uploadFile } from '@/utils/upload.ts';
|
||||
import { setMediaDescription } from '@/db/unattached-media.ts';
|
||||
|
||||
const mediaBodySchema = z.object({
|
||||
file: fileSchema,
|
||||
|
|
@ -13,6 +14,10 @@ const mediaBodySchema = z.object({
|
|||
focus: z.string().optional(),
|
||||
});
|
||||
|
||||
const mediaUpdateSchema = z.object({
|
||||
description: z.string(),
|
||||
});
|
||||
|
||||
const mediaController: AppController = async (c) => {
|
||||
const pubkey = await c.get('signer')?.getPublicKey()!;
|
||||
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 };
|
||||
|
|
|
|||
|
|
@ -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. */
|
||||
async function deleteAttachedMedia(pubkey: string, urls: string[]): Promise<void> {
|
||||
if (!urls.length) return;
|
||||
|
|
@ -71,5 +83,6 @@ export {
|
|||
deleteUnattachedMediaByUrl,
|
||||
getUnattachedMediaByIds,
|
||||
insertUnattachedMedia,
|
||||
setMediaDescription,
|
||||
type UnattachedMedia,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -84,7 +84,12 @@ async function renderStatus(event: DittoEvent, opts: RenderStatusOpts): Promise<
|
|||
|
||||
const imeta: string[][][] = event.tags
|
||||
.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);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue