feat: add and close #169

This commit is contained in:
Xyphyn committed 2023-10-14 11:31:09 -07:00
1 parent ea594e0061
commit 108169f5a9
9 files changed
+89 -44

No files matched your search

+52 -15
View File
@@ -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<ProfileData>(
@@ -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)
+14 -14
View File
@@ -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 @@
<Button
href="/admin"
aria-label="Admin"
class="max-md:w-9 max-md:h-8 max-md:!p-0 dark:text-zinc-300 text-slate-700"
class="max-md:w-9 max-md:h-8 max-md:!p-0 dark:text-zinc-300 text-slate-700 relative"
>
<Icon src={ServerStack} mini size="18" slot="prefix" />
<span class="hidden md:inline">Admin</span>
{#if $profile?.user?.notifications.applications ?? 0 > 0}
<div
class="rounded-full w-2 h-2 bg-red-500 absolute -top-1 -left-1"
/>
{/if}
</Button>
{/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}
<div
class="rounded-full w-2 h-2 bg-red-500 absolute -top-1 -left-1"
/>
@@ -205,7 +212,7 @@
alt={$profile.user.local_user_view.person.name}
/>
</div>
{#if $profile.user.unreads > 0}
{#if $profile.user.notifications.inbox > 0}
<div
class="rounded-full w-2 h-2 bg-red-500 absolute top-0 left-0 z-10"
/>
@@ -226,12 +233,10 @@
<MenuButton link href="/inbox">
<Icon src={Inbox} mini width={16} />
Inbox
{#if $profile.user.unreads > 0}
<div
class="rounded-full w-auto flex items-center px-2 h-5 justify-center font-bold bg-red-500 ml-auto"
>
{$profile.user.unreads}
</div>
{#if $profile.user.notifications.inbox > 0}
<Badge color="red-subtle" class="text-xs ml-auto font-bold !py-0.5">
{$profile.user.notifications.inbox}
</Badge>
{/if}
</MenuButton>
<MenuButton link href="/saved">
@@ -241,11 +246,6 @@
<MenuButton link href="/accounts">
<Icon src={UserGroup} mini width={16} />
Accounts
<span
class="text-xs font-bold bg-slate-100 dark:bg-zinc-700 px-2 py-0.5 rounded-md ml-auto"
>
{$profileData.profiles.length}
</span>
</MenuButton>
<hr class="dark:opacity-10 w-[90%] my-2 mx-auto" />
<MenuDivider>App</MenuDivider>