From 108169f5a9adf38b64baba1b6fb71f41e20e3b13 Mon Sep 17 00:00:00 2001 From: Xyphyn Date: Sat, 14 Oct 2023 11:31:09 -0700 Subject: [PATCH] feat: add and close #169 --- src/lib/auth.ts | 67 ++++++++++++++----- src/lib/components/ui/Navbar.svelte | 28 ++++---- src/routes/admin/+layout.svelte | 10 +++ .../admin/applications/Application.svelte | 14 ++-- src/routes/admin/config/+page.svelte | 1 - src/routes/inbox/+page.svelte | 2 +- src/routes/inbox/InboxItem.svelte | 2 +- src/routes/moderation/+page.svelte | 2 +- src/routes/moderation/Report.svelte | 7 +- 9 files changed, 89 insertions(+), 44 deletions(-) diff --git a/src/lib/auth.ts b/src/lib/auth.ts index 3f081c67..8762e8f4 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -1,4 +1,7 @@ -import { amModOfAny } from '$lib/components/lemmy/moderation/moderation.js' +import { + amModOfAny, + isAdmin, +} from '$lib/components/lemmy/moderation/moderation.js' import { toast } from 'mono-svelte' import { DEFAULT_INSTANCE_URL, instance } from '$lib/instance.js' import { client, getClient, site } from '$lib/lemmy.js' @@ -51,8 +54,11 @@ interface ProfileData { } interface PersonData extends MyUserInfo { - unreads: number - reports: number + notifications: { + inbox: number + reports: number + applications: number + } } export let profileData = writable( @@ -192,8 +198,11 @@ async function userFromJwt( return { user: { - unreads: 0, - reports: 0, + notifications: { + applications: 0, + inbox: 0, + reports: 0, + }, ...myUser, }, site: site, @@ -296,23 +305,44 @@ export function moveProfile(id: number, up: boolean) { } } -const getNotificationCount = async (jwt: string, mod: boolean) => { +const getNotificationCount = async ( + jwt: string, + mod: boolean, + admin: boolean +) => { const unreads = await getClient().getUnreadCount() let reports: number = 0 + let applications: number = 0 if (mod) { - const reportRes = await getClient().getReportCount({}) + try { + const reportRes = await getClient().getReportCount({}) - reports = - reportRes.comment_reports + - reportRes.post_reports + - (reportRes.private_message_reports ?? 0) + reports = + reportRes.comment_reports + + reportRes.post_reports + + (reportRes.private_message_reports ?? 0) + } catch (e) { + // doesn't matter + } + } + + if (admin) { + try { + const applicationRes = + await getClient().getUnreadRegistrationApplicationCount() + + applications = applicationRes.registration_applications + } catch (e) { + // doesn't matter + } } return { unreads: unreads.mentions + unreads.private_messages + unreads.replies, reports: reports, + applications: applications, } } @@ -323,13 +353,20 @@ setInterval(async () => { const { user, jwt } = get(profile)! if (!jwt || !user) return - const notifs = await getNotificationCount(jwt, amModOfAny(user) ?? false) + const notifs = await getNotificationCount( + jwt, + amModOfAny(user) ?? false, + isAdmin(user) + ) - user.unreads = notifs.unreads - user.reports = notifs.reports + user.notifications = { + inbox: notifs.unreads, + applications: notifs.applications, + reports: notifs.reports, + } profile.update((p) => ({ ...p!, user: user, })) -}, get(userSettings).notifications.pollRate ?? 30 * 1000) +}, 5 * 60 * 1000) diff --git a/src/lib/components/ui/Navbar.svelte b/src/lib/components/ui/Navbar.svelte index b5d062ed..9d27bac1 100644 --- a/src/lib/components/ui/Navbar.svelte +++ b/src/lib/components/ui/Navbar.svelte @@ -13,6 +13,7 @@ import { userSettings } from '$lib/settings.js' import { theme } from '$lib/ui/colors.js' import { + Badge, Button, Menu, MenuButton, @@ -111,10 +112,16 @@ {/if} {#if amModOfAny($profile?.user)} @@ -123,7 +130,7 @@ aria-label="Moderation" class="max-md:w-9 max-md:h-8 max-md:!p-0 dark:text-zinc-300 text-slate-700 relative" > - {#if $profile?.user?.reports ?? 0 > 0} + {#if $profile?.user?.notifications.reports ?? 0 > 0}
@@ -205,7 +212,7 @@ alt={$profile.user.local_user_view.person.name} />
- {#if $profile.user.unreads > 0} + {#if $profile.user.notifications.inbox > 0}
@@ -226,12 +233,10 @@ Inbox - {#if $profile.user.unreads > 0} -
- {$profile.user.unreads} -
+ {#if $profile.user.notifications.inbox > 0} + + {$profile.user.notifications.inbox} + {/if}
@@ -241,11 +246,6 @@ Accounts - - {$profileData.profiles.length} -
App diff --git a/src/routes/admin/+layout.svelte b/src/routes/admin/+layout.svelte index 4a1dbba3..82b2bc77 100644 --- a/src/routes/admin/+layout.svelte +++ b/src/routes/admin/+layout.svelte @@ -1,9 +1,19 @@ +{#if $profile?.user?.notifications.applications ?? 0 > 0} + + + {$profile?.user?.notifications.applications} + unread applications + +{/if} +
- + User @@ -79,8 +83,8 @@