chats.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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, form_data: ChatForm) -> Optional[ChatModel]:
  47. id = str(uuid.uuid4())
  48. chat = ChatModel(
  49. **{
  50. "id": id,
  51. "user_id": user_id,
  52. "title": form_data.chat["title"]
  53. if "title" in form_data.chat
  54. else "New Chat",
  55. "chat": json.dumps(form_data.chat),
  56. "timestamp": int(time.time()),
  57. }
  58. )
  59. result = Chat.create(**chat.model_dump())
  60. return chat if result else None
  61. def update_chat_by_id(self, id: str, chat: dict) -> Optional[ChatModel]:
  62. try:
  63. query = Chat.update(
  64. chat=json.dumps(chat),
  65. title=chat["title"] if "title" in chat else "New Chat",
  66. timestamp=int(time.time()),
  67. ).where(Chat.id == id)
  68. query.execute()
  69. chat = Chat.get(Chat.id == id)
  70. return ChatModel(**model_to_dict(chat))
  71. except:
  72. return None
  73. def update_chat_by_id(self, id: str, chat: dict) -> Optional[ChatModel]:
  74. try:
  75. query = Chat.update(
  76. chat=json.dumps(chat),
  77. title=chat["title"] if "title" in chat else "New Chat",
  78. timestamp=int(time.time()),
  79. ).where(Chat.id == id)
  80. query.execute()
  81. chat = Chat.get(Chat.id == id)
  82. return ChatModel(**model_to_dict(chat))
  83. except:
  84. return None
  85. def get_chat_lists_by_user_id(
  86. self, user_id: str, skip: int = 0, limit: int = 50
  87. ) -> List[ChatModel]:
  88. return [
  89. ChatModel(**model_to_dict(chat))
  90. for chat in Chat.select()
  91. .where(Chat.user_id == user_id)
  92. .order_by(Chat.timestamp.desc())
  93. # .limit(limit)
  94. # .offset(skip)
  95. ]
  96. def get_chat_by_id_and_user_id(self, id: str, user_id: str) -> Optional[ChatModel]:
  97. try:
  98. chat = Chat.get(Chat.id == id, Chat.user_id == user_id)
  99. return ChatModel(**model_to_dict(chat))
  100. except:
  101. return None
  102. def get_chats(self, skip: int = 0, limit: int = 50) -> List[ChatModel]:
  103. return [
  104. ChatModel(**model_to_dict(chat))
  105. for chat in Chat.select().limit(limit).offset(skip)
  106. ]
  107. def delete_chat_by_id_and_user_id(self, id: str, user_id: str) -> bool:
  108. try:
  109. query = Chat.delete().where((Chat.id == id) & (Chat.user_id == user_id))
  110. query.execute() # Remove the rows, return number of rows removed.
  111. return True
  112. except:
  113. return False
  114. Chats = ChatTable(DB)