notes.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. try:
  141. note = Notes.update_note_by_id(id, form_data)
  142. await sio.emit(
  143. "note-events",
  144. note.model_dump(),
  145. to=f"note:{note.id}",
  146. )
  147. return note
  148. except Exception as e:
  149. log.exception(e)
  150. raise HTTPException(
  151. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  152. )
  153. ############################
  154. # DeleteNoteById
  155. ############################
  156. @router.delete("/{id}/delete", response_model=bool)
  157. async def delete_note_by_id(request: Request, id: str, user=Depends(get_verified_user)):
  158. if user.role != "admin" and not has_permission(
  159. user.id, "features.notes", request.app.state.config.USER_PERMISSIONS
  160. ):
  161. raise HTTPException(
  162. status_code=status.HTTP_401_UNAUTHORIZED,
  163. detail=ERROR_MESSAGES.UNAUTHORIZED,
  164. )
  165. note = Notes.get_note_by_id(id)
  166. if not note:
  167. raise HTTPException(
  168. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  169. )
  170. if user.role != "admin" and (
  171. user.id != note.user_id
  172. and not has_access(user.id, type="write", access_control=note.access_control)
  173. ):
  174. raise HTTPException(
  175. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  176. )
  177. try:
  178. note = Notes.delete_note_by_id(id)
  179. return True
  180. except Exception as e:
  181. log.exception(e)
  182. raise HTTPException(
  183. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  184. )