channels.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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, 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. 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. id: str,
  159. form_data: MessageForm,
  160. background_tasks: BackgroundTasks,
  161. user=Depends(get_verified_user),
  162. ):
  163. channel = Channels.get_channel_by_id(id)
  164. if not channel:
  165. raise HTTPException(
  166. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  167. )
  168. if user.role != "admin" and not has_access(
  169. user.id, type="read", access_control=channel.access_control
  170. ):
  171. raise HTTPException(
  172. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  173. )
  174. try:
  175. message = Messages.insert_new_message(form_data, channel.id, user.id)
  176. if message:
  177. event_data = {
  178. "channel_id": channel.id,
  179. "message_id": message.id,
  180. "data": {
  181. "type": "message",
  182. "data": {
  183. **message.model_dump(),
  184. "user": UserNameResponse(**user.model_dump()).model_dump(),
  185. },
  186. },
  187. "user": UserNameResponse(**user.model_dump()).model_dump(),
  188. "channel": channel.model_dump(),
  189. }
  190. await sio.emit(
  191. "channel-events",
  192. event_data,
  193. to=f"channel:{channel.id}",
  194. )
  195. active_user_ids = get_user_ids_from_room(f"channel:{channel.id}")
  196. background_tasks.add_task(
  197. send_notification, channel, message, active_user_ids
  198. )
  199. return MessageModel(**message.model_dump())
  200. except Exception as e:
  201. log.exception(e)
  202. raise HTTPException(
  203. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  204. )
  205. ############################
  206. # UpdateMessageById
  207. ############################
  208. @router.post(
  209. "/{id}/messages/{message_id}/update", response_model=Optional[MessageModel]
  210. )
  211. async def update_message_by_id(
  212. id: str, message_id: str, form_data: MessageForm, user=Depends(get_verified_user)
  213. ):
  214. channel = Channels.get_channel_by_id(id)
  215. if not channel:
  216. raise HTTPException(
  217. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  218. )
  219. if user.role != "admin" and not has_access(
  220. user.id, type="read", access_control=channel.access_control
  221. ):
  222. raise HTTPException(
  223. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  224. )
  225. message = Messages.get_message_by_id(message_id)
  226. if not message:
  227. raise HTTPException(
  228. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  229. )
  230. if message.channel_id != id:
  231. raise HTTPException(
  232. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  233. )
  234. try:
  235. message = Messages.update_message_by_id(message_id, form_data)
  236. if message:
  237. await sio.emit(
  238. "channel-events",
  239. {
  240. "channel_id": channel.id,
  241. "message_id": message.id,
  242. "data": {
  243. "type": "message:update",
  244. "data": {
  245. **message.model_dump(),
  246. "user": UserNameResponse(**user.model_dump()).model_dump(),
  247. },
  248. },
  249. "user": UserNameResponse(**user.model_dump()).model_dump(),
  250. "channel": channel.model_dump(),
  251. },
  252. to=f"channel:{channel.id}",
  253. )
  254. return MessageModel(**message.model_dump())
  255. except Exception as e:
  256. log.exception(e)
  257. raise HTTPException(
  258. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  259. )
  260. ############################
  261. # DeleteMessageById
  262. ############################
  263. @router.delete("/{id}/messages/{message_id}/delete", response_model=bool)
  264. async def delete_message_by_id(
  265. id: str, message_id: str, user=Depends(get_verified_user)
  266. ):
  267. channel = Channels.get_channel_by_id(id)
  268. if not channel:
  269. raise HTTPException(
  270. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  271. )
  272. if user.role != "admin" and not has_access(
  273. user.id, type="read", access_control=channel.access_control
  274. ):
  275. raise HTTPException(
  276. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  277. )
  278. message = Messages.get_message_by_id(message_id)
  279. if not message:
  280. raise HTTPException(
  281. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  282. )
  283. if message.channel_id != id:
  284. raise HTTPException(
  285. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  286. )
  287. try:
  288. Messages.delete_message_by_id(message_id)
  289. await sio.emit(
  290. "channel-events",
  291. {
  292. "channel_id": channel.id,
  293. "message_id": message.id,
  294. "data": {
  295. "type": "message:delete",
  296. "data": {
  297. **message.model_dump(),
  298. "user": UserNameResponse(**user.model_dump()).model_dump(),
  299. },
  300. },
  301. "user": UserNameResponse(**user.model_dump()).model_dump(),
  302. "channel": channel.model_dump(),
  303. },
  304. to=f"channel:{channel.id}",
  305. )
  306. return True
  307. except Exception as e:
  308. log.exception(e)
  309. raise HTTPException(
  310. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  311. )