feat: add moderation tools (#33)

* feat: Add post submission removal

* feat: add comment submission removal

* feat: add report viewing and resolving

* misc: add notification dot

* misc: add locked thread badge

* feat: add thread locking

* feat: add initiating message conversations

* feat: add post pinning/unpinning

* misc: bump version

* fix: tweak lock colors

* fix: update post data on pin

* fix: return if reason is empty
This commit is contained in:
Xylight authored and GitHub committed 2023-07-27 15:53:31 -07:00
1 parent 53ef843cde
commit d3aae647a8
29 files changed
+835 -67

No files matched your search

@@ -0,0 +1,56 @@
<script lang="ts">
import { authData, getClient, user } from '$lib/lemmy'
import type {
CommentView,
Community,
CommunityView,
PostView,
} from 'lemmy-js-client'
import { isMod, remove } from './moderation'
import Menu from '$lib/components/ui/menu/Menu.svelte'
import Button from '$lib/components/input/Button.svelte'
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
import { Icon, Trash } from 'svelte-hero-icons'
import { Color } from '$lib/ui/colors.js'
import { isComment } from '$lib/lemmy/item.js'
export let item: PostView | CommentView
</script>
<Menu let:toggleOpen alignment="bottom-center" class="top-0 h-[26px] w-[26px]">
<Button
class="!p-1 hover:text-green-500 text-zinc-400"
size="sm"
on:click={toggleOpen}
slot="button"
color="elevatedLow"
{...$$restProps}
>
<svg
width="16"
height="16"
viewBox="0 0 20 22"
fill="none"
xmlns="http://www.w3.org/2000/svg"
slot="icon"
>
<path
fill-rule="evenodd"
clip-rule="evenodd"
d="M10.516 1.17C10.3767 1.03791 10.192 0.964283 10 0.964283C9.80801 0.964283 9.62333 1.03791 9.484 1.17C7.36127 3.18588 4.53412 4.29133 1.607 4.25C1.44753 4.24793 1.29156 4.29674 1.16172 4.38935C1.03189 4.48196 0.934957 4.61355 0.885 4.765C0.462973 6.05116 0.248618 7.39637 0.25 8.75C0.25 14.692 4.314 19.683 9.813 21.098C9.93566 21.1296 10.0643 21.1296 10.187 21.098C15.686 19.683 19.75 14.692 19.75 8.75C19.75 7.36 19.527 6.02 19.115 4.765C19.0652 4.61336 18.9684 4.48156 18.8385 4.38875C18.7087 4.29595 18.5526 4.247 18.393 4.249L18.25 4.25C15.254 4.25 12.533 3.08 10.516 1.17Z"
fill="currentColor"
/>
</svg>
</Button>
<span class="px-4 py-1 my-1 text-xs text-slate-600 dark:text-zinc-400">
Moderation
</span>
<MenuButton color="success" on:click={() => remove(item)}>
<Icon src={Trash} size="16" mini />
{#if isComment(item)}
{item.comment.removed ? 'Restore' : 'Remove'}
{:else}
{item.post.removed ? 'Restore' : 'Remove'}
{/if}
</MenuButton>
</Menu>
@@ -1,6 +1,8 @@
<script lang="ts">
import RemoveModal from '$lib/components/lemmy/moderation/RemoveModal.svelte'
import { modals } from '$lib/components/lemmy/moderation/moderation.js'
import ReportModal from './ReportModal.svelte'
</script>
<ReportModal bind:open={$modals.reporting.open} item={$modals.reporting.item} />
<RemoveModal bind:open={$modals.removing.open} item={$modals.removing.item} />
@@ -0,0 +1,151 @@
<script lang="ts">
import { authData, getClient, user } from '$lib/lemmy'
import type {
CommentView,
Community,
CommunityView,
PostView,
} from 'lemmy-js-client'
import { isMod, remove } from './moderation'
import Menu from '$lib/components/ui/menu/Menu.svelte'
import Button from '$lib/components/input/Button.svelte'
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
import {
Icon,
InformationCircle,
LockClosed,
LockOpen,
Trash,
} from 'svelte-hero-icons'
import { Color } from '$lib/ui/colors.js'
import { isComment, isPost } from '$lib/lemmy/item.js'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
export let item: PostView | CommentView
let locking = false
let pinning = false
$: acting = locking || pinning
async function lock(lock: boolean) {
if (!$authData || !isPost(item)) return
locking = true
try {
await getClient().lockPost({
auth: $authData.token,
locked: lock,
post_id: item.post.id,
})
item.post.locked = lock
toast({
content: `Successfully ${
lock ? 'locked' : 'unlocked'
} that post. You must refresh to see changes.`,
type: ToastType.success,
})
} catch (err) {
toast({
content: err as any,
type: ToastType.error,
})
}
locking = false
}
async function pin(pinned: boolean) {
if (!$authData || !isPost(item)) return
pinning = true
try {
await getClient().featurePost({
feature_type: 'Community',
auth: $authData.token,
featured: pinned,
post_id: item.post.id,
})
item.post.featured_community = pinned
toast({
content: `Successfully ${
pinned ? 'pinned' : 'unpinned'
} that post. You must refresh to see changes.`,
type: ToastType.success,
})
} catch (err) {
toast({
content: err as any,
type: ToastType.error,
})
}
pinning = false
}
</script>
<Menu let:toggleOpen alignment="bottom-right">
<Button
class="!p-1.5 hover:text-green-500 text-zinc-400"
on:click={toggleOpen}
slot="button"
size="sm"
loading={acting}
{...$$restProps}
>
<svg
width="16"
height="16"
viewBox="0 0 20 22"
fill="none"
xmlns="http://www.w3.org/2000/svg"
slot="icon"
>
<path
fill-rule="evenodd"
clip-rule="evenodd"
d="M10.516 1.17C10.3767 1.03791 10.192 0.964283 10 0.964283C9.80801 0.964283 9.62333 1.03791 9.484 1.17C7.36127 3.18588 4.53412 4.29133 1.607 4.25C1.44753 4.24793 1.29156 4.29674 1.16172 4.38935C1.03189 4.48196 0.934957 4.61355 0.885 4.765C0.462973 6.05116 0.248618 7.39637 0.25 8.75C0.25 14.692 4.314 19.683 9.813 21.098C9.93566 21.1296 10.0643 21.1296 10.187 21.098C15.686 19.683 19.75 14.692 19.75 8.75C19.75 7.36 19.527 6.02 19.115 4.765C19.0652 4.61336 18.9684 4.48156 18.8385 4.38875C18.7087 4.29595 18.5526 4.247 18.393 4.249L18.25 4.25C15.254 4.25 12.533 3.08 10.516 1.17Z"
fill="currentColor"
/>
</svg>
</Button>
<span class="px-4 py-1 my-1 text-xs text-slate-600 dark:text-zinc-400">
Moderation
</span>
<MenuButton
color="warning"
on:click={() => lock(!item.post.locked)}
loading={locking}
disabled={locking}
>
<Icon
src={item.post.locked ? LockOpen : LockClosed}
size="16"
mini
slot="icon"
/>
{item.post.locked ? 'Unlock' : 'Lock'}
</MenuButton>
<MenuButton
color="success"
on:click={() => pin(isPost(item) ? !item.post.featured_community : false)}
loading={pinning}
disabled={pinning}
>
<Icon src={InformationCircle} size="16" mini />
{item.post.featured_community ? 'Unpin' : 'Pin'}
</MenuButton>
<MenuButton color="dangerSecondary" on:click={() => remove(item)}>
<Icon src={Trash} size="16" mini />
{#if isComment(item)}
{item.comment.removed ? 'Restore' : 'Remove'}
{:else}
{item.post.removed ? 'Restore' : 'Remove'}
{/if}
</MenuButton>
</Menu>
@@ -0,0 +1,110 @@
<script lang="ts">
import Button from '$lib/components/input/Button.svelte'
import TextArea from '$lib/components/input/TextArea.svelte'
import Comment from '$lib/components/lemmy/comment/Comment.svelte'
import Post from '$lib/components/lemmy/post/Post.svelte'
import Modal from '$lib/components/ui/modal/Modal.svelte'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { authData, getClient } from '$lib/lemmy.js'
import { isComment, isPost } from '$lib/lemmy/item.js'
import type { CommentView, PostView } from 'lemmy-js-client'
export let open: boolean
export let item: PostView | CommentView | undefined = undefined
let reason = ''
let loading = false
$: removed = item
? isComment(item)
? item.comment.removed
: item.post.removed
: false
async function remove() {
if (!item) return
if (!$authData) throw new Error('Unauthenticated')
loading = true
try {
if (isComment(item)) {
await getClient().removeComment({
auth: $authData.token,
comment_id: item.comment.id,
removed: !removed,
reason: reason || undefined,
})
item.comment.removed = !removed
} else if (isPost(item)) {
await getClient().removePost({
auth: $authData.token,
post_id: item.post.id,
removed: !removed,
reason: reason || undefined,
})
item.post.removed = !removed
}
open = false
toast({
content: `Successfully ${
removed ? 'restored' : 'removed'
} that submission.`,
type: ToastType.success,
})
} catch (err) {
toast({
content: err as any,
type: ToastType.error,
})
}
loading = false
}
const resetText = () => (reason = '')
$: {
if (item) {
resetText()
}
}
</script>
<Modal bind:open>
<span slot="title">{removed ? 'Restoring' : 'Removing'} submission</span>
{#if item}
<form
on:submit|preventDefault={remove}
class="flex flex-col gap-4 list-none"
>
{#if isComment(item)}
<Comment
node={{
children: [],
comment_view: item,
depth: 1,
}}
postId={item.post.id}
actions={false}
/>
{:else if isPost(item)}
<Post actions={false} post={item} />
{/if}
<TextArea
rows={3}
label="Reason"
placeholder="Optional"
bind:value={reason}
/>
<Button color="primary" size="lg" {loading} disabled={loading} submit>
{removed ? 'Restore' : 'Remove'}
</Button>
</form>
{/if}
</Modal>
@@ -21,7 +21,7 @@
let reason = ''
async function report() {
if (!item || !$authData) return
if (!item || !$authData || reason == '') return
loading = true
try {
@@ -38,6 +38,7 @@
reason: reason,
})
}
open = false
toast({
content: 'That submission has been reported.',
type: ToastType.success,
@@ -0,0 +1,19 @@
<script lang="ts">
export let width: number = 16
</script>
<svg
{width}
height={width}
viewBox="0 0 20 22"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
fill-rule="evenodd"
clip-rule="evenodd"
d="M10.516 1.17C10.3767 1.03791 10.192 0.964283 10 0.964283C9.80801 0.964283 9.62333 1.03791 9.484 1.17C7.36127 3.18588 4.53412 4.29133 1.607 4.25C1.44753 4.24793 1.29156 4.29674 1.16172 4.38935C1.03189 4.48196 0.934957 4.61355 0.885 4.765C0.462973 6.05116 0.248618 7.39637 0.25 8.75C0.25 14.692 4.314 19.683 9.813 21.098C9.93566 21.1296 10.0643 21.1296 10.187 21.098C15.686 19.683 19.75 14.692 19.75 8.75C19.75 7.36 19.527 6.02 19.115 4.765C19.0652 4.61336 18.9684 4.48156 18.8385 4.38875C18.7087 4.29595 18.5526 4.247 18.393 4.249L18.25 4.25C15.254 4.25 12.533 3.08 10.516 1.17Z"
stroke="currentColor"
stroke-width="1.5"
/>
</svg>
@@ -1,4 +1,4 @@
import type { CommentView, PostView } from 'lemmy-js-client'
import type { CommentView, MyUserInfo, PostView } from 'lemmy-js-client'
import { writable } from 'svelte/store'
interface Modals {
@@ -6,6 +6,10 @@ interface Modals {
open: boolean
item: PostView | CommentView | undefined
}
removing: {
open: boolean
item: PostView | CommentView | undefined
}
}
export let modals = writable<Modals>({
@@ -13,6 +17,10 @@ export let modals = writable<Modals>({
open: false,
item: undefined,
},
removing: {
open: false,
item: undefined,
},
})
export function report(item: PostView | CommentView) {
@@ -24,3 +32,20 @@ export function report(item: PostView | CommentView) {
},
}))
}
export function remove(item: PostView | CommentView) {
modals.update((m) => ({
...m,
removing: {
open: true,
item: item,
},
}))
}
export const isMod = (me: MyUserInfo, community: number) =>
me.moderates.map((c) => c.community.id).includes(community)
export const isModOfAny = (me?: MyUserInfo) => me && me.moderates.length > 0
export const isAdmin = (me: MyUserInfo) => me.local_user_view.person.admin