From 98eeadab4223040264a71c264e3b9ae61bdcb33c Mon Sep 17 00:00:00 2001 From: Xyphyn Date: Mon, 28 Aug 2023 16:28:44 -0700 Subject: [PATCH] feat: make admin adding and removal consistent with mods --- package.json | 2 +- src/lib/lemmy/user.ts | 17 ++++++ src/lib/util.ts | 4 ++ src/routes/admin/team/+page.svelte | 83 +++++++++++++++++++++++++----- 4 files changed, 92 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 8ca40882..2edae784 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "photon-lemmy", - "version": "1.0.9", + "version": "1.1.0", "private": true, "scripts": { "dev": "vite dev", diff --git a/src/lib/lemmy/user.ts b/src/lib/lemmy/user.ts index fea26b47..fc6f2085 100644 --- a/src/lib/lemmy/user.ts +++ b/src/lib/lemmy/user.ts @@ -1,5 +1,6 @@ import { profile } from '$lib/auth.js' import { getClient } from '$lib/lemmy.js' +import { trycatch } from '$lib/util.js' import type { Community, MyUserInfo, PersonView } from 'lemmy-js-client' import { get } from 'svelte/store' @@ -48,3 +49,19 @@ export const addSubscription = ( 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, + }) + }) diff --git a/src/lib/util.ts b/src/lib/util.ts index ba84dbc7..7ebed6ca 100644 --- a/src/lib/util.ts +++ b/src/lib/util.ts @@ -94,3 +94,7 @@ export const trycatch = (func: () => T): Maybe => { }) } } + +export const removeItem = (array: T[], predicate: (item: T) => boolean) => { + array.splice(array.findIndex(predicate), 1) +} diff --git a/src/routes/admin/team/+page.svelte b/src/routes/admin/team/+page.svelte index c1b8fc83..727c3dfc 100644 --- a/src/routes/admin/team/+page.svelte +++ b/src/routes/admin/team/+page.svelte @@ -2,15 +2,22 @@ import { goto } from '$app/navigation' import { profile } from '$lib/auth.js' 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 Placeholder from '$lib/components/ui/Placeholder.svelte' import EditableList from '$lib/components/ui/list/EditableList.svelte' import { toast } from '$lib/components/ui/toasts/toasts.js' + import { instance } from '$lib/instance.js' import { getClient } from '$lib/lemmy.js' - import { trycatch } from '$lib/util.js' - import { Trash } from 'svelte-hero-icons' + import { addAdmin } from '$lib/lemmy/user.js' + import { removeItem, trycatch } from '$lib/util.js' + import { Plus, QuestionMarkCircle, Trash } from 'svelte-hero-icons' export let data + let newAdmin: string = '', + adding: boolean = false + async function removeAdmin(id: number, confirm: boolean): Promise { if (!confirm) return toast({ @@ -29,6 +36,7 @@ ) if (result) { + data.site!.admins = result.admins toast({ content: 'Removed that admin.', type: 'success', @@ -38,15 +46,64 @@

Admins

- removeAdmin(e.detail, false)}> - {#each data.site?.admins ?? [] as admin} -
- -
- {/each} -
+ {:else} + {#each data.site?.admins ?? [] as admin} +
+ +
+ {/each} + {/if} + +
{ + 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 + }) + }} + > + + + +{/if}