diff --git a/backend/open_webui/models/chats.py b/backend/open_webui/models/chats.py index 35e7b0653be..1fe5ed595d8 100644 --- a/backend/open_webui/models/chats.py +++ b/backend/open_webui/models/chats.py @@ -266,83 +266,6 @@ def add_message_status_to_chat_by_id_and_message_id( chat["history"] = history return self.update_chat_by_id(id, chat) - def insert_shared_chat_by_chat_id(self, chat_id: str) -> Optional[ChatModel]: - with get_db() as db: - # Get the existing chat to share - chat = db.get(Chat, chat_id) - # Check if the chat is already shared - if chat.share_id: - return self.get_chat_by_id_and_user_id(chat.share_id, "shared") - # Create a new chat with the same data, but with a new ID - shared_chat = ChatModel( - **{ - "id": str(uuid.uuid4()), - "user_id": f"shared-{chat_id}", - "title": chat.title, - "chat": chat.chat, - "created_at": chat.created_at, - "updated_at": int(time.time()), - } - ) - shared_result = Chat(**shared_chat.model_dump()) - db.add(shared_result) - db.commit() - db.refresh(shared_result) - - # Update the original chat with the share_id - result = ( - db.query(Chat) - .filter_by(id=chat_id) - .update({"share_id": shared_chat.id}) - ) - db.commit() - return shared_chat if (shared_result and result) else None - - def update_shared_chat_by_chat_id(self, chat_id: str) -> Optional[ChatModel]: - try: - with get_db() as db: - chat = db.get(Chat, chat_id) - shared_chat = ( - db.query(Chat).filter_by(user_id=f"shared-{chat_id}").first() - ) - - if shared_chat is None: - return self.insert_shared_chat_by_chat_id(chat_id) - - shared_chat.title = chat.title - shared_chat.chat = chat.chat - - shared_chat.updated_at = int(time.time()) - db.commit() - db.refresh(shared_chat) - - return ChatModel.model_validate(shared_chat) - except Exception: - return None - - def delete_shared_chat_by_chat_id(self, chat_id: str) -> bool: - try: - with get_db() as db: - db.query(Chat).filter_by(user_id=f"shared-{chat_id}").delete() - db.commit() - - return True - except Exception: - return False - - def update_chat_share_id_by_id( - self, id: str, share_id: Optional[str] - ) -> Optional[ChatModel]: - try: - with get_db() as db: - chat = db.get(Chat, id) - chat.share_id = share_id - db.commit() - db.refresh(chat) - return ChatModel.model_validate(chat) - except Exception: - return None - def toggle_chat_pinned_by_id(self, id: str) -> Optional[ChatModel]: try: with get_db() as db: @@ -470,20 +393,6 @@ def get_chat_by_id(self, id: str) -> Optional[ChatModel]: except Exception: return None - def get_chat_by_share_id(self, id: str) -> Optional[ChatModel]: - try: - with get_db() as db: - # it is possible that the shared link was deleted. hence, - # we check if the chat is still shared by checking if a chat with the share_id exists - chat = db.query(Chat).filter_by(share_id=id).first() - - if chat: - return self.get_chat_by_id(id) - else: - return None - except Exception: - return None - def get_chat_by_id_and_user_id(self, id: str, user_id: str) -> Optional[ChatModel]: try: with get_db() as db: @@ -857,25 +766,13 @@ def delete_chat_by_id(self, id: str) -> bool: db.query(Chat).filter_by(id=id).delete() db.commit() - return True and self.delete_shared_chat_by_chat_id(id) - except Exception: - return False - - def delete_chat_by_id_and_user_id(self, id: str, user_id: str) -> bool: - try: - with get_db() as db: - db.query(Chat).filter_by(id=id, user_id=user_id).delete() - db.commit() - - return True and self.delete_shared_chat_by_chat_id(id) + return True except Exception: return False def delete_chats_by_user_id(self, user_id: str) -> bool: try: with get_db() as db: - self.delete_shared_chats_by_user_id(user_id) - db.query(Chat).filter_by(user_id=user_id).delete() db.commit() @@ -895,18 +792,4 @@ def delete_chats_by_user_id_and_folder_id( except Exception: return False - def delete_shared_chats_by_user_id(self, user_id: str) -> bool: - try: - with get_db() as db: - chats_by_user = db.query(Chat).filter_by(user_id=user_id).all() - shared_chat_ids = [f"shared-{chat.id}" for chat in chats_by_user] - - db.query(Chat).filter(Chat.user_id.in_(shared_chat_ids)).delete() - db.commit() - - return True - except Exception: - return False - - Chats = ChatTable() diff --git a/backend/open_webui/routers/chats.py b/backend/open_webui/routers/chats.py index f3389b6541f..dd33c48990d 100644 --- a/backend/open_webui/routers/chats.py +++ b/backend/open_webui/routers/chats.py @@ -290,32 +290,6 @@ async def archive_all_chats(user=Depends(get_verified_user)): return Chats.archive_all_chats_by_user_id(user.id) -############################ -# GetSharedChatById -############################ - - -@router.get("/share/{share_id}", response_model=Optional[ChatResponse]) -async def get_shared_chat_by_id(share_id: str, user=Depends(get_verified_user)): - if user.role == "pending": - raise HTTPException( - status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND - ) - - if user.role == "user" or (user.role == "admin" and not ENABLE_ADMIN_CHAT_ACCESS): - chat = Chats.get_chat_by_share_id(share_id) - elif user.role == "admin" and ENABLE_ADMIN_CHAT_ACCESS: - chat = Chats.get_chat_by_id(share_id) - - if chat: - return ChatResponse(**chat.model_dump()) - - else: - raise HTTPException( - status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND - ) - - ############################ # GetChatsByTags ############################ @@ -582,36 +556,6 @@ async def clone_chat_by_id( status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.DEFAULT() ) - -############################ -# CloneSharedChatById -############################ - - -@router.post("/{id}/clone/shared", response_model=Optional[ChatResponse]) -async def clone_shared_chat_by_id(id: str, user=Depends(get_verified_user)): - - if user.role == "admin": - chat = Chats.get_chat_by_id(id) - else: - chat = Chats.get_chat_by_share_id(id) - - if chat: - updated_chat = { - **chat.chat, - "originalChatId": chat.id, - "branchPointMessageId": chat.chat["history"]["currentId"], - "title": f"Clone of {chat.title}", - } - - chat = Chats.insert_new_chat(user.id, ChatForm(**{"chat": updated_chat})) - return ChatResponse(**chat.model_dump()) - else: - raise HTTPException( - status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.DEFAULT() - ) - - ############################ # ArchiveChat ############################ @@ -642,58 +586,6 @@ async def archive_chat_by_id(id: str, user=Depends(get_verified_user)): status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.DEFAULT() ) - -############################ -# ShareChatById -############################ - - -@router.post("/{id}/share", response_model=Optional[ChatResponse]) -async def share_chat_by_id(id: str, user=Depends(get_verified_user)): - chat = Chats.get_chat_by_id_and_user_id(id, user.id) - if chat: - if chat.share_id: - shared_chat = Chats.update_shared_chat_by_chat_id(chat.id) - return ChatResponse(**shared_chat.model_dump()) - - shared_chat = Chats.insert_shared_chat_by_chat_id(chat.id) - if not shared_chat: - raise HTTPException( - status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, - detail=ERROR_MESSAGES.DEFAULT(), - ) - return ChatResponse(**shared_chat.model_dump()) - - else: - raise HTTPException( - status_code=status.HTTP_401_UNAUTHORIZED, - detail=ERROR_MESSAGES.ACCESS_PROHIBITED, - ) - - -############################ -# DeletedSharedChatById -############################ - - -@router.delete("/{id}/share", response_model=Optional[bool]) -async def delete_shared_chat_by_id(id: str, user=Depends(get_verified_user)): - chat = Chats.get_chat_by_id_and_user_id(id, user.id) - if chat: - if not chat.share_id: - return False - - result = Chats.delete_shared_chat_by_chat_id(id) - update_result = Chats.update_chat_share_id_by_id(id, None) - - return result and update_result != None - else: - raise HTTPException( - status_code=status.HTTP_401_UNAUTHORIZED, - detail=ERROR_MESSAGES.ACCESS_PROHIBITED, - ) - - ############################ # UpdateChatFolderIdById ############################ diff --git a/src/lib/apis/chats/index.ts b/src/lib/apis/chats/index.ts index 02bdd4eb31e..92fa7b801e4 100644 --- a/src/lib/apis/chats/index.ts +++ b/src/lib/apis/chats/index.ts @@ -472,37 +472,6 @@ export const getChatById = async (token: string, id: string) => { return res; }; -export const getChatByShareId = async (token: string, share_id: string) => { - let error = null; - - const res = await fetch(`${WEBUI_API_BASE_URL}/chats/share/${share_id}`, { - method: 'GET', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - ...(token && { authorization: `Bearer ${token}` }) - } - }) - .then(async (res) => { - if (!res.ok) throw await res.json(); - return res.json(); - }) - .then((json) => { - return json; - }) - .catch((err) => { - error = err; - - console.log(err); - return null; - }); - - if (error) { - throw error; - } - - return res; -}; export const getChatPinnedStatusById = async (token: string, id: string) => { let error = null; @@ -621,76 +590,6 @@ export const cloneChatById = async (token: string, id: string, title?: string) = return res; }; -export const cloneSharedChatById = async (token: string, id: string) => { - let error = null; - - const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/clone/shared`, { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - ...(token && { authorization: `Bearer ${token}` }) - } - }) - .then(async (res) => { - if (!res.ok) throw await res.json(); - return res.json(); - }) - .then((json) => { - return json; - }) - .catch((err) => { - error = err; - - if ('detail' in err) { - error = err.detail; - } else { - error = err; - } - - console.log(err); - return null; - }); - - if (error) { - throw error; - } - - return res; -}; - -export const shareChatById = async (token: string, id: string) => { - let error = null; - - const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/share`, { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - ...(token && { authorization: `Bearer ${token}` }) - } - }) - .then(async (res) => { - if (!res.ok) throw await res.json(); - return res.json(); - }) - .then((json) => { - return json; - }) - .catch((err) => { - error = err; - - console.log(err); - return null; - }); - - if (error) { - throw error; - } - - return res; -}; - export const updateChatFolderIdById = async (token: string, id: string, folderId?: string) => { let error = null; @@ -758,38 +657,6 @@ export const archiveChatById = async (token: string, id: string) => { return res; }; -export const deleteSharedChatById = async (token: string, id: string) => { - let error = null; - - const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/share`, { - method: 'DELETE', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - ...(token && { authorization: `Bearer ${token}` }) - } - }) - .then(async (res) => { - if (!res.ok) throw await res.json(); - return res.json(); - }) - .then((json) => { - return json; - }) - .catch((err) => { - error = err; - - console.log(err); - return null; - }); - - if (error) { - throw error; - } - - return res; -}; - export const updateChatById = async (token: string, id: string, chat: object) => { let error = null; diff --git a/src/lib/components/chat/Navbar.svelte b/src/lib/components/chat/Navbar.svelte index 4d51b42af7b..dc2e99333e4 100644 --- a/src/lib/components/chat/Navbar.svelte +++ b/src/lib/components/chat/Navbar.svelte @@ -20,7 +20,6 @@ import { goto } from '$app/navigation'; import { page } from '$app/stores'; - import ShareChatModal from '../chat/ShareChatModal.svelte'; import ModelSelector from '../chat/ModelSelector.svelte'; import Tooltip from '../common/Tooltip.svelte'; import Menu from '$lib/components/layout/Navbar/Menu.svelte'; @@ -50,8 +49,6 @@ let showDownloadChatModal = false; - -