chats.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. ChatIdTagForm,
  20. ChatTagsResponse,
  21. Tags,
  22. )
  23. from utils.utils import (
  24. bearer_scheme,
  25. )
  26. from constants import ERROR_MESSAGES
  27. router = APIRouter()
  28. ############################
  29. # GetChats
  30. ############################
  31. @router.get("/", response_model=List[ChatTitleIdResponse])
  32. async def get_user_chats(
  33. user=Depends(get_current_user), skip: int = 0, limit: int = 50
  34. ):
  35. return Chats.get_chat_lists_by_user_id(user.id, skip, limit)
  36. ############################
  37. # GetAllChats
  38. ############################
  39. @router.get("/all", response_model=List[ChatResponse])
  40. async def get_all_user_chats(user=Depends(get_current_user)):
  41. return [
  42. ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  43. for chat in Chats.get_all_chats_by_user_id(user.id)
  44. ]
  45. ############################
  46. # CreateNewChat
  47. ############################
  48. @router.post("/new", response_model=Optional[ChatResponse])
  49. async def create_new_chat(form_data: ChatForm, user=Depends(get_current_user)):
  50. try:
  51. chat = Chats.insert_new_chat(user.id, form_data)
  52. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  53. except Exception as e:
  54. print(e)
  55. raise HTTPException(
  56. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  57. )
  58. ############################
  59. # GetChatById
  60. ############################
  61. @router.get("/{id}", response_model=Optional[ChatResponse])
  62. async def get_chat_by_id(id: str, user=Depends(get_current_user)):
  63. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  64. if chat:
  65. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  66. else:
  67. raise HTTPException(
  68. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  69. )
  70. ############################
  71. # UpdateChatById
  72. ############################
  73. @router.post("/{id}", response_model=Optional[ChatResponse])
  74. async def update_chat_by_id(
  75. id: str, form_data: ChatForm, user=Depends(get_current_user)
  76. ):
  77. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  78. if chat:
  79. updated_chat = {**json.loads(chat.chat), **form_data.chat}
  80. chat = Chats.update_chat_by_id(id, updated_chat)
  81. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  82. else:
  83. raise HTTPException(
  84. status_code=status.HTTP_401_UNAUTHORIZED,
  85. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  86. )
  87. ############################
  88. # DeleteChatById
  89. ############################
  90. @router.delete("/{id}", response_model=bool)
  91. async def delete_chat_by_id(id: str, user=Depends(get_current_user)):
  92. result = Chats.delete_chat_by_id_and_user_id(id, user.id)
  93. return result
  94. ############################
  95. # GetChatTagsById
  96. ############################
  97. @router.get("/{id}/tags", response_model=List[TagModel])
  98. async def get_chat_tags_by_id(id: str, user=Depends(get_current_user)):
  99. tags = Tags.get_tags_by_chat_id_and_user_id(id, user.id)
  100. if tags:
  101. return tags
  102. else:
  103. raise HTTPException(
  104. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  105. )
  106. ############################
  107. # AddChatTagById
  108. ############################
  109. @router.post("/{id}/tags", response_model=Optional[ChatTagsResponse])
  110. async def add_chat_tag_by_id(
  111. id: str, form_data: ChatIdTagForm, user=Depends(get_current_user)
  112. ):
  113. tag = Tags.add_tag_to_chat(user.id, {"tag_name": form_data.tag_name, "chat_id": id})
  114. if tag:
  115. return tag
  116. else:
  117. raise HTTPException(
  118. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  119. )
  120. ############################
  121. # DeleteChatTagById
  122. ############################
  123. @router.delete("/{id}/tags", response_model=Optional[bool])
  124. async def add_chat_tag_by_id(
  125. id: str, form_data: ChatIdTagForm, user=Depends(get_current_user)
  126. ):
  127. tag = Tags.delete_tag_by_tag_name_and_chat_id_and_user_id(
  128. form_data.tag_name, id, user.id
  129. )
  130. if tag:
  131. return tag
  132. else:
  133. raise HTTPException(
  134. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  135. )
  136. ############################
  137. # DeleteAllChats
  138. ############################
  139. @router.delete("/", response_model=bool)
  140. async def delete_all_user_chats(user=Depends(get_current_user)):
  141. result = Chats.delete_chats_by_user_id(user.id)
  142. return result