notes.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.models.users import Users, UserResponse
  7. from open_webui.models.notes import Notes, NoteModel, NoteForm, NoteUserResponse
  8. from open_webui.config import ENABLE_ADMIN_CHAT_ACCESS, ENABLE_ADMIN_EXPORT
  9. from open_webui.constants import ERROR_MESSAGES
  10. from open_webui.env import SRC_LOG_LEVELS
  11. from open_webui.utils.auth import get_admin_user, get_verified_user
  12. from open_webui.utils.access_control import has_access, has_permission
  13. log = logging.getLogger(__name__)
  14. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  15. router = APIRouter()
  16. ############################
  17. # GetNotes
  18. ############################
  19. @router.get("/", response_model=list[NoteUserResponse])
  20. async def get_notes(request: Request, user=Depends(get_verified_user)):
  21. if user.role != "admin" and not has_permission(
  22. user.id, "features.notes", request.app.state.config.USER_PERMISSIONS
  23. ):
  24. raise HTTPException(
  25. status_code=status.HTTP_401_UNAUTHORIZED,
  26. detail=ERROR_MESSAGES.UNAUTHORIZED,
  27. )
  28. notes = [
  29. NoteUserResponse(
  30. **{
  31. **note.model_dump(),
  32. "user": UserResponse(**Users.get_user_by_id(note.user_id).model_dump()),
  33. }
  34. )
  35. for note in Notes.get_notes_by_user_id(user.id, "write")
  36. ]
  37. return notes
  38. @router.get("/list", response_model=list[NoteUserResponse])
  39. async def get_note_list(request: Request, user=Depends(get_verified_user)):
  40. if user.role != "admin" and not has_permission(
  41. user.id, "features.notes", request.app.state.config.USER_PERMISSIONS
  42. ):
  43. raise HTTPException(
  44. status_code=status.HTTP_401_UNAUTHORIZED,
  45. detail=ERROR_MESSAGES.UNAUTHORIZED,
  46. )
  47. notes = [
  48. NoteUserResponse(
  49. **{
  50. **note.model_dump(),
  51. "user": UserResponse(**Users.get_user_by_id(note.user_id).model_dump()),
  52. }
  53. )
  54. for note in Notes.get_notes_by_user_id(user.id, "read")
  55. ]
  56. return notes
  57. ############################
  58. # CreateNewNote
  59. ############################
  60. @router.post("/create", response_model=Optional[NoteModel])
  61. async def create_new_note(
  62. request: Request, form_data: NoteForm, user=Depends(get_verified_user)
  63. ):
  64. if user.role != "admin" and not has_permission(
  65. user.id, "features.notes", request.app.state.config.USER_PERMISSIONS
  66. ):
  67. raise HTTPException(
  68. status_code=status.HTTP_401_UNAUTHORIZED,
  69. detail=ERROR_MESSAGES.UNAUTHORIZED,
  70. )
  71. try:
  72. note = Notes.insert_new_note(form_data, user.id)
  73. return note
  74. except Exception as e:
  75. log.exception(e)
  76. raise HTTPException(
  77. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  78. )
  79. ############################
  80. # GetNoteById
  81. ############################
  82. @router.get("/{id}", response_model=Optional[NoteModel])
  83. async def get_note_by_id(request: Request, id: str, user=Depends(get_verified_user)):
  84. if user.role != "admin" and not has_permission(
  85. user.id, "features.notes", request.app.state.config.USER_PERMISSIONS
  86. ):
  87. raise HTTPException(
  88. status_code=status.HTTP_401_UNAUTHORIZED,
  89. detail=ERROR_MESSAGES.UNAUTHORIZED,
  90. )
  91. note = Notes.get_note_by_id(id)
  92. if not note:
  93. raise HTTPException(
  94. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  95. )
  96. if user.role != "admin" and not has_access(
  97. user.id, type="read", access_control=note.access_control
  98. ):
  99. raise HTTPException(
  100. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  101. )
  102. return note
  103. ############################
  104. # UpdateNoteById
  105. ############################
  106. @router.post("/{id}/update", response_model=Optional[NoteModel])
  107. async def update_note_by_id(
  108. request: Request, id: str, form_data: NoteForm, user=Depends(get_verified_user)
  109. ):
  110. if user.role != "admin" and not has_permission(
  111. user.id, "features.notes", request.app.state.config.USER_PERMISSIONS
  112. ):
  113. raise HTTPException(
  114. status_code=status.HTTP_401_UNAUTHORIZED,
  115. detail=ERROR_MESSAGES.UNAUTHORIZED,
  116. )
  117. note = Notes.get_note_by_id(id)
  118. if not note:
  119. raise HTTPException(
  120. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  121. )
  122. if user.role != "admin" and not has_access(
  123. user.id, type="write", access_control=note.access_control
  124. ):
  125. raise HTTPException(
  126. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  127. )
  128. try:
  129. note = Notes.update_note_by_id(id, form_data)
  130. return note
  131. except Exception as e:
  132. log.exception(e)
  133. raise HTTPException(
  134. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  135. )
  136. ############################
  137. # DeleteNoteById
  138. ############################
  139. @router.delete("/{id}/delete", response_model=bool)
  140. async def delete_note_by_id(request: Request, id: str, user=Depends(get_verified_user)):
  141. if user.role != "admin" and not has_permission(
  142. user.id, "features.notes", request.app.state.config.USER_PERMISSIONS
  143. ):
  144. raise HTTPException(
  145. status_code=status.HTTP_401_UNAUTHORIZED,
  146. detail=ERROR_MESSAGES.UNAUTHORIZED,
  147. )
  148. note = Notes.get_note_by_id(id)
  149. if not note:
  150. raise HTTPException(
  151. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  152. )
  153. if user.role != "admin" and not has_access(
  154. user.id, type="write", access_control=note.access_control
  155. ):
  156. raise HTTPException(
  157. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  158. )
  159. try:
  160. note = Notes.delete_note_by_id(id)
  161. return True
  162. except Exception as e:
  163. log.exception(e)
  164. raise HTTPException(
  165. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  166. )