From 96d75dfc1b6cfd40d63ca82d6a85d5c28b81784e Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 2 Apr 2024 07:04:29 -0700 Subject: [PATCH] refac: share chat routes --- backend/apps/web/models/chats.py | 7 +++++++ backend/apps/web/routers/chats.py | 12 ++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/backend/apps/web/models/chats.py b/backend/apps/web/models/chats.py index 598494c81..6d2888058 100644 --- a/backend/apps/web/models/chats.py +++ b/backend/apps/web/models/chats.py @@ -181,6 +181,13 @@ class ChatTable: .order_by(Chat.timestamp.desc()) ] + def get_chat_by_id(self, id: str) -> Optional[ChatModel]: + try: + chat = Chat.get(Chat.id == id) + return ChatModel(**model_to_dict(chat)) + except: + return None + def get_chat_by_id_and_user_id(self, id: str, user_id: str) -> Optional[ChatModel]: try: chat = Chat.get(Chat.id == id, Chat.user_id == user_id) diff --git a/backend/apps/web/routers/chats.py b/backend/apps/web/routers/chats.py index 2637953eb..14b97db88 100644 --- a/backend/apps/web/routers/chats.py +++ b/backend/apps/web/routers/chats.py @@ -226,9 +226,9 @@ async def share_chat_by_id(id: str, user=Depends(get_current_user)): ############################ -@router.delete("/{id}/share", response_model=Optional[bool]) -async def delete_shared_chat_by_id(id: str, user=Depends(get_current_user)): - chat = Chats.get_chat_by_id_and_user_id(id, user.id) +@router.delete("/{share_id}/share", response_model=Optional[bool]) +async def delete_shared_chat_by_id(share_id: str, user=Depends(get_current_user)): + chat = Chats.get_chat_by_id_and_user_id(share_id, user.id) if chat: if not chat.share_id: return False @@ -248,9 +248,9 @@ async def delete_shared_chat_by_id(id: str, user=Depends(get_current_user)): ############################ -@router.get("/share/{id}", response_model=Optional[ChatResponse]) -async def get_shared_chat_by_id(id: str, user=Depends(get_current_user)): - chat = Chats.get_chat_by_id_and_user_id(id, "shared") +@router.get("/share/{share_id}", response_model=Optional[ChatResponse]) +async def get_shared_chat_by_id(share_id: str, user=Depends(get_current_user)): + chat = Chats.get_chat_by_id(share_id) if chat: return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})