notes.py 6.3 KB

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