1
0

notes.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_user_id(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(request: Request, user=Depends(get_verified_user)):
  46. if user.role != "admin" and not has_permission(
  47. user.id, "features.notes", request.app.state.config.USER_PERMISSIONS
  48. ):
  49. raise HTTPException(
  50. status_code=status.HTTP_401_UNAUTHORIZED,
  51. detail=ERROR_MESSAGES.UNAUTHORIZED,
  52. )
  53. notes = [
  54. NoteTitleIdResponse(**note.model_dump())
  55. for note in Notes.get_notes_by_user_id(user.id, "write")
  56. ]
  57. return notes
  58. ############################
  59. # CreateNewNote
  60. ############################
  61. @router.post("/create", response_model=Optional[NoteModel])
  62. async def create_new_note(
  63. request: Request, form_data: NoteForm, user=Depends(get_verified_user)
  64. ):
  65. if user.role != "admin" and not has_permission(
  66. user.id, "features.notes", request.app.state.config.USER_PERMISSIONS
  67. ):
  68. raise HTTPException(
  69. status_code=status.HTTP_401_UNAUTHORIZED,
  70. detail=ERROR_MESSAGES.UNAUTHORIZED,
  71. )
  72. try:
  73. note = Notes.insert_new_note(form_data, user.id)
  74. return note
  75. except Exception as e:
  76. log.exception(e)
  77. raise HTTPException(
  78. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  79. )
  80. ############################
  81. # GetNoteById
  82. ############################
  83. @router.get("/{id}", response_model=Optional[NoteModel])
  84. async def get_note_by_id(request: Request, id: str, user=Depends(get_verified_user)):
  85. if user.role != "admin" and not has_permission(
  86. user.id, "features.notes", request.app.state.config.USER_PERMISSIONS
  87. ):
  88. raise HTTPException(
  89. status_code=status.HTTP_401_UNAUTHORIZED,
  90. detail=ERROR_MESSAGES.UNAUTHORIZED,
  91. )
  92. note = Notes.get_note_by_id(id)
  93. if not note:
  94. raise HTTPException(
  95. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  96. )
  97. if user.role != "admin" and (
  98. 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 user.role != "admin" and (
  125. user.id != note.user_id
  126. and not has_access(user.id, type="write", access_control=note.access_control)
  127. ):
  128. raise HTTPException(
  129. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  130. )
  131. try:
  132. note = Notes.update_note_by_id(id, form_data)
  133. await sio.emit(
  134. "note-events",
  135. note.model_dump(),
  136. to=f"note:{note.id}",
  137. )
  138. return note
  139. except Exception as e:
  140. log.exception(e)
  141. raise HTTPException(
  142. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  143. )
  144. ############################
  145. # DeleteNoteById
  146. ############################
  147. @router.delete("/{id}/delete", response_model=bool)
  148. async def delete_note_by_id(request: Request, id: str, user=Depends(get_verified_user)):
  149. if user.role != "admin" and not has_permission(
  150. user.id, "features.notes", request.app.state.config.USER_PERMISSIONS
  151. ):
  152. raise HTTPException(
  153. status_code=status.HTTP_401_UNAUTHORIZED,
  154. detail=ERROR_MESSAGES.UNAUTHORIZED,
  155. )
  156. note = Notes.get_note_by_id(id)
  157. if not note:
  158. raise HTTPException(
  159. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  160. )
  161. if user.role != "admin" and (
  162. user.id != note.user_id
  163. and not has_access(user.id, type="write", access_control=note.access_control)
  164. ):
  165. raise HTTPException(
  166. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  167. )
  168. try:
  169. note = Notes.delete_note_by_id(id)
  170. return True
  171. except Exception as e:
  172. log.exception(e)
  173. raise HTTPException(
  174. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  175. )