From 8ca6a4f5a379ac75ad410431ee17af04afefc5d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Sat, 3 Aug 2024 15:01:57 +0200 Subject: [PATCH] feat: render each guild --- .../src/components/dashboard/UserInfo.astro | 18 ++++++++++++--- apps/website/src/components/preact/Guild.tsx | 12 +++++++--- .../src/components/preact/LoadingGuild.tsx | 4 ++-- apps/website/src/lib/guilds.ts | 16 +++++++++++-- apps/website/src/lib/user.ts | 7 +++++- .../src/pages/dashboard/guilds/[id].astro | 23 +++++++++++++++++++ 6 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 apps/website/src/pages/dashboard/guilds/[id].astro diff --git a/apps/website/src/components/dashboard/UserInfo.astro b/apps/website/src/components/dashboard/UserInfo.astro index 273a362..7261fc1 100644 --- a/apps/website/src/components/dashboard/UserInfo.astro +++ b/apps/website/src/components/dashboard/UserInfo.astro @@ -1,10 +1,22 @@ --- import { Image } from "astro:assets"; +import type { User } from "~/env"; import { getUser } from "~/lib/user"; -const user = await getUser(Astro.request); -if (!user) { - return Astro.redirect("/auth/login"); +interface Props { + user?: User; +} + +let user: User; +if (!Astro.props.user) { + const u = await getUser(Astro.request); + if (!u) { + return Astro.redirect("/auth/login"); + } + + user = u; +} else { + user = Astro.props.user; } --- diff --git a/apps/website/src/components/preact/Guild.tsx b/apps/website/src/components/preact/Guild.tsx index 08ef8d6..6195fc8 100644 --- a/apps/website/src/components/preact/Guild.tsx +++ b/apps/website/src/components/preact/Guild.tsx @@ -7,10 +7,16 @@ interface Props { export default function Guild({ guild, mutual }: Props) { return ( -
{guild.name}
-
+ ); } diff --git a/apps/website/src/components/preact/LoadingGuild.tsx b/apps/website/src/components/preact/LoadingGuild.tsx index 59e2a06..e575bcf 100644 --- a/apps/website/src/components/preact/LoadingGuild.tsx +++ b/apps/website/src/components/preact/LoadingGuild.tsx @@ -1,9 +1,9 @@ export default function LoadingGuild() { return ( -
+
-
+ ); } diff --git a/apps/website/src/lib/guilds.ts b/apps/website/src/lib/guilds.ts index 5776160..b7996f9 100644 --- a/apps/website/src/lib/guilds.ts +++ b/apps/website/src/lib/guilds.ts @@ -1,4 +1,17 @@ import type { Guild, MutualeGuild, User } from "~/env"; +import { isUserEligible } from "./user"; + +export async function getGuild( + user: User, + guildId: string +): Promise { + const guilds = await getUserGuilds(user); + const guild = guilds.find((g) => g.id === guildId); + + if (!guild || !(await isMutualGuild(guild))) return undefined; + + return guild; +} export async function getUserGuilds(user: User): Promise { const discordApiGuildsResponse = await fetch( @@ -14,12 +27,11 @@ export async function getUserGuilds(user: User): Promise { return (await discordApiGuildsResponse.json()) as Guild[]; } -// Checks if user has AMDINISTRATOR permissions in the guild export async function filterUserGuilds( guilds: Guild[] ): Promise { const filtered = guilds - .filter((g) => (BigInt(g.permissions) & 0x8n) == 0x8n) + .filter(isUserEligible) .sort((a, b) => Number(b.owner) - Number(a.owner)); const result: MutualeGuild[] = []; diff --git a/apps/website/src/lib/user.ts b/apps/website/src/lib/user.ts index eae9165..de9ea56 100644 --- a/apps/website/src/lib/user.ts +++ b/apps/website/src/lib/user.ts @@ -1,5 +1,5 @@ import { getSession } from "auth-astro/server"; -import type { User } from "~/env"; +import type { Guild, User } from "~/env"; export async function getUser(req: Request): Promise { const session = await getSession(req); @@ -7,3 +7,8 @@ export async function getUser(req: Request): Promise { return session.user as User; } + +// Checks if user has AMDINISTRATOR permissions in the guild +export function isUserEligible(guild: Guild): boolean { + return (BigInt(guild.permissions) & 0x8n) == 0x8n; +} diff --git a/apps/website/src/pages/dashboard/guilds/[id].astro b/apps/website/src/pages/dashboard/guilds/[id].astro new file mode 100644 index 0000000..088bf70 --- /dev/null +++ b/apps/website/src/pages/dashboard/guilds/[id].astro @@ -0,0 +1,23 @@ +--- +import { getGuild } from "~/lib/guilds"; +import { getUser, isUserEligible } from "~/lib/user"; + +const { id } = Astro.params; + +const user = await getUser(Astro.request); +if (!user || !id) { + return Astro.redirect("/auth/login"); +} + +const guild = await getGuild(user, id); +if (!guild) { + return Astro.redirect("/dashboard"); +} + +const eligible = isUserEligible(guild); +if (!eligible) { + return Astro.redirect("/dashboard"); +} +--- + +You can manage guild {guild.name} with id {guild.id}