notes.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 (
  97. user.id != note.user_id
  98. and (not has_access(user.id, type="read", access_control=note.access_control))
  99. ):
  100. raise HTTPException(
  101. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  102. )
  103. return note
  104. ############################
  105. # UpdateNoteById
  106. ############################
  107. @router.post("/{id}/update", response_model=Optional[NoteModel])
  108. async def update_note_by_id(
  109. request: Request, id: str, form_data: NoteForm, user=Depends(get_verified_user)
  110. ):
  111. if user.role != "admin" and not has_permission(
  112. user.id, "features.notes", request.app.state.config.USER_PERMISSIONS
  113. ):
  114. raise HTTPException(
  115. status_code=status.HTTP_401_UNAUTHORIZED,
  116. detail=ERROR_MESSAGES.UNAUTHORIZED,
  117. )
  118. note = Notes.get_note_by_id(id)
  119. if not note:
  120. raise HTTPException(
  121. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  122. )
  123. if user.role != "admin" and (
  124. user.id != note.user_id
  125. and not has_access(user.id, type="write", access_control=note.access_control)
  126. ):
  127. raise HTTPException(
  128. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  129. )
  130. try:
  131. note = Notes.update_note_by_id(id, form_data)
  132. return note
  133. except Exception as e:
  134. log.exception(e)
  135. raise HTTPException(
  136. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  137. )
  138. ############################
  139. # DeleteNoteById
  140. ############################
  141. @router.delete("/{id}/delete", response_model=bool)
  142. async def delete_note_by_id(request: Request, id: str, user=Depends(get_verified_user)):
  143. if user.role != "admin" and not has_permission(
  144. user.id, "features.notes", request.app.state.config.USER_PERMISSIONS
  145. ):
  146. raise HTTPException(
  147. status_code=status.HTTP_401_UNAUTHORIZED,
  148. detail=ERROR_MESSAGES.UNAUTHORIZED,
  149. )
  150. note = Notes.get_note_by_id(id)
  151. if not note:
  152. raise HTTPException(
  153. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  154. )
  155. if user.role != "admin" and (
  156. user.id != note.user_id
  157. and not has_access(user.id, type="write", access_control=note.access_control)
  158. ):
  159. raise HTTPException(
  160. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  161. )
  162. try:
  163. note = Notes.delete_note_by_id(id)
  164. return True
  165. except Exception as e:
  166. log.exception(e)
  167. raise HTTPException(
  168. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  169. )