From a8b92e5e9d6a559a967e1747409323e92d4d7f96 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sat, 18 May 2024 14:19:48 -0700 Subject: [PATCH] fix: display username in shared chats --- backend/apps/web/routers/users.py | 26 +++++++++++++++++- src/lib/apis/users/index.ts | 27 +++++++++++++++++++ src/lib/components/chat/Messages.svelte | 5 ++-- .../chat/Messages/UserMessage.svelte | 4 ++- .../chat/Settings/Personalization.svelte | 2 +- src/routes/s/[id]/+page.svelte | 5 ++++ 6 files changed, 64 insertions(+), 5 deletions(-) diff --git a/backend/apps/web/routers/users.py b/backend/apps/web/routers/users.py index d87854e89..e9d74a119 100644 --- a/backend/apps/web/routers/users.py +++ b/backend/apps/web/routers/users.py @@ -12,7 +12,7 @@ import logging from apps.web.models.users import UserModel, UserUpdateForm, UserRoleUpdateForm, Users from apps.web.models.auths import Auths -from utils.utils import get_current_user, get_password_hash, get_admin_user +from utils.utils import get_verified_user, get_password_hash, get_admin_user from constants import ERROR_MESSAGES from config import SRC_LOG_LEVELS @@ -67,6 +67,30 @@ async def update_user_role(form_data: UserRoleUpdateForm, user=Depends(get_admin ) +############################ +# GetUserById +############################ + + +class UserResponse(BaseModel): + name: str + profile_image_url: str + + +@router.get("/{user_id}", response_model=UserResponse) +async def get_user_by_id(user_id: str, user=Depends(get_verified_user)): + + user = Users.get_user_by_id(user_id) + + if user: + return UserResponse(name=user.name, profile_image_url=user.profile_image_url) + else: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail=ERROR_MESSAGES.USER_NOT_FOUND, + ) + + ############################ # UpdateUserById ############################ diff --git a/src/lib/apis/users/index.ts b/src/lib/apis/users/index.ts index 788e8820f..2d2bd386f 100644 --- a/src/lib/apis/users/index.ts +++ b/src/lib/apis/users/index.ts @@ -115,6 +115,33 @@ export const getUsers = async (token: string) => { return res ? res : []; }; +export const getUserById = async (token: string, userId: string) => { + let error = null; + + const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}` + } + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + console.log(err); + error = err.detail; + return null; + }); + + if (error) { + throw error; + } + + return res; +}; + export const deleteUserById = async (token: string, userId: string) => { let error = null; diff --git a/src/lib/components/chat/Messages.svelte b/src/lib/components/chat/Messages.svelte index 6d314835b..b99ebc172 100644 --- a/src/lib/components/chat/Messages.svelte +++ b/src/lib/components/chat/Messages.svelte @@ -1,7 +1,7 @@