-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
apps/web/src/routes/(user)/admin/waifu/confirm/+page.server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
apps/web/src/routes/(user)/admin/waifu/confirm/+page.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |