mirror of
https://github.com/open-webui/open-webui
synced 2024-12-28 06:42:47 +00:00
refac
This commit is contained in:
parent
5748f6ef77
commit
a4333295ce
@ -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
|
||||
|
@ -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
|
||||
############################
|
||||
|
@ -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;
|
||||
|
||||
|
@ -1,11 +1,14 @@
|
||||
<script lang="ts">
|
||||
import { getChannelMessages, sendMessage } from '$lib/apis/channels';
|
||||
import { toast } from 'svelte-sonner';
|
||||
import MessageInput from './MessageInput.svelte';
|
||||
import Messages from './Messages.svelte';
|
||||
import { socket } from '$lib/stores';
|
||||
import { onDestroy, onMount, tick } from 'svelte';
|
||||
|
||||
import { socket } from '$lib/stores';
|
||||
import { getChannelById, getChannelMessages, sendMessage } from '$lib/apis/channels';
|
||||
|
||||
import Messages from './Messages.svelte';
|
||||
import MessageInput from './MessageInput.svelte';
|
||||
import { goto } from '$app/navigation';
|
||||
|
||||
export let id = '';
|
||||
|
||||
let scrollEnd = true;
|
||||
@ -14,6 +17,7 @@
|
||||
let top = false;
|
||||
let page = 1;
|
||||
|
||||
let channel = null;
|
||||
let messages = null;
|
||||
|
||||
$: if (id) {
|
||||
@ -28,15 +32,24 @@
|
||||
top = false;
|
||||
page = 1;
|
||||
messages = null;
|
||||
channel = null;
|
||||
|
||||
messages = await getChannelMessages(localStorage.token, id, page);
|
||||
channel = await getChannelById(localStorage.token, id).catch((error) => {
|
||||
return null;
|
||||
});
|
||||
|
||||
if (messages) {
|
||||
messagesContainerElement.scrollTop = messagesContainerElement.scrollHeight;
|
||||
if (channel) {
|
||||
messages = await getChannelMessages(localStorage.token, id, page);
|
||||
|
||||
if (messages.length < 50) {
|
||||
top = true;
|
||||
if (messages) {
|
||||
messagesContainerElement.scrollTop = messagesContainerElement.scrollHeight;
|
||||
|
||||
if (messages.length < 50) {
|
||||
top = true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
goto('/');
|
||||
}
|
||||
};
|
||||
|
||||
@ -93,6 +106,7 @@
|
||||
>
|
||||
{#key id}
|
||||
<Messages
|
||||
{channel}
|
||||
{messages}
|
||||
{top}
|
||||
onLoad={async () => {
|
||||
|
Loading…
Reference in New Issue
Block a user