Skip to content

Commit

Permalink
web: unable to change simping waifu
Browse files Browse the repository at this point in the history
  • Loading branch information
leomotors committed Nov 18, 2023
1 parent d8c1549 commit 8ed72bc
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 0 deletions.
10 changes: 10 additions & 0 deletions apps/web/src/lib/server/diffDays.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function diffDays(begin: Date, end: Date) {
const days =
Math.round((end.getTime() - begin.getTime()) / (1000 * 3600 * 24)) + 1;

return {
begin,
end,
days,
};
}
30 changes: 30 additions & 0 deletions apps/web/src/lib/server/promoteWaifu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { prisma } from "@waifu-bot/database";

import { diffDays } from "./diffDays";
import { getConfig } from "./getConfig";

export async function promoteWaifu(id: string) {
const now = new Date();
const config = await getConfig();

// Add Simp Interval to current waifu as memorial
await prisma.simpInterval.create({
data: {
waifu: {
connect: {
id: config.currentWaifuId,
},
},
...diffDays(config.simpingSince, now),
},
});

// Promote new Waifu to Production
await prisma.configuration.update({
where: { id: 0 },
data: {
currentWaifuId: Number(id),
simpingSince: now,
},
});
}
6 changes: 6 additions & 0 deletions apps/web/src/routes/(user)/admin/waifu/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@
<button
class="bg-pink-100 disabled:bg-slate-100"
disabled={data.user.role !== "SUPERADMIN"}
on:click={() =>
goto(`/admin/waifu/confirm?action=simp&id=${currentWaifu.id}`)}
>
<HeartFill class="h-6 w-6" />
<p>SIMP this Waifu</p>
Expand All @@ -165,6 +167,10 @@
<button
class="bg-pink-100 disabled:bg-slate-100"
disabled={data.user.role !== "SUPERADMIN"}
on:click={() =>
goto(
`/admin/waifu/confirm?action=resync&id=${currentWaifu.id}`,
)}
>
<ArrowRepeat class="h-6 w-6" />
<p>Resync to Discord</p>
Expand Down
79 changes: 79 additions & 0 deletions apps/web/src/routes/(user)/admin/waifu/confirm/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { prisma } from "@waifu-bot/database";

import { error, redirect } from "@sveltejs/kit";

import { getConfig } from "$lib/server/getConfig";
import { promoteWaifu } from "$lib/server/promoteWaifu";

import { z } from "zod";

import type { Actions, PageServerLoad } from "./$types";

const schema = z.object({
action: z.union([z.literal("simp"), z.literal("resync")]),
id: z.string().regex(/^\d+$/),
});

export const load = (async ({ parent, url }) => {
const { user } = await parent();

if (user.role !== "SUPERADMIN") {
throw error(403, "Forbidden");
}

const searchParams = new URLSearchParams(url.search);
const result = schema.safeParse(Object.fromEntries(searchParams));

if (!result.success) {
throw error(400, `Bad Request: ${result.error.message}`);
}

const { action, id } = result.data;

const config = await getConfig();

if (action === "resync" && config.currentWaifuId !== +id) {
throw error(400, "You seem to try to resync wrong waifu");
}

const waifu = await prisma.waifu.findUnique({
where: {
id: Number(id),
},
select: {
shortNameJa: true,
},
});

if (!waifu) {
throw error(404, "Not Found");
}

return {
action,
id,
waifuName: waifu.shortNameJa,
};
}) satisfies PageServerLoad;

export const actions = {
default: async ({ request }) => {
const formData = await request.formData();
const id = formData.get("id");
const action = formData.get("action");

const result = schema.safeParse({ id, action });

if (!result.success) {
throw error(422, `zod error: ${result.error.message}`);
}

if (result.data.action === "simp") {
await promoteWaifu(result.data.id);
}

// TODO Fire webhook

throw redirect(303, "/admin/waifu");
},
} satisfies Actions;
43 changes: 43 additions & 0 deletions apps/web/src/routes/(user)/admin/waifu/confirm/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<script lang="ts">
import type { PageData } from "./$types";
export let data: PageData;
$: ({ action, id, waifuName } = data);
const actions = {
simp: {
title: "Simping new Waifu",
message: (waifuName) => `This will change simping waifu to ${waifuName}`,
},
resync: {
title: "Resync data to Discord",
message: (waifuName) =>
`This will resync Discord data for current waifu (Should be ${waifuName})`,
},
} satisfies Record<
typeof action,
{ title: string; message: (name: string) => string }
>;
$: ({ title, message } = actions[action]);
</script>

<main class="flex flex-col items-center gap-4">
<h1 class="text-4xl">{title}</h1>

<p class="text-xl text-red-500">Warning: Changing username is rate limited</p>

<p class="text-xl">{message(waifuName)} (ID = {id})</p>

<form method="POST">
<input type="hidden" name="id" value={id} />
<input type="hidden" name="action" value={action} />

<button
type="submit"
class="rounded-lg bg-red-500 p-2 text-white transition-colors hover:bg-red-400"
>
Confirm
</button>
</form>
</main>

0 comments on commit 8ed72bc

Please sign in to comment.