Skip to content

Commit

Permalink
feat: aggregated cases
Browse files Browse the repository at this point in the history
  • Loading branch information
iCrawl committed Sep 30, 2023
1 parent be45131 commit eb71990
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 100 deletions.
2 changes: 1 addition & 1 deletion apps/website/src/app/api/discord/callback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}
2 changes: 1 addition & 1 deletion apps/website/src/app/api/discord/logout/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}
78 changes: 0 additions & 78 deletions apps/website/src/app/appeals/page.tsx

This file was deleted.

78 changes: 78 additions & 0 deletions apps/website/src/app/cases/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<a
href={`https://discord.com/api/oauth2/authorize?client_id=${
process.env.DISCORD_CLIENT_ID
}&redirect_uri=${encodeURIComponent(
process.env.DISCORD_REDIRECT_URI!,
)}&response_type=code&scope=identify%20guilds.members.read%20guilds.join%20guilds`}
>
Login with Discord
</a>
);
}

// 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 <div className="mx-auto max-w-5xl gap-2 p-8">Nah, surely not.</div>;
}

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 (
<div className="mx-auto flex max-w-5xl flex-col gap-2 px-4 pb-8 md:flex-row md:gap-8">
<div className="from-dark-800 from-82% md:bg-dark-800 sticky top-0 w-full place-self-center bg-gradient-to-b py-8 md:w-auto md:place-self-start">
<UserDisplay user={user} />
</div>

<div className="flex w-full flex-col gap-4 md:pt-8">
{cases.map((case_: any) => (
<div key={case_.case_id} className="bg-dark-400 rounded-lg p-4">
<div>Case: {case_.case_id}</div>
<div>Reason: {case_.reason}</div>
</div>
))}
</div>
</div>
);
}
4 changes: 2 additions & 2 deletions apps/website/src/components/UserDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export function UserDisplay({
const isBannerAnimated = user.banner?.startsWith("a_");

return (
<div className="flex flex-col p-4">
<div className="flex w-[340px] max-w-[340px] flex-col">
<div className="relative">
<div className="h-[120px] max-h-[120px] w-[340px] max-w-[340px]">
<div className="h-[120px] max-h-[120px]">
{user.banner ? (
<picture>
<img
Expand Down
1 change: 1 addition & 0 deletions apps/yuudachi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@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",
Expand Down
4 changes: 2 additions & 2 deletions apps/yuudachi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type { Redis } from "ioredis";
import { register } from "prom-client";
import readdirp from "readdirp";
import { scamDomainRequestHeaders } from "./functions/anti-scam/refreshScamDomains.js";
// import { api } from "./util/api.js";
import { api } from "./util/api.js";
import { createWebhooks } from "./util/webhooks.js";
import { WebSocketConnection } from "./websocket/WebSocketConnection.js";

Expand Down Expand Up @@ -121,7 +121,7 @@ try {
}

await client.login();
// api.listen({ port: Number(process.env.API_PORT) });
api.listen({ port: Number(process.env.API_PORT) });

const wsURL = process.env.SCAM_DOMAIN_WS;
const identity = process.env.SCAM_DOMAIN_IDENTITY;
Expand Down
27 changes: 17 additions & 10 deletions apps/yuudachi/src/util/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import process from "node:process";
import helmet from "@fastify/helmet";
import jwt from "@fastify/jwt";
import sensible from "@fastify/sensible";
import { container, kSQL } from "@yuudachi/framework";
import { Client } from "discord.js";
Expand All @@ -9,29 +11,34 @@ import type { RawCase } from "../functions/cases/transformCase.js";
export const api = fastify({ trustProxy: true })
.register(helmet)
.register(sensible)
.register(jwt, { secret: process.env.API_JWT_SECRET! })
.addHook("onRequest", async (request, reply) => {
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<Sql<any>>(kSQL);

return sql<RawCase[]>`
const user = await client.users.fetch(id);
const cases = await sql<RawCase[]>`
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();
Expand Down
Loading

1 comment on commit eb71990

@vercel
Copy link

@vercel vercel bot commented on eb71990 Sep 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

yuudachi – ./apps/website

yuudachi.vercel.app
www.yuudachi.dev
yuudachi.dev
yuudachi-git-main-discordjs.vercel.app
yuudachi-discordjs.vercel.app

Please sign in to comment.