notes.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import json
  2. import time
  3. import uuid
  4. from typing import Optional
  5. from open_webui.internal.db import Base, get_db
  6. from open_webui.models.groups import Groups
  7. from open_webui.utils.access_control import has_access
  8. from open_webui.models.users import Users, UserResponse
  9. from pydantic import BaseModel, ConfigDict
  10. from sqlalchemy import BigInteger, Boolean, Column, String, Text, JSON
  11. from sqlalchemy import or_, func, select, and_, text
  12. from sqlalchemy.sql import exists
  13. ####################
  14. # Note DB Schema
  15. ####################
  16. class Note(Base):
  17. __tablename__ = "note"
  18. id = Column(Text, primary_key=True)
  19. user_id = Column(Text)
  20. title = Column(Text)
  21. data = Column(JSON, nullable=True)
  22. meta = Column(JSON, nullable=True)
  23. access_control = Column(JSON, nullable=True)
  24. created_at = Column(BigInteger)
  25. updated_at = Column(BigInteger)
  26. class NoteModel(BaseModel):
  27. model_config = ConfigDict(from_attributes=True)
  28. id: str
  29. user_id: str
  30. title: str
  31. data: Optional[dict] = None
  32. meta: Optional[dict] = None
  33. access_control: Optional[dict] = None
  34. created_at: int # timestamp in epoch
  35. updated_at: int # timestamp in epoch
  36. ####################
  37. # Forms
  38. ####################
  39. class NoteForm(BaseModel):
  40. title: str
  41. data: Optional[dict] = None
  42. meta: Optional[dict] = None
  43. access_control: Optional[dict] = None
  44. class NoteUpdateForm(BaseModel):
  45. title: Optional[str] = None
  46. data: Optional[dict] = None
  47. meta: Optional[dict] = None
  48. access_control: Optional[dict] = None
  49. class NoteUserResponse(NoteModel):
  50. user: Optional[UserResponse] = None
  51. class NoteTable:
  52. def insert_new_note(
  53. self,
  54. form_data: NoteForm,
  55. user_id: str,
  56. ) -> Optional[NoteModel]:
  57. with get_db() as db:
  58. note = NoteModel(
  59. **{
  60. "id": str(uuid.uuid4()),
  61. "user_id": user_id,
  62. **form_data.model_dump(),
  63. "created_at": int(time.time_ns()),
  64. "updated_at": int(time.time_ns()),
  65. }
  66. )
  67. new_note = Note(**note.model_dump())
  68. db.add(new_note)
  69. db.commit()
  70. return note
  71. def get_notes(self) -> list[NoteModel]:
  72. with get_db() as db:
  73. notes = db.query(Note).order_by(Note.updated_at.desc()).all()
  74. return [NoteModel.model_validate(note) for note in notes]
  75. def get_notes_by_user_id(
  76. self, user_id: str, permission: str = "write"
  77. ) -> list[NoteModel]:
  78. notes = self.get_notes()
  79. user_group_ids = {group.id for group in Groups.get_groups_by_member_id(user_id)}
  80. return [
  81. note
  82. for note in notes
  83. if note.user_id == user_id
  84. or has_access(user_id, permission, note.access_control, user_group_ids)
  85. ]
  86. def get_note_by_id(self, id: str) -> Optional[NoteModel]:
  87. with get_db() as db:
  88. note = db.query(Note).filter(Note.id == id).first()
  89. return NoteModel.model_validate(note) if note else None
  90. def update_note_by_id(
  91. self, id: str, form_data: NoteUpdateForm
  92. ) -> Optional[NoteModel]:
  93. with get_db() as db:
  94. note = db.query(Note).filter(Note.id == id).first()
  95. if not note:
  96. return None
  97. form_data = form_data.model_dump(exclude_unset=True)
  98. if "title" in form_data:
  99. note.title = form_data["title"]
  100. if "data" in form_data:
  101. note.data = {**note.data, **form_data["data"]}
  102. if "meta" in form_data:
  103. note.meta = {**note.meta, **form_data["meta"]}
  104. if "access_control" in form_data:
  105. note.access_control = form_data["access_control"]
  106. note.updated_at = int(time.time_ns())
  107. db.commit()
  108. return NoteModel.model_validate(note) if note else None
  109. def delete_note_by_id(self, id: str):
  110. with get_db() as db:
  111. db.query(Note).filter(Note.id == id).delete()
  112. db.commit()
  113. return True
  114. Notes = NoteTable()