notes.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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(
  72. self, skip: Optional[int] = None, limit: Optional[int] = None
  73. ) -> list[NoteModel]:
  74. with get_db() as db:
  75. query = db.query(Note).order_by(Note.updated_at.desc())
  76. if skip is not None:
  77. query = query.offset(skip)
  78. if limit is not None:
  79. query = query.limit(limit)
  80. notes = query.all()
  81. return [NoteModel.model_validate(note) for note in notes]
  82. def get_notes_by_user_id(
  83. self,
  84. user_id: str,
  85. permission: str = "write",
  86. skip: Optional[int] = None,
  87. limit: Optional[int] = None,
  88. ) -> list[NoteModel]:
  89. notes = self.get_notes(skip=skip, limit=limit)
  90. user_group_ids = {group.id for group in Groups.get_groups_by_member_id(user_id)}
  91. return [
  92. note
  93. for note in notes
  94. if note.user_id == user_id
  95. or has_access(user_id, permission, note.access_control, user_group_ids)
  96. ]
  97. def get_note_by_id(self, id: str) -> Optional[NoteModel]:
  98. with get_db() as db:
  99. note = db.query(Note).filter(Note.id == id).first()
  100. return NoteModel.model_validate(note) if note else None
  101. def update_note_by_id(
  102. self, id: str, form_data: NoteUpdateForm
  103. ) -> Optional[NoteModel]:
  104. with get_db() as db:
  105. note = db.query(Note).filter(Note.id == id).first()
  106. if not note:
  107. return None
  108. form_data = form_data.model_dump(exclude_unset=True)
  109. if "title" in form_data:
  110. note.title = form_data["title"]
  111. if "data" in form_data:
  112. note.data = {**note.data, **form_data["data"]}
  113. if "meta" in form_data:
  114. note.meta = {**note.meta, **form_data["meta"]}
  115. if "access_control" in form_data:
  116. note.access_control = form_data["access_control"]
  117. note.updated_at = int(time.time_ns())
  118. db.commit()
  119. return NoteModel.model_validate(note) if note else None
  120. def delete_note_by_id(self, id: str):
  121. with get_db() as db:
  122. db.query(Note).filter(Note.id == id).delete()
  123. db.commit()
  124. return True
  125. Notes = NoteTable()