chats.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 ChatResponse(BaseModel):
  32. id: str
  33. user_id: str
  34. title: str
  35. chat: dict
  36. timestamp: int # timestamp in epoch
  37. class ChatTitleIdResponse(BaseModel):
  38. id: str
  39. title: str
  40. class ChatTable:
  41. def __init__(self, db):
  42. self.db = db
  43. db.create_tables([Chat])
  44. def insert_new_chat(self, user_id: str, form_data: ChatForm) -> Optional[ChatModel]:
  45. id = str(uuid.uuid4())
  46. chat = ChatModel(
  47. **{
  48. "id": id,
  49. "user_id": user_id,
  50. "title": form_data.chat["title"]
  51. if "title" in form_data.chat
  52. else "New Chat",
  53. "chat": json.dumps(form_data.chat),
  54. "timestamp": int(time.time()),
  55. }
  56. )
  57. result = Chat.create(**chat.model_dump())
  58. return chat if result else None
  59. def update_chat_by_id(self, id: str, chat: dict) -> Optional[ChatModel]:
  60. try:
  61. query = Chat.update(
  62. chat=json.dumps(chat),
  63. title=chat["title"] if "title" in chat else "New Chat",
  64. timestamp=int(time.time()),
  65. ).where(Chat.id == id)
  66. query.execute()
  67. chat = Chat.get(Chat.id == id)
  68. return ChatModel(**model_to_dict(chat))
  69. except:
  70. return None
  71. def get_chat_lists_by_user_id(
  72. self, user_id: str, skip: int = 0, limit: int = 50
  73. ) -> List[ChatModel]:
  74. return [
  75. ChatModel(**model_to_dict(chat))
  76. for chat in Chat.select()
  77. .where(Chat.user_id == user_id)
  78. .order_by(Chat.timestamp.desc())
  79. .limit(limit)
  80. .offset(skip)
  81. ]
  82. def get_chat_by_id_and_user_id(self, id: str, user_id: str) -> Optional[ChatModel]:
  83. try:
  84. chat = Chat.get(Chat.id == id, Chat.user_id == user_id)
  85. return ChatModel(**model_to_dict(chat))
  86. except:
  87. return None
  88. def get_chats(self, skip: int = 0, limit: int = 50) -> List[ChatModel]:
  89. return [
  90. ChatModel(**model_to_dict(chat))
  91. for chat in Chat.select().limit(limit).offset(skip)
  92. ]
  93. def delete_chat_by_id_and_user_id(self, id: str, user_id: str) -> bool:
  94. try:
  95. query = Chat.delete().where((Chat.id == id) & (Chat.user_id == user_id))
  96. query.execute() # Remove the rows, return number of rows removed.
  97. return True
  98. except:
  99. return False
  100. Chats = ChatTable(DB)