Make verify_credentials and update_credentials return a consistent CredentialAccount object

This commit is contained in:
Alex Gleason 2024-10-11 16:25:29 -05:00
parent fab0c8a409
commit c5ddd2ebb7
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 34 additions and 28 deletions

View file

@ -51,7 +51,7 @@ const verifyCredentialsController: AppController = async (c) => {
const store = await Storages.db(); const store = await Storages.db();
const [author, [settingsStore], [captcha]] = await Promise.all([ const [author, [settingsEvent], [captcha]] = await Promise.all([
getAuthor(pubkey, { signal: AbortSignal.timeout(5000) }), getAuthor(pubkey, { signal: AbortSignal.timeout(5000) }),
store.query([{ store.query([{
@ -71,21 +71,16 @@ const verifyCredentialsController: AppController = async (c) => {
}]), }]),
]); ]);
let settingsStore: Record<string, unknown> | undefined;
try {
settingsStore = n.json().pipe(z.record(z.string(), z.unknown())).parse(settingsEvent?.content);
} catch {
// Do nothing
}
const account = author const account = author
? await renderAccount(author, { withSource: true }) ? await renderAccount(author, { withSource: true, settingsStore, captcha })
: await accountFromPubkey(pubkey, { withSource: true }); : await accountFromPubkey(pubkey, { withSource: true, settingsStore, captcha });
if (settingsStore) {
try {
account.pleroma.settings_store = JSON.parse(settingsStore.content);
} catch {
// Ignore
}
}
if (captcha && account.source) {
account.source.ditto.captcha_solved = true;
}
return c.json(account); return c.json(account);
}; };
@ -280,7 +275,7 @@ const updateCredentialsSchema = z.object({
bot: z.boolean().optional(), bot: z.boolean().optional(),
discoverable: z.boolean().optional(), discoverable: z.boolean().optional(),
nip05: z.string().email().or(z.literal('')).optional(), nip05: z.string().email().or(z.literal('')).optional(),
pleroma_settings_store: z.unknown().optional(), pleroma_settings_store: z.record(z.string(), z.unknown()).optional(),
lud16: z.string().email().or(z.literal('')).optional(), lud16: z.string().email().or(z.literal('')).optional(),
website: z.string().url().or(z.literal('')).optional(), website: z.string().url().or(z.literal('')).optional(),
}); });
@ -290,6 +285,7 @@ const updateCredentialsController: AppController = async (c) => {
const pubkey = await signer.getPublicKey(); const pubkey = await signer.getPublicKey();
const body = await parseBody(c.req.raw); const body = await parseBody(c.req.raw);
const result = updateCredentialsSchema.safeParse(body); const result = updateCredentialsSchema.safeParse(body);
const store = await Storages.db();
if (!result.success) { if (!result.success) {
return c.json(result.error, 422); return c.json(result.error, 422);
@ -339,8 +335,17 @@ const updateCredentialsController: AppController = async (c) => {
c, c,
); );
const account = await renderAccount(event, { withSource: true }); const [captcha] = await store.query([{
kinds: [1985],
authors: [Conf.pubkey],
'#L': ['pub.ditto.captcha'],
'#l': ['solved'],
'#p': [pubkey],
limit: 1,
}]);
const settingsStore = result.data.pleroma_settings_store; const settingsStore = result.data.pleroma_settings_store;
const account = await renderAccount(event, { withSource: true, settingsStore, captcha });
if (settingsStore) { if (settingsStore) {
await createEvent({ await createEvent({
@ -350,8 +355,6 @@ const updateCredentialsController: AppController = async (c) => {
}, c); }, c);
} }
account.pleroma.settings_store = settingsStore;
return c.json(account); return c.json(account);
}; };

View file

@ -54,7 +54,7 @@ export interface MastodonAccount {
is_moderator: boolean; is_moderator: boolean;
is_suggested: boolean; is_suggested: boolean;
is_local: boolean; is_local: boolean;
settings_store: unknown; settings_store?: Record<string, unknown>;
tags: string[]; tags: string[];
}; };
nostr: { nostr: {

View file

@ -1,4 +1,4 @@
import { NSchema as n } from '@nostrify/nostrify'; import { type NostrEvent, NSchema as n } from '@nostrify/nostrify';
import { escape } from 'entities'; import { escape } from 'entities';
import { nip19, UnsignedEvent } from 'nostr-tools'; import { nip19, UnsignedEvent } from 'nostr-tools';
@ -12,16 +12,19 @@ import { faviconCache } from '@/utils/favicon.ts';
import { nostrDate, nostrNow } from '@/utils.ts'; import { nostrDate, nostrNow } from '@/utils.ts';
import { renderEmojis } from '@/views/mastodon/emojis.ts'; import { renderEmojis } from '@/views/mastodon/emojis.ts';
interface ToAccountOpts { type ToAccountOpts = {
withSource?: boolean; withSource: true;
} settingsStore: Record<string, unknown> | undefined;
captcha: NostrEvent | undefined;
} | {
withSource?: false;
};
async function renderAccount( async function renderAccount(
event: Omit<DittoEvent, 'id' | 'sig'>, event: Omit<DittoEvent, 'id' | 'sig'>,
opts: ToAccountOpts = {}, opts: ToAccountOpts = {},
signal = AbortSignal.timeout(3000), signal = AbortSignal.timeout(3000),
): Promise<MastodonAccount> { ): Promise<MastodonAccount> {
const { withSource = false } = opts;
const { pubkey } = event; const { pubkey } = event;
const names = getTagSet(event.user?.tags ?? [], 'n'); const names = getTagSet(event.user?.tags ?? [], 'n');
@ -76,7 +79,7 @@ async function renderAccount(
locked: false, locked: false,
note: about ? escape(about) : '', note: about ? escape(about) : '',
roles: [], roles: [],
source: withSource source: opts.withSource
? { ? {
fields: [], fields: [],
language: '', language: '',
@ -88,7 +91,7 @@ async function renderAccount(
nip05, nip05,
}, },
ditto: { ditto: {
captcha_solved: false, captcha_solved: Boolean(opts.captcha),
}, },
} }
: undefined, : undefined,
@ -107,7 +110,7 @@ async function renderAccount(
is_moderator: names.has('admin') || names.has('moderator'), is_moderator: names.has('admin') || names.has('moderator'),
is_suggested: names.has('suggested'), is_suggested: names.has('suggested'),
is_local: parsed05?.domain === Conf.url.host, is_local: parsed05?.domain === Conf.url.host,
settings_store: undefined as unknown, settings_store: opts.withSource ? opts.settingsStore : undefined,
tags: [...getTagSet(event.user?.tags ?? [], 't')], tags: [...getTagSet(event.user?.tags ?? [], 't')],
favicon: favicon?.toString(), favicon: favicon?.toString(),
}, },