refactor(updateCredentialsController): stop overwriting kind 0 unnecessarily

This commit is contained in:
P. Reis 2024-12-03 14:33:04 -03:00
parent b45fcdde69
commit 30d7f1a053

View file

@ -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,12 +288,19 @@ 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(
const keys = Object.keys(result.data);
let event: NostrEvent;
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);
@ -341,6 +348,7 @@ const updateCredentialsController: AppController = async (c) => {
},
c,
);
}
const settingsStore = result.data.pleroma_settings_store;
const account = await renderAccount(event, { withSource: true, settingsStore });