chats.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. from fastapi import Depends, Request, HTTPException, status
  2. from datetime import datetime, timedelta
  3. from typing import List, Union, Optional
  4. from utils.utils import get_current_user
  5. from fastapi import APIRouter
  6. from pydantic import BaseModel
  7. import json
  8. from apps.web.models.users import Users
  9. from apps.web.models.chats import (
  10. ChatModel,
  11. ChatResponse,
  12. ChatTitleForm,
  13. ChatForm,
  14. ChatTitleIdResponse,
  15. Chats,
  16. )
  17. from apps.web.models.tags import (
  18. TagModel,
  19. ChatIdTagModel,
  20. ChatIdTagForm,
  21. ChatTagsResponse,
  22. Tags,
  23. )
  24. from utils.utils import (
  25. bearer_scheme,
  26. )
  27. from constants import ERROR_MESSAGES
  28. router = APIRouter()
  29. ############################
  30. # GetChats
  31. ############################
  32. @router.get("/", response_model=List[ChatTitleIdResponse])
  33. async def get_user_chats(
  34. user=Depends(get_current_user), skip: int = 0, limit: int = 50
  35. ):
  36. return Chats.get_chat_lists_by_user_id(user.id, skip, limit)
  37. ############################
  38. # GetAllChats
  39. ############################
  40. @router.get("/all", response_model=List[ChatResponse])
  41. async def get_all_user_chats(user=Depends(get_current_user)):
  42. return [
  43. ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  44. for chat in Chats.get_all_chats_by_user_id(user.id)
  45. ]
  46. ############################
  47. # CreateNewChat
  48. ############################
  49. @router.post("/new", response_model=Optional[ChatResponse])
  50. async def create_new_chat(form_data: ChatForm, user=Depends(get_current_user)):
  51. try:
  52. chat = Chats.insert_new_chat(user.id, form_data)
  53. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  54. except Exception as e:
  55. print(e)
  56. raise HTTPException(
  57. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  58. )
  59. ############################
  60. # GetChatById
  61. ############################
  62. @router.get("/{id}", response_model=Optional[ChatResponse])
  63. async def get_chat_by_id(id: str, user=Depends(get_current_user)):
  64. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  65. if chat:
  66. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  67. else:
  68. raise HTTPException(
  69. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  70. )
  71. ############################
  72. # UpdateChatById
  73. ############################
  74. @router.post("/{id}", response_model=Optional[ChatResponse])
  75. async def update_chat_by_id(
  76. id: str, form_data: ChatForm, user=Depends(get_current_user)
  77. ):
  78. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  79. if chat:
  80. updated_chat = {**json.loads(chat.chat), **form_data.chat}
  81. chat = Chats.update_chat_by_id(id, updated_chat)
  82. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  83. else:
  84. raise HTTPException(
  85. status_code=status.HTTP_401_UNAUTHORIZED,
  86. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  87. )
  88. ############################
  89. # DeleteChatById
  90. ############################
  91. @router.delete("/{id}", response_model=bool)
  92. async def delete_chat_by_id(id: str, user=Depends(get_current_user)):
  93. result = Chats.delete_chat_by_id_and_user_id(id, user.id)
  94. return result
  95. ############################
  96. # GetChatTagsById
  97. ############################
  98. @router.get("/{id}/tags", response_model=List[TagModel])
  99. async def get_chat_tags_by_id(id: str, user=Depends(get_current_user)):
  100. tags = Tags.get_tags_by_chat_id_and_user_id(id, user.id)
  101. if tags != None:
  102. return tags
  103. else:
  104. raise HTTPException(
  105. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  106. )
  107. ############################
  108. # AddChatTagById
  109. ############################
  110. @router.post("/{id}/tags", response_model=Optional[ChatIdTagModel])
  111. async def add_chat_tag_by_id(
  112. id: str, form_data: ChatIdTagForm, user=Depends(get_current_user)
  113. ):
  114. tags = Tags.get_tags_by_chat_id_and_user_id(id, user.id)
  115. if form_data.tag_name not in tags:
  116. tag = Tags.add_tag_to_chat(user.id, form_data)
  117. if tag:
  118. return tag
  119. else:
  120. raise HTTPException(
  121. status_code=status.HTTP_401_UNAUTHORIZED,
  122. detail=ERROR_MESSAGES.NOT_FOUND,
  123. )
  124. else:
  125. raise HTTPException(
  126. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.DEFAULT()
  127. )
  128. ############################
  129. # DeleteChatTagById
  130. ############################
  131. @router.delete("/{id}/tags", response_model=Optional[bool])
  132. async def delete_chat_tag_by_id(
  133. id: str, form_data: ChatIdTagForm, user=Depends(get_current_user)
  134. ):
  135. result = Tags.delete_tag_by_tag_name_and_chat_id_and_user_id(
  136. form_data.tag_name, id, user.id
  137. )
  138. if result:
  139. return result
  140. else:
  141. raise HTTPException(
  142. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  143. )
  144. ############################
  145. # DeleteAllChatTagsById
  146. ############################
  147. @router.delete("/{id}/tags/all", response_model=Optional[bool])
  148. async def delete_all_chat_tags_by_id(id: str, user=Depends(get_current_user)):
  149. result = Tags.delete_tags_by_chat_id_and_user_id(id, user.id)
  150. if result:
  151. return result
  152. else:
  153. raise HTTPException(
  154. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  155. )
  156. ############################
  157. # DeleteAllChats
  158. ############################
  159. @router.delete("/", response_model=bool)
  160. async def delete_all_user_chats(user=Depends(get_current_user)):
  161. result = Chats.delete_chats_by_user_id(user.id)
  162. return result