From 93d8e0b22bdf877bf3c7beee8344b73cbe006df8 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 11 Sep 2023 18:02:17 -0500 Subject: [PATCH] accounts: support updating avatar and banner --- src/controllers/api/accounts.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index 211eb77..695c707 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -11,6 +11,7 @@ import { paginated, paginationSchema, parseBody } from '@/utils/web.ts'; import { createEvent } from '@/utils/web.ts'; import { renderEventAccounts } from '@/views.ts'; import { insertUser } from '@/db/users.ts'; +import { uploadFile } from '@/upload.ts'; const usernameSchema = z .string().min(1).max(30) @@ -171,13 +172,24 @@ const updateCredentialsController: AppController = async (c) => { } const author = await getAuthor(pubkey); - if (!author) { - return c.json({ error: 'Could not find user.' }, 404); - } + const meta = author ? jsonMetaContentSchema.parse(author.content) : {}; - const meta = jsonMetaContentSchema.parse(author.content); - meta.name = result.data.display_name ?? meta.name; - meta.about = result.data.note ?? meta.about; + const { + avatar: avatarFile, + header: headerFile, + display_name, + note, + } = result.data; + + const [avatar, header] = await Promise.all([ + avatarFile ? uploadFile(avatarFile, { pubkey }) : undefined, + headerFile ? uploadFile(headerFile, { pubkey }) : undefined, + ]); + + meta.name = display_name ?? meta.name; + meta.about = note ?? meta.about; + meta.picture = avatar?.url ?? meta.picture; + meta.banner = header?.url ?? meta.banner; const event = await createEvent({ kind: 0,