feat: make admin adding and removal consistent with mods
This commit is contained in:
1 parent
9d0694f138
commit
98eeadab42
4 files changed
+83
-5
No files matched your search
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "photon-lemmy",
|
"name": "photon-lemmy",
|
||||||
"version": "1.0.9",
|
"version": "1.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite dev",
|
"dev": "vite dev",
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { profile } from '$lib/auth.js'
|
import { profile } from '$lib/auth.js'
|
||||||
import { getClient } from '$lib/lemmy.js'
|
import { getClient } from '$lib/lemmy.js'
|
||||||
|
import { trycatch } from '$lib/util.js'
|
||||||
import type { Community, MyUserInfo, PersonView } from 'lemmy-js-client'
|
import type { Community, MyUserInfo, PersonView } from 'lemmy-js-client'
|
||||||
import { get } from 'svelte/store'
|
import { get } from 'svelte/store'
|
||||||
|
|
||||||
@@ -48,3 +49,19 @@ export const addSubscription = (
|
|||||||
profile.set(p)
|
profile.set(p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const addAdmin = async (handle: string, added: boolean, jwt: string) =>
|
||||||
|
trycatch(async () => {
|
||||||
|
const user = await getClient().resolveObject({
|
||||||
|
auth: jwt,
|
||||||
|
q: handle,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!user.person) throw new Error('No user found')
|
||||||
|
|
||||||
|
return await getClient().addAdmin({
|
||||||
|
auth: jwt,
|
||||||
|
added: true,
|
||||||
|
person_id: user.person.person.id,
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -94,3 +94,7 @@ export const trycatch = <T>(func: () => T): Maybe<T> => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const removeItem = <T>(array: T[], predicate: (item: T) => boolean) => {
|
||||||
|
array.splice(array.findIndex(predicate), 1)
|
||||||
|
}
|
||||||
@@ -2,15 +2,22 @@
|
|||||||
import { goto } from '$app/navigation'
|
import { goto } from '$app/navigation'
|
||||||
import { profile } from '$lib/auth.js'
|
import { profile } from '$lib/auth.js'
|
||||||
import Button from '$lib/components/input/Button.svelte'
|
import Button from '$lib/components/input/Button.svelte'
|
||||||
|
import TextInput from '$lib/components/input/TextInput.svelte'
|
||||||
import UserLink from '$lib/components/lemmy/user/UserLink.svelte'
|
import UserLink from '$lib/components/lemmy/user/UserLink.svelte'
|
||||||
|
import Placeholder from '$lib/components/ui/Placeholder.svelte'
|
||||||
import EditableList from '$lib/components/ui/list/EditableList.svelte'
|
import EditableList from '$lib/components/ui/list/EditableList.svelte'
|
||||||
import { toast } from '$lib/components/ui/toasts/toasts.js'
|
import { toast } from '$lib/components/ui/toasts/toasts.js'
|
||||||
|
import { instance } from '$lib/instance.js'
|
||||||
import { getClient } from '$lib/lemmy.js'
|
import { getClient } from '$lib/lemmy.js'
|
||||||
import { trycatch } from '$lib/util.js'
|
import { addAdmin } from '$lib/lemmy/user.js'
|
||||||
import { Trash } from 'svelte-hero-icons'
|
import { removeItem, trycatch } from '$lib/util.js'
|
||||||
|
import { Plus, QuestionMarkCircle, Trash } from 'svelte-hero-icons'
|
||||||
|
|
||||||
export let data
|
export let data
|
||||||
|
|
||||||
|
let newAdmin: string = '',
|
||||||
|
adding: boolean = false
|
||||||
|
|
||||||
async function removeAdmin(id: number, confirm: boolean): Promise<any> {
|
async function removeAdmin(id: number, confirm: boolean): Promise<any> {
|
||||||
if (!confirm)
|
if (!confirm)
|
||||||
return toast({
|
return toast({
|
||||||
@@ -29,6 +36,7 @@
|
|||||||
)
|
)
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
|
data.site!.admins = result.admins
|
||||||
toast({
|
toast({
|
||||||
content: 'Removed that admin.',
|
content: 'Removed that admin.',
|
||||||
type: 'success',
|
type: 'success',
|
||||||
@@ -38,7 +46,15 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h1 class="font-bold text-2xl">Admins</h1>
|
<h1 class="font-bold text-2xl">Admins</h1>
|
||||||
<EditableList let:action on:action={(e) => removeAdmin(e.detail, false)}>
|
{#if data.site}
|
||||||
|
<EditableList let:action on:action={(e) => removeAdmin(e.detail, false)}>
|
||||||
|
{#if data.site.admins.length <= 0}
|
||||||
|
<Placeholder
|
||||||
|
icon={QuestionMarkCircle}
|
||||||
|
title="No admins"
|
||||||
|
description="Somehow there's no admins of this site. How?!"
|
||||||
|
/>
|
||||||
|
{:else}
|
||||||
{#each data.site?.admins ?? [] as admin}
|
{#each data.site?.admins ?? [] as admin}
|
||||||
<div class="py-3 flex items-center justify-between">
|
<div class="py-3 flex items-center justify-between">
|
||||||
<UserLink avatar showInstance={false} user={admin.person} />
|
<UserLink avatar showInstance={false} user={admin.person} />
|
||||||
@@ -49,4 +65,45 @@
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
{/each}
|
{/each}
|
||||||
</EditableList>
|
{/if}
|
||||||
|
</EditableList>
|
||||||
|
<form
|
||||||
|
class="flex flex-row items-center gap-2 mt-auto w-full"
|
||||||
|
on:submit|preventDefault={() => {
|
||||||
|
trycatch(async () => {
|
||||||
|
if (!$profile?.jwt || newAdmin == '') return
|
||||||
|
adding = true
|
||||||
|
|
||||||
|
const r = await addAdmin(`${newAdmin}@${$instance}`, true, $profile.jwt)
|
||||||
|
if (!r) return
|
||||||
|
|
||||||
|
toast({
|
||||||
|
content: 'Successfully added that admin.',
|
||||||
|
type: 'success',
|
||||||
|
})
|
||||||
|
|
||||||
|
if (data.site) data.site.admins = r.admins
|
||||||
|
|
||||||
|
newAdmin = ''
|
||||||
|
adding = false
|
||||||
|
})
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<TextInput
|
||||||
|
bind:value={newAdmin}
|
||||||
|
placeholder="@user"
|
||||||
|
class="flex-1"
|
||||||
|
pattern={'@[^ |]{1,}'}
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
loading={adding}
|
||||||
|
disabled={adding}
|
||||||
|
icon={Plus}
|
||||||
|
size="md"
|
||||||
|
class="h-full"
|
||||||
|
submit
|
||||||
|
>
|
||||||
|
Add Admin
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
{/if}
|
||||||
Reference in new issue
Block a user