From 388713a3c690e237f09c3b624ffb2097b8020933 Mon Sep 17 00:00:00 2001 From: diced Date: Wed, 11 Jan 2023 21:33:01 -0800 Subject: [PATCH] feat: new embed method --- .../20230111055303_embed/migration.sql | 13 +++++ prisma/schema.prisma | 6 +-- src/components/pages/Manage/index.tsx | 54 +++++++++++++------ src/lib/middleware/withZipline.ts | 16 ++++-- src/pages/api/user/[id].ts | 16 +----- src/pages/api/user/index.ts | 20 ++----- src/pages/view/[id].tsx | 10 ++-- src/scripts/set-user.ts | 4 +- 8 files changed, 76 insertions(+), 63 deletions(-) create mode 100644 prisma/migrations/20230111055303_embed/migration.sql diff --git a/prisma/migrations/20230111055303_embed/migration.sql b/prisma/migrations/20230111055303_embed/migration.sql new file mode 100644 index 0000000..0b61dcc --- /dev/null +++ b/prisma/migrations/20230111055303_embed/migration.sql @@ -0,0 +1,13 @@ +/* + Warnings: + + - You are about to drop the column `embedColor` on the `User` table. All the data in the column will be lost. + - You are about to drop the column `embedSiteName` on the `User` table. All the data in the column will be lost. + - You are about to drop the column `embedTitle` on the `User` table. All the data in the column will be lost. + +*/ +-- AlterTable +ALTER TABLE "User" DROP COLUMN "embedColor", +DROP COLUMN "embedSiteName", +DROP COLUMN "embedTitle", +ADD COLUMN "embed" JSONB NOT NULL DEFAULT '{}'; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 4dd807a..cf39d49 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -16,9 +16,7 @@ model User { administrator Boolean @default(false) superAdmin Boolean @default(false) systemTheme String @default("system") - embedTitle String? - embedColor String @default("#2f3136") - embedSiteName String? @default("{image.file} • {user.name}") + embed Json @default("{}") ratelimit DateTime? totpSecret String? domains String[] @@ -48,7 +46,7 @@ model Image { password String? invisible InvisibleImage? format ImageFormat @default(RANDOM) - user User? @relation(fields: [userId], references: [id], onDelete: SetNull) + user User? @relation(fields: [userId], references: [id], onDelete: SetNull) userId Int? } diff --git a/src/components/pages/Manage/index.tsx b/src/components/pages/Manage/index.tsx index 4eab265..5743845 100644 --- a/src/components/pages/Manage/index.tsx +++ b/src/components/pages/Manage/index.tsx @@ -8,6 +8,7 @@ import { Group, Image, PasswordInput, + SimpleGrid, Space, Text, TextInput, @@ -139,9 +140,10 @@ export default function Manage({ oauth_registration, oauth_providers: raw_oauth_ initialValues: { username: user.username, password: '', - embedTitle: user.embedTitle ?? '', - embedColor: user.embedColor, - embedSiteName: user.embedSiteName ?? '', + embedTitle: user.embed?.title ?? null, + embedColor: user.embed?.color ?? '', + embedSiteName: user.embed?.siteName ?? null, + embedDescription: user.embed?.description ?? null, domains: user.domains.join(','), }, }); @@ -149,9 +151,12 @@ export default function Manage({ oauth_registration, oauth_providers: raw_oauth_ const onSubmit = async (values) => { const cleanUsername = values.username.trim(); const cleanPassword = values.password.trim(); - const cleanEmbedTitle = values.embedTitle.trim(); - const cleanEmbedColor = values.embedColor.trim(); - const cleanEmbedSiteName = values.embedSiteName.trim(); + const cleanEmbed = { + title: values.embedTitle ? values.embedTitle.trim() : null, + color: values.embedColor !== '' ? values.embedColor.trim() : null, + siteName: values.embedSiteName ? values.embedSiteName.trim() : null, + description: values.embedDescription ? values.embedDescription.trim() : null, + }; if (cleanUsername === '') return form.setFieldError('username', "Username can't be nothing"); @@ -166,13 +171,11 @@ export default function Manage({ oauth_registration, oauth_providers: raw_oauth_ const data = { username: cleanUsername, password: cleanPassword === '' ? null : cleanPassword, - embedTitle: cleanEmbedTitle === '' ? null : cleanEmbedTitle, - embedColor: cleanEmbedColor === '' ? null : cleanEmbedColor, - embedSiteName: cleanEmbedSiteName === '' ? null : cleanEmbedSiteName, domains: values.domains .split(/\s?,\s?/) .map((x) => x.trim()) .filter((x) => x !== ''), + embed: cleanEmbed, }; const newUser = await useFetch('/api/user', 'PATCH', data); @@ -405,14 +408,31 @@ export default function Manage({ oauth_registration, oauth_providers: raw_oauth_ my='sm' {...form.getInputProps('password')} /> - - - + + + + + + + + Promise; @@ -166,7 +176,7 @@ export const withZipline = include: { oauth: true }, }); - if (user) return user; + if (user) return user as UserExtended; } const userId = req.getCookie('user'); @@ -182,7 +192,7 @@ export const withZipline = }); if (!user) return null; - return user; + return user as UserExtended; } catch (e) { Logger.get('withZipline').debug(e.message); if (e.code && e.code === 'ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH') { diff --git a/src/pages/api/user/[id].ts b/src/pages/api/user/[id].ts index 8506c5f..e871410 100644 --- a/src/pages/api/user/[id].ts +++ b/src/pages/api/user/[id].ts @@ -101,22 +101,10 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) { data: { avatar: req.body.avatar }, }); - if (req.body.embedTitle) + if (req.body.embed) await prisma.user.update({ where: { id: target.id }, - data: { embedTitle: req.body.embedTitle }, - }); - - if (req.body.embedColor) - await prisma.user.update({ - where: { id: target.id }, - data: { embedColor: req.body.embedColor }, - }); - - if (req.body.embedSiteName) - await prisma.user.update({ - where: { id: target.id }, - data: { embedSiteName: req.body.embedSiteName }, + data: { embed: req.body.embed }, }); if (req.body.systemTheme) diff --git a/src/pages/api/user/index.ts b/src/pages/api/user/index.ts index d432055..bd54618 100644 --- a/src/pages/api/user/index.ts +++ b/src/pages/api/user/index.ts @@ -165,22 +165,10 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) { data: { avatar: req.body.avatar }, }); - if (req.body.embedTitle) + if (req.body.embed) await prisma.user.update({ where: { id: user.id }, - data: { embedTitle: req.body.embedTitle }, - }); - - if (req.body.embedColor) - await prisma.user.update({ - where: { id: user.id }, - data: { embedColor: req.body.embedColor }, - }); - - if (req.body.embedSiteName) - await prisma.user.update({ - where: { id: user.id }, - data: { embedSiteName: req.body.embedSiteName }, + data: { embed: req.body.embed }, }); if (req.body.systemTheme) @@ -222,9 +210,7 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) { }, select: { administrator: true, - embedColor: true, - embedTitle: true, - embedSiteName: true, + embed: true, id: true, images: false, password: false, diff --git a/src/pages/view/[id].tsx b/src/pages/view/[id].tsx index 4d9f0ff..77865ce 100644 --- a/src/pages/view/[id].tsx +++ b/src/pages/view/[id].tsx @@ -75,16 +75,16 @@ export default function EmbeddedFile({ {image.embed && ( <> - {user.embedSiteName && ( + {user.embed.siteName && ( )} - {user.embedTitle && ( - + {user.embed.title && ( + )} - + )} {image.mimetype.startsWith('image') && ( diff --git a/src/scripts/set-user.ts b/src/scripts/set-user.ts index 4c4d742..e42d80f 100644 --- a/src/scripts/set-user.ts +++ b/src/scripts/set-user.ts @@ -11,9 +11,7 @@ const SUPPORTED_FIELDS = [ 'token', 'superAdmin', 'systemTheme', - 'embedTitle', - 'embedColor', - 'embedSiteName', + 'embed', 'ratelimit', 'domains', ];