channels.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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, get_user_ids_from_room
  7. from open_webui.models.users import Users, UserNameResponse
  8. from open_webui.models.channels import Channels, ChannelModel, ChannelForm
  9. from open_webui.models.messages import Messages, MessageModel, MessageForm
  10. from open_webui.config import ENABLE_ADMIN_CHAT_ACCESS, ENABLE_ADMIN_EXPORT
  11. from open_webui.constants import ERROR_MESSAGES
  12. from open_webui.env import SRC_LOG_LEVELS
  13. from open_webui.utils.auth import get_admin_user, get_verified_user
  14. from open_webui.utils.access_control import has_access, get_users_with_access
  15. from open_webui.utils.webhook import post_webhook
  16. log = logging.getLogger(__name__)
  17. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  18. router = APIRouter()
  19. ############################
  20. # GetChatList
  21. ############################
  22. @router.get("/", response_model=list[ChannelModel])
  23. async def get_channels(user=Depends(get_verified_user)):
  24. if user.role == "admin":
  25. return Channels.get_channels()
  26. else:
  27. return Channels.get_channels_by_user_id(user.id)
  28. ############################
  29. # CreateNewChannel
  30. ############################
  31. @router.post("/create", response_model=Optional[ChannelModel])
  32. async def create_new_channel(form_data: ChannelForm, user=Depends(get_admin_user)):
  33. try:
  34. channel = Channels.insert_new_channel(form_data, user.id)
  35. return ChannelModel(**channel.model_dump())
  36. except Exception as e:
  37. log.exception(e)
  38. raise HTTPException(
  39. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  40. )
  41. ############################
  42. # GetChannelById
  43. ############################
  44. @router.get("/{id}", response_model=Optional[ChannelModel])
  45. async def get_channel_by_id(id: str, user=Depends(get_verified_user)):
  46. channel = Channels.get_channel_by_id(id)
  47. if not channel:
  48. raise HTTPException(
  49. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  50. )
  51. if user.role != "admin" and not has_access(
  52. user.id, type="read", access_control=channel.access_control
  53. ):
  54. raise HTTPException(
  55. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  56. )
  57. return ChannelModel(**channel.model_dump())
  58. ############################
  59. # UpdateChannelById
  60. ############################
  61. @router.post("/{id}/update", response_model=Optional[ChannelModel])
  62. async def update_channel_by_id(
  63. id: str, form_data: ChannelForm, user=Depends(get_admin_user)
  64. ):
  65. channel = Channels.get_channel_by_id(id)
  66. if not channel:
  67. raise HTTPException(
  68. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  69. )
  70. try:
  71. channel = Channels.update_channel_by_id(id, form_data)
  72. return ChannelModel(**channel.model_dump())
  73. except Exception as e:
  74. log.exception(e)
  75. raise HTTPException(
  76. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  77. )
  78. ############################
  79. # DeleteChannelById
  80. ############################
  81. @router.delete("/{id}/delete", response_model=bool)
  82. async def delete_channel_by_id(id: str, user=Depends(get_admin_user)):
  83. channel = Channels.get_channel_by_id(id)
  84. if not channel:
  85. raise HTTPException(
  86. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  87. )
  88. try:
  89. Channels.delete_channel_by_id(id)
  90. return True
  91. except Exception as e:
  92. log.exception(e)
  93. raise HTTPException(
  94. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  95. )
  96. ############################
  97. # GetChannelMessages
  98. ############################
  99. class MessageUserModel(MessageModel):
  100. user: UserNameResponse
  101. @router.get("/{id}/messages", response_model=list[MessageUserModel])
  102. async def get_channel_messages(
  103. id: str, skip: int = 0, limit: int = 50, user=Depends(get_verified_user)
  104. ):
  105. channel = Channels.get_channel_by_id(id)
  106. if not channel:
  107. raise HTTPException(
  108. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  109. )
  110. if user.role != "admin" and not has_access(
  111. user.id, type="read", access_control=channel.access_control
  112. ):
  113. raise HTTPException(
  114. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  115. )
  116. message_list = Messages.get_messages_by_channel_id(id, skip, limit)
  117. users = {}
  118. messages = []
  119. for message in message_list:
  120. if message.user_id not in users:
  121. user = Users.get_user_by_id(message.user_id)
  122. users[message.user_id] = user
  123. messages.append(
  124. MessageUserModel(
  125. **{
  126. **message.model_dump(),
  127. "user": UserNameResponse(**users[message.user_id].model_dump()),
  128. }
  129. )
  130. )
  131. return messages
  132. ############################
  133. # PostNewMessage
  134. ############################
  135. async def send_notification(webui_url, channel, message, active_user_ids):
  136. users = get_users_with_access("read", channel.access_control)
  137. for user in users:
  138. if user.id in active_user_ids:
  139. continue
  140. else:
  141. if user.settings:
  142. webhook_url = user.settings.ui.get("notifications", {}).get(
  143. "webhook_url", None
  144. )
  145. if webhook_url:
  146. post_webhook(
  147. webhook_url,
  148. f"#{channel.name} - {webui_url}/channels/{channel.id}\n\n{message.content}",
  149. {
  150. "action": "channel",
  151. "message": message.content,
  152. "title": channel.name,
  153. "url": f"{webui_url}/channels/{channel.id}",
  154. },
  155. )
  156. @router.post("/{id}/messages/post", response_model=Optional[MessageModel])
  157. async def post_new_message(
  158. request: Request,
  159. id: str,
  160. form_data: MessageForm,
  161. background_tasks: BackgroundTasks,
  162. user=Depends(get_verified_user),
  163. ):
  164. channel = Channels.get_channel_by_id(id)
  165. if not channel:
  166. raise HTTPException(
  167. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  168. )
  169. if user.role != "admin" and not has_access(
  170. user.id, type="read", access_control=channel.access_control
  171. ):
  172. raise HTTPException(
  173. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  174. )
  175. try:
  176. message = Messages.insert_new_message(form_data, channel.id, user.id)
  177. if message:
  178. event_data = {
  179. "channel_id": channel.id,
  180. "message_id": message.id,
  181. "data": {
  182. "type": "message",
  183. "data": {
  184. **message.model_dump(),
  185. "user": UserNameResponse(**user.model_dump()).model_dump(),
  186. },
  187. },
  188. "user": UserNameResponse(**user.model_dump()).model_dump(),
  189. "channel": channel.model_dump(),
  190. }
  191. await sio.emit(
  192. "channel-events",
  193. event_data,
  194. to=f"channel:{channel.id}",
  195. )
  196. active_user_ids = get_user_ids_from_room(f"channel:{channel.id}")
  197. background_tasks.add_task(
  198. send_notification,
  199. request.app.state.config.WEBUI_URL,
  200. channel,
  201. message,
  202. active_user_ids,
  203. )
  204. return MessageModel(**message.model_dump())
  205. except Exception as e:
  206. log.exception(e)
  207. raise HTTPException(
  208. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  209. )
  210. ############################
  211. # UpdateMessageById
  212. ############################
  213. @router.post(
  214. "/{id}/messages/{message_id}/update", response_model=Optional[MessageModel]
  215. )
  216. async def update_message_by_id(
  217. id: str, message_id: str, form_data: MessageForm, user=Depends(get_verified_user)
  218. ):
  219. channel = Channels.get_channel_by_id(id)
  220. if not channel:
  221. raise HTTPException(
  222. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  223. )
  224. if user.role != "admin" and not has_access(
  225. user.id, type="read", access_control=channel.access_control
  226. ):
  227. raise HTTPException(
  228. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  229. )
  230. message = Messages.get_message_by_id(message_id)
  231. if not message:
  232. raise HTTPException(
  233. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  234. )
  235. if message.channel_id != id:
  236. raise HTTPException(
  237. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  238. )
  239. try:
  240. message = Messages.update_message_by_id(message_id, form_data)
  241. if message:
  242. await sio.emit(
  243. "channel-events",
  244. {
  245. "channel_id": channel.id,
  246. "message_id": message.id,
  247. "data": {
  248. "type": "message:update",
  249. "data": {
  250. **message.model_dump(),
  251. "user": UserNameResponse(**user.model_dump()).model_dump(),
  252. },
  253. },
  254. "user": UserNameResponse(**user.model_dump()).model_dump(),
  255. "channel": channel.model_dump(),
  256. },
  257. to=f"channel:{channel.id}",
  258. )
  259. return MessageModel(**message.model_dump())
  260. except Exception as e:
  261. log.exception(e)
  262. raise HTTPException(
  263. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  264. )
  265. ############################
  266. # DeleteMessageById
  267. ############################
  268. @router.delete("/{id}/messages/{message_id}/delete", response_model=bool)
  269. async def delete_message_by_id(
  270. id: str, message_id: str, user=Depends(get_verified_user)
  271. ):
  272. channel = Channels.get_channel_by_id(id)
  273. if not channel:
  274. raise HTTPException(
  275. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  276. )
  277. if user.role != "admin" and not has_access(
  278. user.id, type="read", access_control=channel.access_control
  279. ):
  280. raise HTTPException(
  281. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  282. )
  283. message = Messages.get_message_by_id(message_id)
  284. if not message:
  285. raise HTTPException(
  286. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  287. )
  288. if message.channel_id != id:
  289. raise HTTPException(
  290. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  291. )
  292. try:
  293. Messages.delete_message_by_id(message_id)
  294. await sio.emit(
  295. "channel-events",
  296. {
  297. "channel_id": channel.id,
  298. "message_id": message.id,
  299. "data": {
  300. "type": "message:delete",
  301. "data": {
  302. **message.model_dump(),
  303. "user": UserNameResponse(**user.model_dump()).model_dump(),
  304. },
  305. },
  306. "user": UserNameResponse(**user.model_dump()).model_dump(),
  307. "channel": channel.model_dump(),
  308. },
  309. to=f"channel:{channel.id}",
  310. )
  311. return True
  312. except Exception as e:
  313. log.exception(e)
  314. raise HTTPException(
  315. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  316. )