소스 검색

enh: channel delete

Timothy Jaeryang Baek 4 달 전
부모
커밋
4c756b5501

+ 23 - 0
backend/open_webui/routers/channels.py

@@ -103,6 +103,29 @@ async def update_channel_by_id(
         )
 
 
+############################
+# DeleteChannelById
+############################
+
+
+@router.delete("/{id}/delete", response_model=bool)
+async def delete_channel_by_id(id: str, user=Depends(get_admin_user)):
+    channel = Channels.get_channel_by_id(id)
+    if not channel:
+        raise HTTPException(
+            status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
+        )
+
+    try:
+        Channels.delete_channel_by_id(id)
+        return True
+    except Exception as e:
+        log.exception(e)
+        raise HTTPException(
+            status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
+        )
+
+
 ############################
 # GetChannelMessages
 ############################

+ 31 - 0
src/lib/apis/channels/index.ts

@@ -134,6 +134,37 @@ export const updateChannelById = async (token: string = '', channel_id: string,
 	return res;
 }
 
+export const deleteChannelById = async (token: string = '', channel_id: string) => {
+	let error = null;
+
+	const res = await fetch(`${WEBUI_API_BASE_URL}/channels/${channel_id}/delete`, {
+		method: 'DELETE',
+		headers: {
+			Accept: 'application/json',
+			'Content-Type': 'application/json',
+			authorization: `Bearer ${token}`
+		}
+	})
+		.then(async (res) => {
+			if (!res.ok) throw await res.json();
+			return res.json();
+		})
+		.then((json) => {
+			return json;
+		})
+		.catch((err) => {
+			error = err.detail;
+			console.log(err);
+			return null;
+		});
+
+	if (error) {
+		throw error;
+	}
+
+	return res;
+}
+
 
 export const getChannelMessages = async (token: string = '', channel_id: string, page: number = 1) => {
 	let error = null;

+ 1 - 0
src/lib/components/layout/Sidebar/ChannelItem.svelte

@@ -24,6 +24,7 @@
 	bind:show={showEditChannelModal}
 	{channel}
 	edit={true}
+	{onUpdate}
 	onSubmit={async ({ name, access_control }) => {
 		const res = await updateChannelById(localStorage.token, channel.id, {
 			name,

+ 18 - 1
src/lib/components/layout/Sidebar/ChannelModal.svelte

@@ -1,16 +1,19 @@
 <script lang="ts">
 	import { getContext, createEventDispatcher, onMount } from 'svelte';
-	import { createNewChannel } from '$lib/apis/channels';
+	import { createNewChannel, deleteChannelById } from '$lib/apis/channels';
 
 	import Modal from '$lib/components/common/Modal.svelte';
 	import AccessControl from '$lib/components/workspace/common/AccessControl.svelte';
 	import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
 
 	import { toast } from 'svelte-sonner';
+	import { page } from '$app/stores';
+	import { goto } from '$app/navigation';
 	const i18n = getContext('i18n');
 
 	export let show = false;
 	export let onSubmit: Function = () => {};
+	export let onUpdate: Function = () => {};
 
 	export let channel = null;
 	export let edit = false;
@@ -47,6 +50,20 @@
 
 	const deleteHandler = async () => {
 		showDeleteConfirmDialog = false;
+
+		const res = await deleteChannelById(localStorage.token, channel.id).catch((error) => {
+			toast.error(error.message);
+		});
+
+		if (res) {
+			toast.success('Channel deleted successfully');
+			onUpdate();
+
+			if ($page.url.pathname === `/channels/${channel.id}`) {
+				goto('/');
+			}
+		}
+
 		show = false;
 	};
 </script>