channels.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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, SESSION_POOL
  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, WEBUI_URL
  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(channel, message, active_user_ids):
  136. print(f"Sending notification to {channel=}, {message=}, {active_user_ids=}")
  137. users = get_users_with_access("read", channel.access_control)
  138. for user in users:
  139. if user.id in active_user_ids:
  140. continue
  141. else:
  142. if user.settings:
  143. webhook_url = user.settings.ui.get("notifications", {}).get(
  144. "webhook_url", None
  145. )
  146. if webhook_url:
  147. post_webhook(
  148. webhook_url,
  149. f"#{channel.name} - {WEBUI_URL}/c/{channel.id}\n\n{message.content}",
  150. {
  151. "action": "channel",
  152. "message": message.content,
  153. "title": channel.name,
  154. "url": f"{WEBUI_URL}/c/{channel.id}",
  155. },
  156. )
  157. @router.post("/{id}/messages/post", response_model=Optional[MessageModel])
  158. async def post_new_message(
  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_session_ids = sio.manager.get_participants(
  197. namespace="/",
  198. room=f"channel:{channel.id}",
  199. )
  200. active_user_ids = list(
  201. set(
  202. [
  203. SESSION_POOL.get(session_id[0])
  204. for session_id in active_session_ids
  205. ]
  206. )
  207. )
  208. background_tasks.add_task(
  209. send_notification, channel, message, active_user_ids
  210. )
  211. return MessageModel(**message.model_dump())
  212. except Exception as e:
  213. log.exception(e)
  214. raise HTTPException(
  215. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  216. )
  217. ############################
  218. # UpdateMessageById
  219. ############################
  220. @router.post(
  221. "/{id}/messages/{message_id}/update", response_model=Optional[MessageModel]
  222. )
  223. async def update_message_by_id(
  224. id: str, message_id: str, form_data: MessageForm, user=Depends(get_verified_user)
  225. ):
  226. channel = Channels.get_channel_by_id(id)
  227. if not channel:
  228. raise HTTPException(
  229. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  230. )
  231. if user.role != "admin" and not has_access(
  232. user.id, type="read", access_control=channel.access_control
  233. ):
  234. raise HTTPException(
  235. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  236. )
  237. message = Messages.get_message_by_id(message_id)
  238. if not message:
  239. raise HTTPException(
  240. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  241. )
  242. if message.channel_id != id:
  243. raise HTTPException(
  244. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  245. )
  246. try:
  247. message = Messages.update_message_by_id(message_id, form_data)
  248. if message:
  249. await sio.emit(
  250. "channel-events",
  251. {
  252. "channel_id": channel.id,
  253. "message_id": message.id,
  254. "data": {
  255. "type": "message:update",
  256. "data": {
  257. **message.model_dump(),
  258. "user": UserNameResponse(**user.model_dump()).model_dump(),
  259. },
  260. },
  261. "user": UserNameResponse(**user.model_dump()).model_dump(),
  262. "channel": channel.model_dump(),
  263. },
  264. to=f"channel:{channel.id}",
  265. )
  266. return MessageModel(**message.model_dump())
  267. except Exception as e:
  268. log.exception(e)
  269. raise HTTPException(
  270. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  271. )
  272. ############################
  273. # DeleteMessageById
  274. ############################
  275. @router.delete("/{id}/messages/{message_id}/delete", response_model=bool)
  276. async def delete_message_by_id(
  277. id: str, message_id: str, user=Depends(get_verified_user)
  278. ):
  279. channel = Channels.get_channel_by_id(id)
  280. if not channel:
  281. raise HTTPException(
  282. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  283. )
  284. if user.role != "admin" and not has_access(
  285. user.id, type="read", access_control=channel.access_control
  286. ):
  287. raise HTTPException(
  288. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  289. )
  290. message = Messages.get_message_by_id(message_id)
  291. if not message:
  292. raise HTTPException(
  293. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  294. )
  295. if message.channel_id != id:
  296. raise HTTPException(
  297. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  298. )
  299. try:
  300. Messages.delete_message_by_id(message_id)
  301. await sio.emit(
  302. "channel-events",
  303. {
  304. "channel_id": channel.id,
  305. "message_id": message.id,
  306. "data": {
  307. "type": "message:delete",
  308. "data": {
  309. **message.model_dump(),
  310. "user": UserNameResponse(**user.model_dump()).model_dump(),
  311. },
  312. },
  313. "user": UserNameResponse(**user.model_dump()).model_dump(),
  314. "channel": channel.model_dump(),
  315. },
  316. to=f"channel:{channel.id}",
  317. )
  318. return True
  319. except Exception as e:
  320. log.exception(e)
  321. raise HTTPException(
  322. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  323. )