1
0

notes.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import json
  2. import logging
  3. from typing import Optional
  4. from fastapi import APIRouter, Depends, HTTPException, Request, status, BackgroundTasks
  5. from pydantic import BaseModel
  6. from open_webui.socket.main import sio
  7. from open_webui.models.users import Users, UserResponse
  8. from open_webui.models.notes import Notes, NoteModel, NoteForm, NoteUserResponse
  9. from open_webui.config import ENABLE_ADMIN_CHAT_ACCESS, ENABLE_ADMIN_EXPORT
  10. from open_webui.constants import ERROR_MESSAGES
  11. from open_webui.env import SRC_LOG_LEVELS
  12. from open_webui.utils.auth import get_admin_user, get_verified_user
  13. from open_webui.utils.access_control import has_access, has_permission
  14. log = logging.getLogger(__name__)
  15. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  16. router = APIRouter()
  17. ############################
  18. # GetNotes
  19. ############################
  20. @router.get("/", response_model=list[NoteUserResponse])
  21. async def get_notes(request: Request, user=Depends(get_verified_user)):
  22. if user.role != "admin" and not has_permission(
  23. user.id, "features.notes", request.app.state.config.USER_PERMISSIONS
  24. ):
  25. raise HTTPException(
  26. status_code=status.HTTP_401_UNAUTHORIZED,
  27. detail=ERROR_MESSAGES.UNAUTHORIZED,
  28. )
  29. notes = [
  30. NoteUserResponse(
  31. **{
  32. **note.model_dump(),
  33. "user": UserResponse(**Users.get_user_by_id(note.user_id).model_dump()),
  34. }
  35. )
  36. for note in Notes.get_notes_by_permission(user.id, "write")
  37. ]
  38. return notes
  39. class NoteTitleIdResponse(BaseModel):
  40. id: str
  41. title: str
  42. updated_at: int
  43. created_at: int
  44. @router.get("/list", response_model=list[NoteTitleIdResponse])
  45. async def get_note_list(
  46. request: Request, page: Optional[int] = None, user=Depends(get_verified_user)
  47. ):
  48. if user.role != "admin" and not has_permission(
  49. user.id, "features.notes", request.app.state.config.USER_PERMISSIONS
  50. ):
  51. raise HTTPException(
  52. status_code=status.HTTP_401_UNAUTHORIZED,
  53. detail=ERROR_MESSAGES.UNAUTHORIZED,
  54. )
  55. limit = None
  56. skip = None
  57. if page is not None:
  58. limit = 60
  59. skip = (page - 1) * limit
  60. notes = [
  61. NoteTitleIdResponse(**note.model_dump())
  62. for note in Notes.get_notes_by_permission(
  63. user.id, "write", skip=skip, limit=limit
  64. )
  65. ]
  66. return notes
  67. ############################
  68. # CreateNewNote
  69. ############################
  70. @router.post("/create", response_model=Optional[NoteModel])
  71. async def create_new_note(
  72. request: Request, form_data: NoteForm, user=Depends(get_verified_user)
  73. ):
  74. if user.role != "admin" and not has_permission(
  75. user.id, "features.notes", request.app.state.config.USER_PERMISSIONS
  76. ):
  77. raise HTTPException(
  78. status_code=status.HTTP_401_UNAUTHORIZED,
  79. detail=ERROR_MESSAGES.UNAUTHORIZED,
  80. )
  81. try:
  82. note = Notes.insert_new_note(form_data, user.id)
  83. return note
  84. except Exception as e:
  85. log.exception(e)
  86. raise HTTPException(
  87. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  88. )
  89. ############################
  90. # GetNoteById
  91. ############################
  92. @router.get("/{id}", response_model=Optional[NoteModel])
  93. async def get_note_by_id(request: Request, id: str, user=Depends(get_verified_user)):
  94. if user.role != "admin" and not has_permission(
  95. user.id, "features.notes", request.app.state.config.USER_PERMISSIONS
  96. ):
  97. raise HTTPException(
  98. status_code=status.HTTP_401_UNAUTHORIZED,
  99. detail=ERROR_MESSAGES.UNAUTHORIZED,
  100. )
  101. note = Notes.get_note_by_id(id)
  102. if not note:
  103. raise HTTPException(
  104. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  105. )
  106. if user.role != "admin" and (
  107. user.id != note.user_id
  108. and (not has_access(user.id, type="read", access_control=note.access_control))
  109. ):
  110. raise HTTPException(
  111. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  112. )
  113. return note
  114. ############################
  115. # UpdateNoteById
  116. ############################
  117. @router.post("/{id}/update", response_model=Optional[NoteModel])
  118. async def update_note_by_id(
  119. request: Request, id: str, form_data: NoteForm, user=Depends(get_verified_user)
  120. ):
  121. if user.role != "admin" and not has_permission(
  122. user.id, "features.notes", request.app.state.config.USER_PERMISSIONS
  123. ):
  124. raise HTTPException(
  125. status_code=status.HTTP_401_UNAUTHORIZED,
  126. detail=ERROR_MESSAGES.UNAUTHORIZED,
  127. )
  128. note = Notes.get_note_by_id(id)
  129. if not note:
  130. raise HTTPException(
  131. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  132. )
  133. if user.role != "admin" and (
  134. user.id != note.user_id
  135. and not has_access(user.id, type="write", access_control=note.access_control)
  136. ):
  137. raise HTTPException(
  138. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  139. )
  140. # Check if user can share publicly
  141. if (
  142. user.role != "admin"
  143. and form_data.access_control == None
  144. and not has_permission(
  145. user.id,
  146. "sharing.public_notes",
  147. request.app.state.config.USER_PERMISSIONS,
  148. )
  149. ):
  150. form_data.access_control = {}
  151. try:
  152. note = Notes.update_note_by_id(id, form_data)
  153. await sio.emit(
  154. "note-events",
  155. note.model_dump(),
  156. to=f"note:{note.id}",
  157. )
  158. return note
  159. except Exception as e:
  160. log.exception(e)
  161. raise HTTPException(
  162. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  163. )
  164. ############################
  165. # DeleteNoteById
  166. ############################
  167. @router.delete("/{id}/delete", response_model=bool)
  168. async def delete_note_by_id(request: Request, id: str, user=Depends(get_verified_user)):
  169. if user.role != "admin" and not has_permission(
  170. user.id, "features.notes", request.app.state.config.USER_PERMISSIONS
  171. ):
  172. raise HTTPException(
  173. status_code=status.HTTP_401_UNAUTHORIZED,
  174. detail=ERROR_MESSAGES.UNAUTHORIZED,
  175. )
  176. note = Notes.get_note_by_id(id)
  177. if not note:
  178. raise HTTPException(
  179. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  180. )
  181. if user.role != "admin" and (
  182. user.id != note.user_id
  183. and not has_access(user.id, type="write", access_control=note.access_control)
  184. ):
  185. raise HTTPException(
  186. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  187. )
  188. try:
  189. note = Notes.delete_note_by_id(id)
  190. return True
  191. except Exception as e:
  192. log.exception(e)
  193. raise HTTPException(
  194. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  195. )