diff --git a/backend/apps/web/models/chats.py b/backend/apps/web/models/chats.py index ff636f779..f874958f8 100644 --- a/backend/apps/web/models/chats.py +++ b/backend/apps/web/models/chats.py @@ -98,7 +98,7 @@ class ChatTable: except: return None - def insert_shared_chat(self, chat_id: str) -> Optional[ChatModel]: + def insert_shared_chat_by_chat_id(self, chat_id: str) -> Optional[ChatModel]: # Get the existing chat to share chat = Chat.get(Chat.id == chat_id) # Check if the chat is already shared @@ -122,6 +122,24 @@ class ChatTable: return shared_chat if (shared_result and result) else None + def update_shared_chat_by_chat_id(self, chat_id: str) -> Optional[ChatModel]: + try: + print("update_shared_chat_by_id") + chat = Chat.get(Chat.id == chat_id) + print(chat) + + query = Chat.update( + title=chat.title, + chat=chat.chat, + ).where(Chat.id == chat.share_id) + + query.execute() + + chat = Chat.get(Chat.id == chat.share_id) + return ChatModel(**model_to_dict(chat)) + except: + return None + def delete_shared_chat_by_chat_id(self, chat_id: str) -> bool: try: query = Chat.delete().where(Chat.user_id == f"shared-{chat_id}") diff --git a/backend/apps/web/routers/chats.py b/backend/apps/web/routers/chats.py index 2b8e85284..660a0d7f6 100644 --- a/backend/apps/web/routers/chats.py +++ b/backend/apps/web/routers/chats.py @@ -199,12 +199,12 @@ async def share_chat_by_id(id: str, user=Depends(get_current_user)): chat = Chats.get_chat_by_id_and_user_id(id, user.id) if chat: if chat.share_id: - shared_chat = Chats.get_chat_by_id_and_user_id(chat.share_id, "shared") + shared_chat = Chats.update_shared_chat_by_chat_id(chat.id) return ChatResponse( **{**shared_chat.model_dump(), "chat": json.loads(shared_chat.chat)} ) - shared_chat = Chats.insert_shared_chat(chat.id) + 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, diff --git a/src/lib/components/chat/ShareChatModal.svelte b/src/lib/components/chat/ShareChatModal.svelte index 286592eb5..62d03ed0a 100644 --- a/src/lib/components/chat/ShareChatModal.svelte +++ b/src/lib/components/chat/ShareChatModal.svelte @@ -18,13 +18,8 @@ const shareLocalChat = async () => { const _chat = chat; - let chatShareUrl = ''; - if (_chat.share_id) { - chatShareUrl = `${window.location.origin}/s/${_chat.share_id}`; - } else { - const sharedChat = await shareChatById(localStorage.token, $chatId); - chatShareUrl = `${window.location.origin}/s/${sharedChat.id}`; - } + const sharedChat = await shareChatById(localStorage.token, $chatId); + const chatShareUrl = `${window.location.origin}/s/${sharedChat.id}`; toast.success($i18n.t('Copied shared conversation URL to clipboard!')); copyToClipboard(chatShareUrl);