mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
refactor(updateCredentialsController): stop overwriting kind 0 unnecessarily
This commit is contained in:
parent
b45fcdde69
commit
30d7f1a053
1 changed files with 52 additions and 44 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { NostrFilter, NSchema as n } from '@nostrify/nostrify';
|
||||
import { NostrEvent, NostrFilter, NSchema as n } from '@nostrify/nostrify';
|
||||
import { nip19 } from 'nostr-tools';
|
||||
import { z } from 'zod';
|
||||
|
||||
|
|
@ -288,59 +288,67 @@ const updateCredentialsController: AppController = async (c) => {
|
|||
const pubkey = await signer.getPublicKey();
|
||||
const body = await parseBody(c.req.raw);
|
||||
const result = updateCredentialsSchema.safeParse(body);
|
||||
const store = await Storages.db();
|
||||
|
||||
if (!result.success) {
|
||||
return c.json(result.error, 422);
|
||||
}
|
||||
|
||||
const event = await updateEvent(
|
||||
{ kinds: [0], authors: [pubkey], limit: 1 },
|
||||
async (prev) => {
|
||||
const meta = n.json().pipe(metadataSchema).catch({}).parse(prev.content);
|
||||
const {
|
||||
avatar: avatarFile,
|
||||
header: headerFile,
|
||||
display_name,
|
||||
fields_attributes,
|
||||
note,
|
||||
nip05,
|
||||
lud16,
|
||||
website,
|
||||
bot,
|
||||
} = result.data;
|
||||
const keys = Object.keys(result.data);
|
||||
let event: NostrEvent;
|
||||
|
||||
const [avatar, header] = await Promise.all([
|
||||
avatarFile ? uploadFile(c, avatarFile, { pubkey }) : undefined,
|
||||
headerFile ? uploadFile(c, headerFile, { pubkey }) : undefined,
|
||||
]);
|
||||
if (keys.length === 1 && keys[0] === 'pleroma_settings_store') {
|
||||
event = (await store.query([{ kinds: [0], authors: [pubkey] }]))[0];
|
||||
} else {
|
||||
event = await updateEvent(
|
||||
{ kinds: [0], authors: [pubkey], limit: 1 },
|
||||
async (prev) => {
|
||||
const meta = n.json().pipe(metadataSchema).catch({}).parse(prev.content);
|
||||
const {
|
||||
avatar: avatarFile,
|
||||
header: headerFile,
|
||||
display_name,
|
||||
fields_attributes,
|
||||
note,
|
||||
nip05,
|
||||
lud16,
|
||||
website,
|
||||
bot,
|
||||
} = result.data;
|
||||
|
||||
meta.name = display_name ?? meta.name;
|
||||
meta.about = note ?? meta.about;
|
||||
meta.picture = avatar?.url ?? meta.picture;
|
||||
meta.banner = header?.url ?? meta.banner;
|
||||
meta.nip05 = nip05 ?? meta.nip05;
|
||||
meta.lud16 = lud16 ?? meta.lud16;
|
||||
meta.website = website ?? meta.website;
|
||||
meta.bot = bot ?? meta.bot;
|
||||
const [avatar, header] = await Promise.all([
|
||||
avatarFile ? uploadFile(c, avatarFile, { pubkey }) : undefined,
|
||||
headerFile ? uploadFile(c, headerFile, { pubkey }) : undefined,
|
||||
]);
|
||||
|
||||
if (avatarFile === '') delete meta.picture;
|
||||
if (headerFile === '') delete meta.banner;
|
||||
if (nip05 === '') delete meta.nip05;
|
||||
if (lud16 === '') delete meta.lud16;
|
||||
if (website === '') delete meta.website;
|
||||
meta.name = display_name ?? meta.name;
|
||||
meta.about = note ?? meta.about;
|
||||
meta.picture = avatar?.url ?? meta.picture;
|
||||
meta.banner = header?.url ?? meta.banner;
|
||||
meta.nip05 = nip05 ?? meta.nip05;
|
||||
meta.lud16 = lud16 ?? meta.lud16;
|
||||
meta.website = website ?? meta.website;
|
||||
meta.bot = bot ?? meta.bot;
|
||||
|
||||
if (fields_attributes) {
|
||||
meta.fields = fields_attributes.map(({ name, value }) => [name, value]);
|
||||
}
|
||||
if (avatarFile === '') delete meta.picture;
|
||||
if (headerFile === '') delete meta.banner;
|
||||
if (nip05 === '') delete meta.nip05;
|
||||
if (lud16 === '') delete meta.lud16;
|
||||
if (website === '') delete meta.website;
|
||||
|
||||
return {
|
||||
kind: 0,
|
||||
content: JSON.stringify(meta),
|
||||
tags: [],
|
||||
};
|
||||
},
|
||||
c,
|
||||
);
|
||||
if (fields_attributes) {
|
||||
meta.fields = fields_attributes.map(({ name, value }) => [name, value]);
|
||||
}
|
||||
|
||||
return {
|
||||
kind: 0,
|
||||
content: JSON.stringify(meta),
|
||||
tags: [],
|
||||
};
|
||||
},
|
||||
c,
|
||||
);
|
||||
}
|
||||
|
||||
const settingsStore = result.data.pleroma_settings_store;
|
||||
const account = await renderAccount(event, { withSource: true, settingsStore });
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue