feat: add user banning

This commit is contained in:
Xyphyn committed 2023-07-31 17:02:54 -07:00
1 parent 47cc6bae67
commit d70548f6c7
8 files changed
+195 -5

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "photon-lemmy",
"version": "0.4.16",
"version": "0.4.17",
"private": true,
"scripts": {
"dev": "vite dev",
+4
View File
@@ -6,6 +6,7 @@
export let value: string = ''
export let maxlength: number = -1
export let rows: number = 3
export let required: boolean = false
let clazz = ''
export { clazz as class }
@@ -19,6 +20,9 @@
class="font-bold text-sm text-left mb-1 cursor-pointer w-max self-start"
>
{label}
{#if required}
<span class="font-bold text-red-500">*</span>
{/if}
</span>
{/if}
<textarea
@@ -0,0 +1,122 @@
<script lang="ts">
import Button from '$lib/components/input/Button.svelte'
import TextArea from '$lib/components/input/TextArea.svelte'
import UserItem from '$lib/components/lemmy/user/UserItem.svelte'
import Avatar from '$lib/components/ui/Avatar.svelte'
import Modal from '$lib/components/ui/modal/Modal.svelte'
import type { Community, Person, PersonView } from 'lemmy-js-client'
import { authData, getClient, user } from '$lib/lemmy.js'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import TextInput from '$lib/components/input/TextInput.svelte'
export let open = false
let item: Person | undefined
export { item as user }
export let community: Community | undefined
export let banned: boolean
let reason = ''
let deleteData = false
let expires = ''
let loading = false
// hack due to svelte's reactive declarations
const resetReason = () => (reason = '')
$: if (item) resetReason()
async function submit() {
if (!item || !$user || !community || !$authData) return
loading = true
try {
let date: Date = new Date()
if (expires != '') {
date = new Date(expires)
if (date.toLocaleString('en') == 'Invalid Date') {
toast({
content: 'Invalid date. It must be an absolute date.',
type: ToastType.error,
})
loading = false
return
}
if (date.getTime() < Date.now()) {
toast({
content: 'Invalid date. It is before the current time.',
type: ToastType.error,
})
loading = false
return
}
}
await getClient().banFromCommunity({
auth: $authData.token,
ban: !banned,
community_id: community.id,
person_id: item.id,
reason: reason || undefined,
remove_data: deleteData,
expires: date.getTime(),
})
open = false
toast({
content: `Successfully ${
banned ? 'unbanned' : 'banned'
} that user. You may need to refresh to see changes.`,
type: ToastType.success,
})
item.banned = !banned
} catch (err) {
toast({
content: err as any,
type: ToastType.error,
})
}
loading = false
}
</script>
<Modal bind:open>
<h1 slot="title">{banned ? 'Unbanning' : 'Banning'} user</h1>
{#if item}
<form class="flex flex-col gap-4" on:submit|preventDefault={submit}>
<div class="flex items-center gap-1">
<Avatar url={item.avatar} alt={item.name} width={24} />
<span class="font-bold">{item.name}</span>
</div>
<TextArea
required
bind:value={reason}
label="Reason"
placeholder="Why are you banning {item.name}?"
/>
<div>
<label for="delete-data" class="block my-1 font-bold text-sm">
<input id="delete-data" type="checkbox" bind:value={deleteData} />
Delete data
</label>
</div>
<TextInput
bind:value={expires}
label="Expires"
placeholder="2024 August 5"
/>
<Button submit color="primary" {loading} disabled={loading} size="lg">
Submit
</Button>
</form>
{/if}
</Modal>
@@ -6,11 +6,11 @@
CommunityView,
PostView,
} from 'lemmy-js-client'
import { amMod, remove } from './moderation'
import { amMod, ban, 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 { Icon, ShieldExclamation, Trash } from 'svelte-hero-icons'
import { Color } from '$lib/ui/colors.js'
import { isComment } from '$lib/lemmy/item.js'
@@ -53,4 +53,19 @@
{item.post.removed ? 'Restore' : 'Remove'}
{/if}
</MenuButton>
{#if $user && $user.local_user_view.person.id != item.creator.id}
<span class="px-4 py-1 my-1 text-xs text-slate-600 dark:text-zinc-400">
User
</span>
<MenuButton
color="dangerSecondary"
on:click={() =>
ban(item.creator_banned_from_community, item.creator, item.community)}
>
<Icon src={ShieldExclamation} size="16" mini />
{item.creator_banned_from_community
? 'Unban from community'
: 'Ban from community'}
</MenuButton>
{/if}
</Menu>
@@ -1,4 +1,5 @@
<script lang="ts">
import BanModal from '$lib/components/lemmy/moderation/BanModal.svelte'
import RemoveModal from '$lib/components/lemmy/moderation/RemoveModal.svelte'
import { modals } from '$lib/components/lemmy/moderation/moderation.js'
import ReportModal from './ReportModal.svelte'
@@ -6,3 +7,9 @@
<ReportModal bind:open={$modals.reporting.open} item={$modals.reporting.item} />
<RemoveModal bind:open={$modals.removing.open} item={$modals.removing.item} />
<BanModal
bind:open={$modals.banning.open}
banned={$modals.banning.banned}
user={$modals.banning.user}
community={$modals.banning.community}
/>
@@ -6,7 +6,7 @@
CommunityView,
PostView,
} from 'lemmy-js-client'
import { amMod, remove } from './moderation'
import { amMod, ban, 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'
@@ -15,6 +15,7 @@
InformationCircle,
LockClosed,
LockOpen,
ShieldExclamation,
Trash,
} from 'svelte-hero-icons'
import { Color } from '$lib/ui/colors.js'
@@ -148,4 +149,19 @@
{item.post.removed ? 'Restore' : 'Remove'}
{/if}
</MenuButton>
{#if $user && $user.local_user_view.person.id != item.creator.id}
<span class="px-4 py-1 my-1 text-xs text-slate-600 dark:text-zinc-400">
User
</span>
<MenuButton
color="dangerSecondary"
on:click={() =>
ban(item.creator_banned_from_community, item.creator, item.community)}
>
<Icon src={ShieldExclamation} size="16" mini />
{item.creator_banned_from_community
? 'Unban from community'
: 'Ban from community'}
</MenuButton>
{/if}
</Menu>
@@ -3,6 +3,8 @@ import type {
Community,
CommunityView,
MyUserInfo,
Person,
PersonView,
PostView,
} from 'lemmy-js-client'
import { writable } from 'svelte/store'
@@ -16,6 +18,12 @@ interface Modals {
open: boolean
item: PostView | CommentView | undefined
}
banning: {
open: boolean
banned: boolean
user: Person | undefined
community: Community | undefined
}
}
export let modals = writable<Modals>({
@@ -27,6 +35,12 @@ export let modals = writable<Modals>({
open: false,
item: undefined,
},
banning: {
open: false,
banned: false,
user: undefined,
community: undefined,
},
})
export function report(item: PostView | CommentView) {
@@ -49,6 +63,18 @@ export function remove(item: PostView | CommentView) {
}))
}
export function ban(banned: boolean, item: Person, community: Community) {
modals.update((m) => ({
...m,
banning: {
open: true,
user: item,
banned: banned,
community,
},
}))
}
export const amMod = (me: MyUserInfo, community: Community) =>
me.moderates.map((c) => c.community.id).includes(community.id) ||
(community.local && isAdmin(me))
+1 -1
View File
@@ -3,7 +3,7 @@
// @ts-ignore
import { build, files } from '$service-worker'
const version = '0.4.16'
const version = '0.4.17'
const timestamp = new Date().toLocaleString('en')
const worker = self as unknown as ServiceWorkerGlobalScope
const FILES = `cache${timestamp}`