chats.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. from pydantic import BaseModel
  2. from typing import List, Union, Optional
  3. from peewee import *
  4. from playhouse.shortcuts import model_to_dict
  5. import json
  6. import uuid
  7. import time
  8. from apps.web.internal.db import DB
  9. ####################
  10. # Chat DB Schema
  11. ####################
  12. class Chat(Model):
  13. id = CharField(unique=True)
  14. user_id = CharField()
  15. title = CharField()
  16. chat = TextField() # Save Chat JSON as Text
  17. timestamp = DateField()
  18. class Meta:
  19. database = DB
  20. class ChatModel(BaseModel):
  21. id: str
  22. user_id: str
  23. title: str
  24. chat: str
  25. timestamp: int # timestamp in epoch
  26. ####################
  27. # Forms
  28. ####################
  29. class ChatForm(BaseModel):
  30. chat: dict
  31. class ChatTitleForm(BaseModel):
  32. title: str
  33. class ChatResponse(BaseModel):
  34. id: str
  35. user_id: str
  36. title: str
  37. chat: dict
  38. timestamp: int # timestamp in epoch
  39. class ChatTitleIdResponse(BaseModel):
  40. id: str
  41. title: str
  42. class ChatTable:
  43. def __init__(self, db):
  44. self.db = db
  45. db.create_tables([Chat])
  46. def insert_new_chat(self, user_id: str,
  47. form_data: ChatForm) -> Optional[ChatModel]:
  48. id = str(uuid.uuid4())
  49. chat = ChatModel(
  50. **{
  51. "id": id,
  52. "user_id": user_id,
  53. "title": form_data.chat["title"] if "title" in
  54. form_data.chat else "New Chat",
  55. "chat": json.dumps(form_data.chat),
  56. "timestamp": int(time.time()),
  57. })
  58. result = Chat.create(**chat.model_dump())
  59. return chat if result else None
  60. def update_chat_by_id(self, id: str, chat: dict) -> Optional[ChatModel]:
  61. try:
  62. query = Chat.update(
  63. chat=json.dumps(chat),
  64. title=chat["title"] if "title" in chat else "New Chat",
  65. timestamp=int(time.time()),
  66. ).where(Chat.id == id)
  67. query.execute()
  68. chat = Chat.get(Chat.id == id)
  69. return ChatModel(**model_to_dict(chat))
  70. except:
  71. return None
  72. def update_chat_by_id(self, id: str, chat: dict) -> Optional[ChatModel]:
  73. try:
  74. query = Chat.update(
  75. chat=json.dumps(chat),
  76. title=chat["title"] if "title" in chat else "New Chat",
  77. timestamp=int(time.time()),
  78. ).where(Chat.id == id)
  79. query.execute()
  80. chat = Chat.get(Chat.id == id)
  81. return ChatModel(**model_to_dict(chat))
  82. except:
  83. return None
  84. def get_chat_lists_by_user_id(self,
  85. user_id: str,
  86. skip: int = 0,
  87. limit: int = 50) -> List[ChatModel]:
  88. return [
  89. ChatModel(**model_to_dict(chat)) for chat in Chat.select().where(
  90. Chat.user_id == user_id).order_by(Chat.timestamp.desc())
  91. # .limit(limit)
  92. # .offset(skip)
  93. ]
  94. def get_all_chats_by_user_id(self, user_id: str) -> List[ChatModel]:
  95. return [
  96. ChatModel(**model_to_dict(chat)) for chat in Chat.select().where(
  97. Chat.user_id == user_id).order_by(Chat.timestamp.desc())
  98. ]
  99. def get_chat_by_id_and_user_id(self, id: str,
  100. user_id: str) -> Optional[ChatModel]:
  101. try:
  102. chat = Chat.get(Chat.id == id, Chat.user_id == user_id)
  103. return ChatModel(**model_to_dict(chat))
  104. except:
  105. return None
  106. def get_chats(self, skip: int = 0, limit: int = 50) -> List[ChatModel]:
  107. return [
  108. ChatModel(**model_to_dict(chat))
  109. for chat in Chat.select().limit(limit).offset(skip)
  110. ]
  111. def delete_chat_by_id_and_user_id(self, id: str, user_id: str) -> bool:
  112. try:
  113. query = Chat.delete().where((Chat.id == id)
  114. & (Chat.user_id == user_id))
  115. query.execute() # Remove the rows, return number of rows removed.
  116. return True
  117. except:
  118. return False
  119. def delete_chats_by_user_id(self, user_id: str) -> bool:
  120. try:
  121. query = Chat.delete().where(Chat.user_id == user_id)
  122. query.execute() # Remove the rows, return number of rows removed.
  123. return True
  124. except:
  125. return False
  126. Chats = ChatTable(DB)