1
0

notes.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import json
  2. import time
  3. import uuid
  4. from typing import Optional
  5. from functools import lru_cache
  6. from open_webui.internal.db import Base, get_db
  7. from open_webui.models.groups import Groups
  8. from open_webui.utils.access_control import has_access
  9. from open_webui.models.users import Users, UserResponse
  10. from pydantic import BaseModel, ConfigDict
  11. from sqlalchemy import BigInteger, Boolean, Column, String, Text, JSON
  12. from sqlalchemy import or_, func, select, and_, text
  13. from sqlalchemy.sql import exists
  14. ####################
  15. # Note DB Schema
  16. ####################
  17. class Note(Base):
  18. __tablename__ = "note"
  19. id = Column(Text, primary_key=True)
  20. user_id = Column(Text)
  21. title = Column(Text)
  22. data = Column(JSON, nullable=True)
  23. meta = Column(JSON, nullable=True)
  24. access_control = Column(JSON, nullable=True)
  25. created_at = Column(BigInteger)
  26. updated_at = Column(BigInteger)
  27. class NoteModel(BaseModel):
  28. model_config = ConfigDict(from_attributes=True)
  29. id: str
  30. user_id: str
  31. title: str
  32. data: Optional[dict] = None
  33. meta: Optional[dict] = None
  34. access_control: Optional[dict] = None
  35. created_at: int # timestamp in epoch
  36. updated_at: int # timestamp in epoch
  37. ####################
  38. # Forms
  39. ####################
  40. class NoteForm(BaseModel):
  41. title: str
  42. data: Optional[dict] = None
  43. meta: Optional[dict] = None
  44. access_control: Optional[dict] = None
  45. class NoteUpdateForm(BaseModel):
  46. title: Optional[str] = None
  47. data: Optional[dict] = None
  48. meta: Optional[dict] = None
  49. access_control: Optional[dict] = None
  50. class NoteUserResponse(NoteModel):
  51. user: Optional[UserResponse] = None
  52. class NoteTable:
  53. def insert_new_note(
  54. self,
  55. form_data: NoteForm,
  56. user_id: str,
  57. ) -> Optional[NoteModel]:
  58. with get_db() as db:
  59. note = NoteModel(
  60. **{
  61. "id": str(uuid.uuid4()),
  62. "user_id": user_id,
  63. **form_data.model_dump(),
  64. "created_at": int(time.time_ns()),
  65. "updated_at": int(time.time_ns()),
  66. }
  67. )
  68. new_note = Note(**note.model_dump())
  69. db.add(new_note)
  70. db.commit()
  71. return note
  72. def get_notes(
  73. self, skip: Optional[int] = None, limit: Optional[int] = None
  74. ) -> list[NoteModel]:
  75. with get_db() as db:
  76. query = db.query(Note).order_by(Note.updated_at.desc())
  77. if skip is not None:
  78. query = query.offset(skip)
  79. if limit is not None:
  80. query = query.limit(limit)
  81. notes = query.all()
  82. return [NoteModel.model_validate(note) for note in notes]
  83. def get_notes_by_user_id(
  84. self,
  85. user_id: str,
  86. skip: Optional[int] = None,
  87. limit: Optional[int] = None,
  88. ) -> list[NoteModel]:
  89. with get_db() as db:
  90. query = db.query(Note).filter(Note.user_id == user_id)
  91. query = query.order_by(Note.updated_at.desc())
  92. if skip is not None:
  93. query = query.offset(skip)
  94. if limit is not None:
  95. query = query.limit(limit)
  96. notes = query.all()
  97. return [NoteModel.model_validate(note) for note in notes]
  98. def get_notes_by_permission(
  99. self,
  100. user_id: str,
  101. permission: str = "write",
  102. skip: Optional[int] = None,
  103. limit: Optional[int] = None,
  104. ) -> list[NoteModel]:
  105. with get_db() as db:
  106. user_groups = Groups.get_groups_by_member_id(user_id)
  107. user_group_ids = {group.id for group in user_groups}
  108. # Order newest-first. We stream to keep memory usage low.
  109. query = (
  110. db.query(Note)
  111. .order_by(Note.updated_at.desc())
  112. .execution_options(stream_results=True)
  113. .yield_per(256)
  114. )
  115. results: list[NoteModel] = []
  116. n_skipped = 0
  117. for note in query:
  118. # Fast-pass #1: owner
  119. if note.user_id == user_id:
  120. permitted = True
  121. # Fast-pass #2: public/open
  122. elif note.access_control is None:
  123. # Technically this should mean public access for both read and write, but we'll only do read for now
  124. # We might want to change this behavior later
  125. permitted = permission == "read"
  126. else:
  127. permitted = has_access(
  128. user_id, permission, note.access_control, user_group_ids
  129. )
  130. if not permitted:
  131. continue
  132. # Apply skip AFTER permission filtering so it counts only accessible notes
  133. if skip and n_skipped < skip:
  134. n_skipped += 1
  135. continue
  136. results.append(NoteModel.model_validate(note))
  137. if limit is not None and len(results) >= limit:
  138. break
  139. return results
  140. def get_note_by_id(self, id: str) -> Optional[NoteModel]:
  141. with get_db() as db:
  142. note = db.query(Note).filter(Note.id == id).first()
  143. return NoteModel.model_validate(note) if note else None
  144. def update_note_by_id(
  145. self, id: str, form_data: NoteUpdateForm
  146. ) -> Optional[NoteModel]:
  147. with get_db() as db:
  148. note = db.query(Note).filter(Note.id == id).first()
  149. if not note:
  150. return None
  151. form_data = form_data.model_dump(exclude_unset=True)
  152. if "title" in form_data:
  153. note.title = form_data["title"]
  154. if "data" in form_data:
  155. note.data = {**note.data, **form_data["data"]}
  156. if "meta" in form_data:
  157. note.meta = {**note.meta, **form_data["meta"]}
  158. if "access_control" in form_data:
  159. note.access_control = form_data["access_control"]
  160. note.updated_at = int(time.time_ns())
  161. db.commit()
  162. return NoteModel.model_validate(note) if note else None
  163. def delete_note_by_id(self, id: str):
  164. with get_db() as db:
  165. db.query(Note).filter(Note.id == id).delete()
  166. db.commit()
  167. return True
  168. Notes = NoteTable()