impr: replace amMod, amModOfAny, isAdmin, with profile class getters
This commit is contained in:
18 files changed
+89
-89
No files matched your search
@@ -3,7 +3,6 @@ import { env } from '$env/dynamic/public'
|
||||
import { DEFAULT_CLIENT_TYPE, type ClientType } from '$lib/api/base'
|
||||
import { client, getClient, site } from '$lib/api/client.svelte'
|
||||
import type { Community, GetSiteResponse, MyUserInfo } from '$lib/api/types'
|
||||
import { amModOfAny, isAdmin } from '$lib/feature/moderation/moderation'
|
||||
import { publishedToDate } from '$lib/ui/util/date'
|
||||
import { toast } from 'mono-svelte'
|
||||
import { writable } from 'svelte/store'
|
||||
@@ -207,11 +206,7 @@ class Profile {
|
||||
const { user, jwt } = profile.current
|
||||
if (!user || !jwt) throw new Error('checkInbox() called with invalid user')
|
||||
|
||||
const notifs = await getNotificationCount(
|
||||
jwt,
|
||||
amModOfAny(user) ?? false,
|
||||
user ? isAdmin(user) : false,
|
||||
)
|
||||
const notifs = await getNotificationCount(jwt, this.isMod(), this.isAdmin)
|
||||
|
||||
return {
|
||||
inbox: notifs.unreads,
|
||||
@@ -268,6 +263,26 @@ class Profile {
|
||||
}
|
||||
}
|
||||
|
||||
isMod(community?: Community) {
|
||||
if (community)
|
||||
return (
|
||||
(this.#current.user?.moderates.some(
|
||||
(i) => i.community.id == community.id,
|
||||
) ||
|
||||
(community.local && this.isAdmin)) ??
|
||||
false
|
||||
)
|
||||
else return (this.#current.user?.moderates.length ?? 0) > 0
|
||||
}
|
||||
|
||||
get isAdmin() {
|
||||
return (
|
||||
site.data?.admins.some(
|
||||
(i) => i.person.id == this.#current.user?.local_user_view.person.id,
|
||||
) ?? false
|
||||
)
|
||||
}
|
||||
|
||||
mainEffect = $effect.root(() => {
|
||||
// Sync with localStorage
|
||||
$effect(() => {
|
||||
|
||||
@@ -12,11 +12,12 @@
|
||||
Flag,
|
||||
PencilSquare,
|
||||
Share,
|
||||
ShieldCheck,
|
||||
Trash,
|
||||
} from 'svelte-hero-icons/dist'
|
||||
import { deleteItem, save } from '../legacy/contentview'
|
||||
import CommentModerationMenu from '../moderation/CommentModerationMenu.svelte'
|
||||
import { amMod, isAdmin, report } from '../moderation/moderation'
|
||||
import { report } from '../moderation/moderation'
|
||||
import CommentVote from './CommentVote.svelte'
|
||||
|
||||
interface Props {
|
||||
@@ -36,7 +37,7 @@
|
||||
|
||||
<div
|
||||
class={[
|
||||
'flex flex-row items-center gap-0.5 h-7 w-full',
|
||||
'flex flex-row items-center gap-0.5 w-full',
|
||||
settings.posts.reverseActions && 'flex-row-reverse',
|
||||
]}
|
||||
>
|
||||
@@ -57,8 +58,20 @@
|
||||
>
|
||||
{$t('comment.reply')}
|
||||
</Button>
|
||||
{#if profile.current?.user && (amMod(profile.current?.user, comment.community) || isAdmin(profile.current.user))}
|
||||
<CommentModerationMenu bind:item={comment} />
|
||||
{#if profile.current?.user && (profile.isMod(comment.community) || profile.isAdmin)}
|
||||
<CommentModerationMenu bind:item={comment}>
|
||||
{#snippet target(attachment)}
|
||||
<Button
|
||||
{@attach attachment}
|
||||
class="dark:text-zinc-400 text-slate-600"
|
||||
color="tertiary"
|
||||
size="square-md"
|
||||
rounding="pill"
|
||||
icon={ShieldCheck}
|
||||
aria-label={$t('moderation.label')}
|
||||
/>
|
||||
{/snippet}
|
||||
</CommentModerationMenu>
|
||||
{/if}
|
||||
<Menu placement="bottom">
|
||||
{#snippet target(attachment)}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { site } from '$lib/api/client.svelte'
|
||||
import type { Comment } from '$lib/api/types'
|
||||
import { profile } from '$lib/app/auth.svelte'
|
||||
import { errorMessage } from '$lib/app/error'
|
||||
import { t } from '$lib/app/i18n'
|
||||
|
||||
@@ -38,7 +38,6 @@
|
||||
ShieldCheck,
|
||||
Tag,
|
||||
} from 'svelte-hero-icons/dist'
|
||||
import { amMod, isAdmin } from '../moderation/moderation'
|
||||
import { addSubscription } from '../user'
|
||||
import CommunityFlair from './CommunityFlair.svelte'
|
||||
|
||||
@@ -219,7 +218,7 @@
|
||||
/>
|
||||
{/if}
|
||||
{/if}
|
||||
{#if profile.current?.user && amMod(profile.current.user, community_view.community)}
|
||||
{#if profile.isMod(community_view.community)}
|
||||
<SidebarButton
|
||||
href="/c/{fullCommunityName(
|
||||
community_view.community.name,
|
||||
@@ -242,7 +241,7 @@
|
||||
{$t('cards.community.modlog')}
|
||||
</MenuButton>
|
||||
{#if profile.current?.jwt}
|
||||
{#if profile.current.user && amMod(profile.current.user, community_view.community)}
|
||||
{#if profile.isMod(community_view.community)}
|
||||
<MenuButton
|
||||
color="success-subtle"
|
||||
href="/moderation?community={community_view.community.id}"
|
||||
@@ -272,7 +271,7 @@
|
||||
{$t('cards.community.blockInstance')}
|
||||
</MenuButton>
|
||||
{/if}
|
||||
{#if profile.current?.user && isAdmin(profile.current.user)}
|
||||
{#if profile.isAdmin}
|
||||
<MenuButton
|
||||
color="danger-subtle"
|
||||
onclick={() =>
|
||||
|
||||
@@ -38,7 +38,6 @@
|
||||
Tag,
|
||||
} from 'svelte-hero-icons/dist'
|
||||
import Subscribe from '../../../routes/communities/Subscribe.svelte'
|
||||
import { amMod, isAdmin } from '../moderation/moderation'
|
||||
import { block, blockInstance, purgeCommunity } from './CommunityCard.svelte'
|
||||
import CommunityFlair from './CommunityFlair.svelte'
|
||||
|
||||
@@ -219,7 +218,7 @@
|
||||
<Icon src={Newspaper} size="16" mini />
|
||||
{$t('cards.community.modlog')}
|
||||
</MenuButton>
|
||||
{#if profile.current.user && amMod(profile.current.user, community)}
|
||||
{#if profile.isMod(community)}
|
||||
<MenuButton
|
||||
color="success-subtle"
|
||||
href="/moderation?community={community.id}"
|
||||
@@ -239,17 +238,15 @@
|
||||
? $t('cards.community.unblock')
|
||||
: $t('cards.community.block')}
|
||||
</MenuButton>
|
||||
{#if profile.current?.user}
|
||||
<MenuButton
|
||||
color="danger-subtle"
|
||||
size="lg"
|
||||
onclick={() => blockInstance(community.instance_id)}
|
||||
icon={BuildingOffice2}
|
||||
>
|
||||
{$t('cards.community.blockInstance')}
|
||||
</MenuButton>
|
||||
{/if}
|
||||
{#if profile.current?.user && isAdmin(profile.current.user)}
|
||||
<MenuButton
|
||||
color="danger-subtle"
|
||||
size="lg"
|
||||
onclick={() => blockInstance(community.instance_id)}
|
||||
icon={BuildingOffice2}
|
||||
>
|
||||
{$t('cards.community.blockInstance')}
|
||||
</MenuButton>
|
||||
{#if profile.isAdmin}
|
||||
<MenuButton
|
||||
color="danger-subtle"
|
||||
onclick={() =>
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
Newspaper,
|
||||
ShieldCheck,
|
||||
} from 'svelte-hero-icons/dist'
|
||||
import { amModOfAny } from '../moderation/moderation'
|
||||
|
||||
interface Props extends SelectProps<string> {
|
||||
selected: string
|
||||
@@ -64,11 +63,7 @@
|
||||
>
|
||||
{$t('filter.location.subscribed')}
|
||||
</Option>
|
||||
<Option
|
||||
value="ModeratorView"
|
||||
disabled={!profile.current?.jwt || !amModOfAny(profile.current?.user)}
|
||||
icon={ShieldCheck}
|
||||
>
|
||||
<Option value="ModeratorView" disabled={!profile.isMod()} icon={ShieldCheck}>
|
||||
{$t('filter.location.moderator')}
|
||||
</Option>
|
||||
{@render children?.()}
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
import type { CommentView } from '$lib/api/types'
|
||||
import { profile } from '$lib/app/auth.svelte'
|
||||
import { t } from '$lib/app/i18n'
|
||||
import { Button, Menu, MenuButton, MenuDivider } from 'mono-svelte'
|
||||
import { Menu, MenuButton, MenuDivider } from 'mono-svelte'
|
||||
import type { Snippet } from 'svelte'
|
||||
import {
|
||||
ArrowsUpDown,
|
||||
Fire,
|
||||
@@ -12,32 +13,22 @@
|
||||
ShieldExclamation,
|
||||
Trash,
|
||||
} from 'svelte-hero-icons/dist'
|
||||
import type { Attachment } from 'svelte/attachments'
|
||||
import { isCommentView } from '../legacy/item'
|
||||
import { amMod, ban, feature, isAdmin, remove, viewVotes } from './moderation'
|
||||
import ShieldIcon from './ShieldIcon.svelte'
|
||||
import { ban, feature, remove, viewVotes } from './moderation'
|
||||
|
||||
interface Props {
|
||||
item: CommentView
|
||||
target: Snippet<[Attachment]>
|
||||
}
|
||||
|
||||
let { item = $bindable(), ...rest }: Props = $props()
|
||||
let { item = $bindable(), target, ...rest }: Props = $props()
|
||||
</script>
|
||||
|
||||
<Menu placement="bottom" class="top-0 h-[26px] w-[26px] ">
|
||||
{#snippet target(attachment)}
|
||||
<Button
|
||||
{@attach attachment}
|
||||
class="w-[26px] h-[26px] hover:text-green-500! dark:text-zinc-400 text-slate-600"
|
||||
size="square-md"
|
||||
color="tertiary"
|
||||
{...rest}
|
||||
>
|
||||
<ShieldIcon filled width={14} />
|
||||
</Button>
|
||||
{/snippet}
|
||||
{#if (profile.current?.user && amMod(profile.current.user, item.community)) || (profile.current?.user && isAdmin(profile.current.user))}
|
||||
<MenuDivider showLabel>
|
||||
{#if !item.community.local && !amMod(profile.current.user, item.community)}
|
||||
<Menu {...rest} placement="bottom" {target}>
|
||||
{#if profile.isMod(item.community) || profile.isAdmin}
|
||||
<MenuDivider hidden>
|
||||
{#if !item.community.local && !profile.isMod(item.community)}
|
||||
{$t('moderation.labelInstanceOnly')}
|
||||
{:else}
|
||||
{$t('moderation.label')}
|
||||
@@ -98,7 +89,7 @@
|
||||
</MenuButton>
|
||||
{/if}
|
||||
|
||||
{#if profile.current?.user && isAdmin(profile.current.user)}
|
||||
{#if profile.isAdmin}
|
||||
<MenuDivider showLabel>{$t('admin.label')}</MenuDivider>
|
||||
<MenuButton
|
||||
color="danger-subtle"
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
} from 'svelte-hero-icons/dist'
|
||||
import type { Attachment } from 'svelte/attachments'
|
||||
import { isCommentView, isPostView } from '../legacy/item'
|
||||
import { amMod, ban, isAdmin, remove, viewVotes } from './moderation'
|
||||
import { ban, remove, viewVotes } from './moderation'
|
||||
|
||||
interface Props {
|
||||
item: PostView | CommentView
|
||||
@@ -79,9 +79,9 @@
|
||||
{#snippet target(attachment)}
|
||||
{@render passedTarget(attachment, acting)}
|
||||
{/snippet}
|
||||
{#if (profile.current?.user && amMod(profile.current.user, item.community)) || (profile.current?.user && isAdmin(profile.current.user))}
|
||||
{#if profile.isMod(item.community) || profile.isAdmin}
|
||||
<MenuDivider showLabel>
|
||||
{#if !item.community.local && !amMod(profile.current.user, item.community)}
|
||||
{#if !item.community.local && !profile.isMod(item.community)}
|
||||
{$t('moderation.labelInstanceOnly')}
|
||||
{:else}
|
||||
{$t('moderation.label')}
|
||||
@@ -111,7 +111,7 @@
|
||||
? $t('moderation.unfeature')
|
||||
: $t('moderation.feature')}
|
||||
</span>
|
||||
{#if isAdmin(profile.current.user)}
|
||||
{#if profile.isAdmin}
|
||||
<span class="text-xs opacity-80">{$t('form.post.community')}</span>
|
||||
{/if}
|
||||
</div>
|
||||
@@ -151,7 +151,7 @@
|
||||
{$t('moderation.votes')}
|
||||
</MenuButton>
|
||||
{/if}
|
||||
{#if profile.current?.user && isAdmin(profile.current.user)}
|
||||
{#if profile.isAdmin}
|
||||
<MenuDivider showLabel>{$t('admin.label')}</MenuDivider>
|
||||
<MenuButton
|
||||
color="success-subtle"
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
import Comment from '../comment/Comment.svelte'
|
||||
import { isCommentView, isPostView } from '../legacy/item'
|
||||
import { Post } from '../post'
|
||||
import { amMod, isAdmin, removalTemplate } from './moderation'
|
||||
import { removalTemplate } from './moderation'
|
||||
|
||||
interface Props {
|
||||
open: boolean
|
||||
@@ -213,7 +213,7 @@
|
||||
bind:value={reason}
|
||||
/>
|
||||
|
||||
{#if !removed && profile.current?.user && (amMod(profile.current.user, item.community) || (isAdmin(profile.current.user) && item.community.local))}
|
||||
{#if !removed}
|
||||
<Switch bind:checked={commentReason}>
|
||||
{$t('moderation.removeSubmission.withReason')}
|
||||
</Switch>
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { client } from '$lib/api/client.svelte'
|
||||
import type {
|
||||
Comment,
|
||||
CommentView,
|
||||
Community,
|
||||
MyUserInfo,
|
||||
Person,
|
||||
PostView,
|
||||
PrivateMessageView,
|
||||
@@ -104,15 +104,6 @@ export async function viewVotes(item: PostView | CommentView) {
|
||||
}))
|
||||
}
|
||||
|
||||
export const amMod = (me: MyUserInfo, community: Community) =>
|
||||
me.moderates.map((c) => c.community.id).includes(community.id) ||
|
||||
(community.local && isAdmin(me))
|
||||
|
||||
export const amModOfAny = (me?: MyUserInfo) =>
|
||||
me && (me.moderates.length > 0 || isAdmin(me))
|
||||
|
||||
export const isAdmin = (me: MyUserInfo) => me.local_user_view.person.admin
|
||||
|
||||
export const removalTemplate = (
|
||||
input: string,
|
||||
content: {
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
import { t } from '$lib/app/i18n'
|
||||
import { settings, type View } from '$lib/app/settings.svelte'
|
||||
import { save } from '$lib/feature/legacy/contentview'
|
||||
import { amMod, isAdmin } from '$lib/feature/moderation/moderation'
|
||||
import { publishedToDate } from '$lib/ui/util/date'
|
||||
import FormattedNumber from '$lib/ui/util/FormattedNumber.svelte'
|
||||
import { Button, Menu, Modal, Spinner } from 'mono-svelte'
|
||||
@@ -133,7 +132,7 @@
|
||||
icon={BugAnt}
|
||||
></Button>
|
||||
{/if}
|
||||
{#if profile.current?.user && (amMod(profile.current.user, post.community) || isAdmin(profile.current.user))}
|
||||
{#if profile.isMod(post.community) || profile.isAdmin}
|
||||
{#await import('$lib/feature/moderation/ModerationMenu.svelte') then { default: ModerationMenu }}
|
||||
<ModerationMenu bind:item={post}>
|
||||
{#snippet target(attachment, acting)}
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
import { notifications, profile } from '$lib/app/auth.svelte'
|
||||
import { t } from '$lib/app/i18n'
|
||||
import { LINKED_INSTANCE_URL } from '$lib/app/instance.svelte'
|
||||
import { amModOfAny, isAdmin } from '$lib/feature/moderation/moderation'
|
||||
import { Menu, Spinner } from 'mono-svelte'
|
||||
import {
|
||||
Bars3,
|
||||
@@ -71,7 +70,7 @@
|
||||
</NavButton>
|
||||
<div class="hidden md:block md:flex-1"></div>
|
||||
<div class="sr-only md:not-sr-only md:contents">
|
||||
{#if profile.current?.user && isAdmin(profile.current.user)}
|
||||
{#if profile.isAdmin}
|
||||
<NavButton
|
||||
href="/admin"
|
||||
label={$t('nav.admin')}
|
||||
@@ -80,7 +79,7 @@
|
||||
isSelectedFilter={(path) => path.startsWith('/admin')}
|
||||
/>
|
||||
{/if}
|
||||
{#if amModOfAny(profile.current?.user)}
|
||||
{#if profile.isMod()}
|
||||
<NavButton
|
||||
href="/moderation"
|
||||
label={$t('nav.moderation')}
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
import { settings } from '$lib/app/settings.svelte'
|
||||
import { theme } from '$lib/app/theme/theme.svelte'
|
||||
import InstanceCard from '$lib/feature/instance/InstanceCard.svelte'
|
||||
import { amModOfAny, isAdmin } from '$lib/feature/moderation/moderation'
|
||||
import {
|
||||
Badge,
|
||||
Button,
|
||||
@@ -70,7 +69,7 @@
|
||||
</Badge>
|
||||
{/if}
|
||||
</MenuButton>
|
||||
{#if amModOfAny(profile.current.user)}
|
||||
{#if profile.isMod()}
|
||||
<MenuButton href="/moderation" icon={ShieldCheck}>
|
||||
{$t('routes.moderation.feed')}
|
||||
{#snippet suffix()}
|
||||
@@ -78,7 +77,7 @@
|
||||
{/snippet}
|
||||
</MenuButton>
|
||||
{/if}
|
||||
{#if profile.current.user && isAdmin(profile.current.user)}
|
||||
{#if profile.isAdmin}
|
||||
<MenuButton href="/admin/applications" icon={ServerStack}>
|
||||
{$t('routes.admin.applications.title')}
|
||||
{#snippet suffix()}
|
||||
|
||||
@@ -9,7 +9,6 @@ import { settings } from '$lib/app/settings.svelte'
|
||||
import { theme, type ThemeData } from '$lib/app/theme/theme.svelte'
|
||||
import { fullCommunityName } from '$lib/app/util.svelte'
|
||||
import type { ResumableItem } from '$lib/feature/legacy/item'
|
||||
import { amModOfAny, isAdmin } from '$lib/feature/moderation/moderation'
|
||||
import {
|
||||
ArrowRightOnRectangle,
|
||||
ArrowTrendingDown,
|
||||
@@ -92,7 +91,7 @@ export function getGroups(
|
||||
name: t.get('nav.communities'),
|
||||
icon: GlobeAlt,
|
||||
},
|
||||
...(amModOfAny(profile.user)
|
||||
...(currentProfile.isMod()
|
||||
? [
|
||||
{
|
||||
href: '/moderation',
|
||||
@@ -101,7 +100,7 @@ export function getGroups(
|
||||
},
|
||||
]
|
||||
: []),
|
||||
...(profile.user && isAdmin(profile.user)
|
||||
...(currentProfile.isAdmin
|
||||
? [
|
||||
{
|
||||
href: '/admin',
|
||||
|
||||
@@ -3,12 +3,16 @@
|
||||
|
||||
let {
|
||||
showLabel = false,
|
||||
hidden = false,
|
||||
children,
|
||||
}: { showLabel?: boolean; children: Snippet } = $props()
|
||||
}: { showLabel?: boolean; hidden?: boolean; children: Snippet } = $props()
|
||||
</script>
|
||||
|
||||
<div
|
||||
class="text-slate-800 dark:text-zinc-200 text-xs font-medium mx-3 my-1.5 flex items-center gap-1"
|
||||
class={[
|
||||
'text-slate-800 dark:text-zinc-200 text-xs font-medium mx-3 my-1.5 flex items-center gap-1',
|
||||
hidden && 'sr-only',
|
||||
]}
|
||||
>
|
||||
<div class={[!showLabel && 'sr-only']}>{@render children?.()}</div>
|
||||
<hr
|
||||
|
||||
@@ -2,7 +2,6 @@ import { getClient } from '$lib/api/client.svelte'
|
||||
import { profile } from '$lib/app/auth.svelte'
|
||||
import { ReactiveState } from '$lib/app/util.svelte'
|
||||
import { isCommentView, isPostView } from '$lib/feature/legacy/item'
|
||||
import { isAdmin } from '$lib/feature/moderation/moderation'
|
||||
import {
|
||||
generalizeCommentReport,
|
||||
generalizePostReport,
|
||||
@@ -30,7 +29,7 @@ export async function load({ url, fetch }) {
|
||||
community_id: community,
|
||||
}
|
||||
|
||||
const admin = profile.current?.user ? isAdmin(profile!.current.user!) : false
|
||||
const admin = profile.isAdmin
|
||||
|
||||
const [posts, comments, messages] = await Promise.all([
|
||||
client.listPostReports({
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
import { settings } from '$lib/app/settings.svelte'
|
||||
import { searchParam } from '$lib/app/util.svelte'
|
||||
import CommunityLink from '$lib/feature/community/CommunityLink.svelte'
|
||||
import { isAdmin } from '$lib/feature/moderation/moderation'
|
||||
import { postLink } from '$lib/feature/post'
|
||||
import UserAutocomplete from '$lib/feature/user/UserAutocomplete.svelte'
|
||||
import UserLink from '$lib/feature/user/UserLink.svelte'
|
||||
@@ -194,7 +193,7 @@
|
||||
onselect={(e) =>
|
||||
searchParam(page.url, 'user', e?.id.toString() ?? '', 'page')}
|
||||
/>
|
||||
{#if profile.current?.user && isAdmin(profile.current?.user)}
|
||||
{#if profile.isAdmin}
|
||||
<UserAutocomplete
|
||||
placeholder="Filter by moderator"
|
||||
listing_type="All"
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import type { PersonView } from '$lib/api/types'
|
||||
import { profile } from '$lib/app/auth.svelte'
|
||||
import { t } from '$lib/app/i18n'
|
||||
import { ban, isAdmin } from '$lib/feature/moderation/moderation'
|
||||
import { ban } from '$lib/feature/moderation/moderation'
|
||||
import { blockUser, isBlocked } from '$lib/feature/user'
|
||||
import {
|
||||
Button,
|
||||
@@ -91,7 +91,7 @@
|
||||
{$t('form.profile.matrix')}
|
||||
</Button>
|
||||
{/if}
|
||||
{#if isAdmin(profile.current?.user)}
|
||||
{#if profile.isAdmin}
|
||||
<Menu class="ml-auto" placement="bottom-end">
|
||||
{#snippet target(attachment)}
|
||||
<Button
|
||||
|
||||
Reference in new issue
Block a user