From eb71990d8a9ac1c891073448e85f0be9dca44985 Mon Sep 17 00:00:00 2001 From: iCrawl Date: Sun, 1 Oct 2023 01:24:56 +0200 Subject: [PATCH] feat: aggregated cases --- .../src/app/api/discord/callback/route.ts | 2 +- .../src/app/api/discord/logout/route.ts | 2 +- apps/website/src/app/appeals/page.tsx | 78 ---------- apps/website/src/app/cases/[id]/page.tsx | 78 ++++++++++ apps/website/src/components/UserDisplay.tsx | 4 +- apps/yuudachi/package.json | 1 + apps/yuudachi/src/index.ts | 4 +- apps/yuudachi/src/util/api.ts | 27 ++-- yarn.lock | 138 +++++++++++++++++- 9 files changed, 234 insertions(+), 100 deletions(-) delete mode 100644 apps/website/src/app/appeals/page.tsx create mode 100644 apps/website/src/app/cases/[id]/page.tsx diff --git a/apps/website/src/app/api/discord/callback/route.ts b/apps/website/src/app/api/discord/callback/route.ts index ff8ebdf8e..865e3efc8 100644 --- a/apps/website/src/app/api/discord/callback/route.ts +++ b/apps/website/src/app/api/discord/callback/route.ts @@ -32,6 +32,6 @@ export async function GET(req: NextRequest) { }); return NextResponse.redirect( - new URL("/appeals", process.env.NODE_ENV === "development" ? `https://${req.headers.get("host")}` : req.url), + new URL("/", process.env.NODE_ENV === "development" ? `https://${req.headers.get("host")}` : req.url), ); } diff --git a/apps/website/src/app/api/discord/logout/route.ts b/apps/website/src/app/api/discord/logout/route.ts index 0bc723b71..5436644f9 100644 --- a/apps/website/src/app/api/discord/logout/route.ts +++ b/apps/website/src/app/api/discord/logout/route.ts @@ -8,6 +8,6 @@ export async function GET(req: NextRequest) { cookies().delete("discord_token"); return NextResponse.redirect( - new URL("/appeals", process.env.NODE_ENV === "development" ? `https://${req.headers.get("host")}` : req.url), + new URL("/", process.env.NODE_ENV === "development" ? `https://${req.headers.get("host")}` : req.url), ); } diff --git a/apps/website/src/app/appeals/page.tsx b/apps/website/src/app/appeals/page.tsx deleted file mode 100644 index 77d8fc916..000000000 --- a/apps/website/src/app/appeals/page.tsx +++ /dev/null @@ -1,78 +0,0 @@ -import process from "node:process"; -import { cookies } from "next/headers"; -import { redirect } from "next/navigation"; -import { UserDisplay } from "~/components/UserDisplay"; - -export default async function Page() { - const cookieStore = cookies(); - - const token = cookieStore.get("discord_token"); - if (!token) { - return ( - - Login with Discord - - ); - } - - const userData = await fetch("https://discord.com/api/v10/users/@me", { - headers: { - Authorization: `Bearer ${token.value}`, - }, - }); - - if (userData.status !== 200) { - return redirect("/api/discord/logout"); - } - - const user = await userData.json(); - - // const caseData = await fetch("https://bot.yuudachi.dev/api/cases"); - - // const cases = await caseData.json(); - - // const targetData = await fetch("https://bot.yuudachi.dev/api/users/492374435274162177"); - - // const target = await targetData.json(); - - return ( -
-
-
- You: - -
- - {/*
- Target: - -
*/} -
- - {/*
- - - - - - - - - {cases.map((c: any) => ( - - - - - ))} - -
Case idReason
{c.case_id}{c.reason}
-
*/} -
- ); -} diff --git a/apps/website/src/app/cases/[id]/page.tsx b/apps/website/src/app/cases/[id]/page.tsx new file mode 100644 index 000000000..c5556a34b --- /dev/null +++ b/apps/website/src/app/cases/[id]/page.tsx @@ -0,0 +1,78 @@ +import process from "node:process"; +import { cookies } from "next/headers"; +import { redirect } from "next/navigation"; +import { UserDisplay } from "~/components/UserDisplay"; + +export default async function Page({ params }: { params: { id: string } }) { + const cookieStore = cookies(); + + const token = cookieStore.get("discord_token"); + if (!token) { + return ( + + Login with Discord + + ); + } + + // const userData = await fetch("https://discord.com/api/v10/users/@me", { + // headers: { + // Authorization: `Bearer ${token.value}`, + // }, + // next: { revalidate: 86_400 }, + // }); + + // if (userData.status !== 200) { + // return redirect("/api/discord/logout"); + // } + + // const user = await userData.json(); + + const memberData = await fetch(`https://discord.com/api/v10/users/@me/guilds/222078108977594368/member`, { + headers: { + Authorization: `Bearer ${token.value}`, + }, + next: { revalidate: 86_400 }, + }); + + if (memberData.status !== 200) { + return redirect("/api/discord/logout"); + } + + const member = await memberData.json(); + + if (!member.roles.includes(process.env.DISCORD_STAFF_ROLE_ID)) { + return
Nah, surely not.
; + } + + const caseData = await fetch(`https://bot.yuudachi.dev/api/cases/${params.id}`, { + headers: { + Authorization: `Bearer ${process.env.JWT_TOKEN}`, + }, + }); + + const { user, cases } = await caseData.json(); + + return ( +
+
+ +
+ +
+ {cases.map((case_: any) => ( +
+
Case: {case_.case_id}
+
Reason: {case_.reason}
+
+ ))} +
+
+ ); +} diff --git a/apps/website/src/components/UserDisplay.tsx b/apps/website/src/components/UserDisplay.tsx index d3d534d25..b2c6be487 100644 --- a/apps/website/src/components/UserDisplay.tsx +++ b/apps/website/src/components/UserDisplay.tsx @@ -13,9 +13,9 @@ export function UserDisplay({ const isBannerAnimated = user.banner?.startsWith("a_"); return ( -
+
-
+
{user.banner ? ( { + try { + await request.jwtVerify(); + } catch (error) { + reply.send(error); + } + }) .register( (app, _, done) => { app.get("/", () => "Welcome to the yuudachi api."); - app.get("/users/:id", async (request) => { - const client = container.resolve(Client); + app.get("/cases/:id", async (request) => { const { id } = request.params as any; - - const user = await client.users.fetch(id); - console.log(user); - return user; - }); - app.get("/cases", async () => { + const client = container.resolve(Client); const sql = container.resolve>(kSQL); - return sql` + const user = await client.users.fetch(id); + const cases = await sql` select * from cases where guild_id = '222078108977594368' - and target_id = '492374435274162177' + and target_id = ${id} and action not in (1, 8) order by created_at desc limit 50 `; + + return { user, cases }; }); done(); diff --git a/yarn.lock b/yarn.lock index 4d3a36877..98d990999 100644 --- a/yarn.lock +++ b/yarn.lock @@ -996,7 +996,7 @@ __metadata: languageName: node linkType: hard -"@fastify/error@npm:^3.2.0": +"@fastify/error@npm:^3.0.0, @fastify/error@npm:^3.2.0": version: 3.3.0 resolution: "@fastify/error@npm:3.3.0" checksum: 202507c8c7f49922cac2f5afc82802151b0bd9c583ca1c2850bf43d0f4cd97eedb3a3388b9016da74f8a01b517a5861d1f666c506dd64fd22995e559bc139264 @@ -1022,6 +1022,19 @@ __metadata: languageName: node linkType: hard +"@fastify/jwt@npm:^7.2.1": + version: 7.2.1 + resolution: "@fastify/jwt@npm:7.2.1" + dependencies: + "@fastify/error": ^3.0.0 + "@lukeed/ms": ^2.0.0 + fast-jwt: ^3.0.0 + fastify-plugin: ^4.0.0 + steed: ^1.1.3 + checksum: 094c11b2ff3e1ebf41eedac9d64e020d0492e83b1e3125f88bc515923694bbbb60ebe0b5589916880155163cbb1327d233ce3f18a267a62b5c81b3cd53652523 + languageName: node + linkType: hard + "@fastify/sensible@npm:^5.3.0": version: 5.3.0 resolution: "@fastify/sensible@npm:5.3.0" @@ -1172,7 +1185,7 @@ __metadata: languageName: node linkType: hard -"@lukeed/ms@npm:^2.0.1": +"@lukeed/ms@npm:^2.0.0, @lukeed/ms@npm:^2.0.1": version: 2.0.1 resolution: "@lukeed/ms@npm:2.0.1" checksum: c7b46933bf7bad3e024dcbbe2ad6201392b4ed2a05a717c0ef7e96a03fb885d44f08b4b749c392cc51c2736a6a45a08c77f1863ace1c072928fbfd9908a13db3 @@ -3752,6 +3765,18 @@ __metadata: languageName: node linkType: hard +"asn1.js@npm:^5.4.1": + version: 5.4.1 + resolution: "asn1.js@npm:5.4.1" + dependencies: + bn.js: ^4.0.0 + inherits: ^2.0.1 + minimalistic-assert: ^1.0.0 + safer-buffer: ^2.1.0 + checksum: 3786a101ac6f304bd4e9a7df79549a7561950a13d4bcaec0c7790d44c80d147c1a94ba3d4e663673406064642a40b23fcd6c82a9952468e386c1a1376d747f9a + languageName: node + linkType: hard + "assertion-error@npm:^1.1.0": version: 1.1.0 resolution: "assertion-error@npm:1.1.0" @@ -4023,6 +4048,13 @@ __metadata: languageName: node linkType: hard +"bn.js@npm:^4.0.0": + version: 4.12.0 + resolution: "bn.js@npm:4.12.0" + checksum: 39afb4f15f4ea537b55eaf1446c896af28ac948fdcf47171961475724d1bb65118cca49fa6e3d67706e4790955ec0e74de584e45c8f1ef89f46c812bee5b5a12 + languageName: node + linkType: hard + "boolbase@npm:^1.0.0": version: 1.0.0 resolution: "boolbase@npm:1.0.0" @@ -5290,6 +5322,15 @@ __metadata: languageName: node linkType: hard +"ecdsa-sig-formatter@npm:^1.0.11": + version: 1.0.11 + resolution: "ecdsa-sig-formatter@npm:1.0.11" + dependencies: + safe-buffer: ^5.0.1 + checksum: 207f9ab1c2669b8e65540bce29506134613dd5f122cccf1e6a560f4d63f2732d427d938f8481df175505aad94583bcb32c688737bb39a6df0625f903d6d93c03 + languageName: node + linkType: hard + "edge-runtime@npm:2.5.1": version: 2.5.1 resolution: "edge-runtime@npm:2.5.1" @@ -6802,6 +6843,18 @@ __metadata: languageName: node linkType: hard +"fast-jwt@npm:^3.0.0": + version: 3.3.0 + resolution: "fast-jwt@npm:3.3.0" + dependencies: + "@lukeed/ms": ^2.0.1 + asn1.js: ^5.4.1 + ecdsa-sig-formatter: ^1.0.11 + mnemonist: ^0.39.5 + checksum: d2e72738d04726a1f3fdaa869dd1dd21db4d039000ef52091b6bc42af2fb43ed825314cbca779212f7261c9d885ad726110c532c5eae1cf831e52363f26d533b + languageName: node + linkType: hard + "fast-levenshtein@npm:^2.0.6": version: 2.0.6 resolution: "fast-levenshtein@npm:2.0.6" @@ -6839,6 +6892,15 @@ __metadata: languageName: node linkType: hard +"fastfall@npm:^1.5.0": + version: 1.5.1 + resolution: "fastfall@npm:1.5.1" + dependencies: + reusify: ^1.0.0 + checksum: 390312ff319439cf4f3b5daf58c6126af4354f7ac0a56f2e497f77b712b0bcf05cdf209f9abbaa55e2b49c98d078b78c32ad4e7a1bb850c44bf8a511a80ac505 + languageName: node + linkType: hard + "fastify-plugin@npm:^4.0.0, fastify-plugin@npm:^4.2.1": version: 4.5.1 resolution: "fastify-plugin@npm:4.5.1" @@ -6870,7 +6932,17 @@ __metadata: languageName: node linkType: hard -"fastq@npm:^1.6.0, fastq@npm:^1.6.1": +"fastparallel@npm:^2.2.0": + version: 2.4.1 + resolution: "fastparallel@npm:2.4.1" + dependencies: + reusify: ^1.0.4 + xtend: ^4.0.2 + checksum: dd8c26a4f4e5c5ef54e6cc0786c6c917a9f09981229a7a6a9cbc1fc21924493fe5dfc7877ff755e83cdff3172bfcacf4f71b35ff52b23acab672e2dbd4b1da83 + languageName: node + linkType: hard + +"fastq@npm:^1.3.0, fastq@npm:^1.6.0, fastq@npm:^1.6.1": version: 1.15.0 resolution: "fastq@npm:1.15.0" dependencies: @@ -6879,6 +6951,16 @@ __metadata: languageName: node linkType: hard +"fastseries@npm:^1.7.0": + version: 1.7.2 + resolution: "fastseries@npm:1.7.2" + dependencies: + reusify: ^1.0.0 + xtend: ^4.0.0 + checksum: 0fc63c4a7236491ca65b8e65d6b3ba910cf75fd1f324ceedbac2099cfb320d93ce5897a0c9f3f16e813227dfb0d1834f959b788c34b26d40d996804c68198f5f + languageName: node + linkType: hard + "fault@npm:^2.0.0": version: 2.0.1 resolution: "fault@npm:2.0.1" @@ -8040,7 +8122,7 @@ __metadata: languageName: node linkType: hard -"inherits@npm:2, inherits@npm:2.0.4, inherits@npm:^2.0.3, inherits@npm:^2.0.4": +"inherits@npm:2, inherits@npm:2.0.4, inherits@npm:^2.0.1, inherits@npm:^2.0.3, inherits@npm:^2.0.4": version: 2.0.4 resolution: "inherits@npm:2.0.4" checksum: 4a48a733847879d6cf6691860a6b1e3f0f4754176e4d71494c41f3475553768b10f84b5ce1d40fbd0e34e6bfbb864ee35858ad4dd2cf31e02fc4a154b724d7f1 @@ -10200,6 +10282,13 @@ __metadata: languageName: node linkType: hard +"minimalistic-assert@npm:^1.0.0": + version: 1.0.1 + resolution: "minimalistic-assert@npm:1.0.1" + checksum: cc7974a9268fbf130fb055aff76700d7e2d8be5f761fb5c60318d0ed010d839ab3661a533ad29a5d37653133385204c503bfac995aaa4236f4e847461ea32ba7 + languageName: node + linkType: hard + "minimatch@npm:^3.0.4, minimatch@npm:^3.0.5, minimatch@npm:^3.1.1, minimatch@npm:^3.1.2": version: 3.1.2 resolution: "minimatch@npm:3.1.2" @@ -10394,6 +10483,15 @@ __metadata: languageName: node linkType: hard +"mnemonist@npm:^0.39.5": + version: 0.39.5 + resolution: "mnemonist@npm:0.39.5" + dependencies: + obliterator: ^2.0.1 + checksum: 6669d687a434226924b2c84ee6eb7ce7d0f83dfc5caad8bcc164c73c0c11fb6d43cbe32636e710f068046f4b40a56c3032532554e93e02640aafc6ca3dd222e6 + languageName: node + linkType: hard + "mri@npm:1.2.0, mri@npm:^1.1.0": version: 1.2.0 resolution: "mri@npm:1.2.0" @@ -10964,6 +11062,13 @@ __metadata: languageName: node linkType: hard +"obliterator@npm:^2.0.1": + version: 2.0.4 + resolution: "obliterator@npm:2.0.4" + checksum: f28ad35b6d812089315f375dc3e6e5f9bebf958ebe4b10ccd471c7115cbcf595e74bdac4783ae758e5b1f47e3096427fdb37cfa7bed566b132df92ff317b9a7c + languageName: node + linkType: hard + "ofetch@npm:^1.3.3": version: 1.3.3 resolution: "ofetch@npm:1.3.3" @@ -12433,7 +12538,7 @@ __metadata: languageName: node linkType: hard -"reusify@npm:^1.0.4": +"reusify@npm:^1.0.0, reusify@npm:^1.0.4": version: 1.0.4 resolution: "reusify@npm:1.0.4" checksum: c3076ebcc22a6bc252cb0b9c77561795256c22b757f40c0d8110b1300723f15ec0fc8685e8d4ea6d7666f36c79ccc793b1939c748bf36f18f542744a4e379fcc @@ -12571,7 +12676,7 @@ __metadata: languageName: node linkType: hard -"safer-buffer@npm:>= 2.1.2 < 3, safer-buffer@npm:>= 2.1.2 < 3.0.0": +"safer-buffer@npm:>= 2.1.2 < 3, safer-buffer@npm:>= 2.1.2 < 3.0.0, safer-buffer@npm:^2.1.0": version: 2.1.2 resolution: "safer-buffer@npm:2.1.2" checksum: cab8f25ae6f1434abee8d80023d7e72b598cf1327164ddab31003c51215526801e40b66c5e65d658a0af1e9d6478cadcb4c745f4bd6751f97d8644786c0978b0 @@ -13045,6 +13150,19 @@ __metadata: languageName: node linkType: hard +"steed@npm:^1.1.3": + version: 1.1.3 + resolution: "steed@npm:1.1.3" + dependencies: + fastfall: ^1.5.0 + fastparallel: ^2.2.0 + fastq: ^1.3.0 + fastseries: ^1.7.0 + reusify: ^1.0.0 + checksum: 62eeea9adf0f05566bcd43ad0183054988e670c87e4f3cf557a0e7ef127b97f7995d1f3a22fdf32101c51361bcf5f8dd0ceb3818174f7209b1f645f521272d00 + languageName: node + linkType: hard + "stop-iteration-iterator@npm:^1.0.0": version: 1.0.0 resolution: "stop-iteration-iterator@npm:1.0.0" @@ -14984,6 +15102,13 @@ __metadata: languageName: node linkType: hard +"xtend@npm:^4.0.0, xtend@npm:^4.0.2": + version: 4.0.2 + resolution: "xtend@npm:4.0.2" + checksum: ac5dfa738b21f6e7f0dd6e65e1b3155036d68104e67e5d5d1bde74892e327d7e5636a076f625599dc394330a731861e87343ff184b0047fef1360a7ec0a5a36a + languageName: node + linkType: hard + "y18n@npm:^5.0.5": version: 5.0.8 resolution: "y18n@npm:5.0.8" @@ -15112,6 +15237,7 @@ __metadata: "@aero/sanitizer": ^1.3.2 "@discordjs/rest": ^2.0.1 "@fastify/helmet": ^11.1.1 + "@fastify/jwt": ^7.2.1 "@fastify/sensible": ^5.3.0 "@naval-base/ms": ^3.1.0 "@skyra/i18next-backend": ^2.0.4