From a4333295ce53f2c89c744c3ffc010f25cc414d87 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 22 Dec 2024 22:20:24 -0700 Subject: [PATCH] refac --- backend/open_webui/models/channels.py | 8 +++--- backend/open_webui/routers/channels.py | 21 +++++++++++++++ src/lib/apis/channels/index.ts | 32 +++++++++++++++++++++++ src/lib/components/channel/Channel.svelte | 32 ++++++++++++++++------- 4 files changed, 80 insertions(+), 13 deletions(-) diff --git a/backend/open_webui/models/channels.py b/backend/open_webui/models/channels.py index 43f01e2f3..1779e1566 100644 --- a/backend/open_webui/models/channels.py +++ b/backend/open_webui/models/channels.py @@ -69,11 +69,11 @@ class ChannelTable: with get_db() as db: channel = ChannelModel( **{ - **form_data.dict(), + **form_data.model_dump(), "id": str(uuid.uuid4()), "user_id": user_id, - "created_at": int(time.time()), - "updated_at": int(time.time()), + "created_at": int(time.time_ns()), + "updated_at": int(time.time_ns()), } ) @@ -116,7 +116,7 @@ class ChannelTable: channel.data = form_data.data channel.meta = form_data.meta channel.access_control = form_data.access_control - channel.updated_at = int(time.time()) + channel.updated_at = int(time.time_ns()) db.commit() return ChannelModel.model_validate(channel) if channel else None diff --git a/backend/open_webui/routers/channels.py b/backend/open_webui/routers/channels.py index 295a25a42..df3ccfd51 100644 --- a/backend/open_webui/routers/channels.py +++ b/backend/open_webui/routers/channels.py @@ -57,6 +57,27 @@ async def create_new_channel(form_data: ChannelForm, user=Depends(get_admin_user ) +############################ +# GetChannelById +############################ + + +@router.get("/{id}", response_model=Optional[ChannelModel]) +async def get_channel_by_id(id: str, user=Depends(get_verified_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 + ) + + if not has_access(user.id, type="read", access_control=channel.access_control): + raise HTTPException( + status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT() + ) + + return ChannelModel(**channel.model_dump()) + + ############################ # GetChannelMessages ############################ diff --git a/src/lib/apis/channels/index.ts b/src/lib/apis/channels/index.ts index 84f372fa0..abc3c2aec 100644 --- a/src/lib/apis/channels/index.ts +++ b/src/lib/apis/channels/index.ts @@ -71,6 +71,38 @@ export const getChannels = async (token: string = '') => { }; +export const getChannelById = async (token: string = '', channel_id: string) => { + let error = null; + + const res = await fetch(`${WEBUI_API_BASE_URL}/channels/${channel_id}`, { + method: 'GET', + 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; diff --git a/src/lib/components/channel/Channel.svelte b/src/lib/components/channel/Channel.svelte index dbcbca29f..dba69a9e0 100644 --- a/src/lib/components/channel/Channel.svelte +++ b/src/lib/components/channel/Channel.svelte @@ -1,11 +1,14 @@