notes.py 4.0 KB

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