From 9975cb17a9104c4f4c1f41c3e19da759e21f8c2d Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 2 Apr 2024 07:42:37 -0700 Subject: [PATCH] feat: update shared chat --- backend/apps/web/models/chats.py | 20 ++++++++++++++++++- backend/apps/web/routers/chats.py | 4 ++-- src/lib/components/chat/ShareChatModal.svelte | 9 ++------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/backend/apps/web/models/chats.py b/backend/apps/web/models/chats.py index ff636f77..f874958f 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 2b8e8528..660a0d7f 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 286592eb..62d03ed0 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);