channels.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import json
  2. import logging
  3. from typing import Optional
  4. from fastapi import APIRouter, Depends, HTTPException, Request, status
  5. from pydantic import BaseModel
  6. from open_webui.socket.main import sio
  7. from open_webui.models.channels import Channels, ChannelModel, ChannelForm
  8. from open_webui.models.messages import Messages, MessageModel, MessageForm
  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
  14. log = logging.getLogger(__name__)
  15. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  16. router = APIRouter()
  17. ############################
  18. # GetChatList
  19. ############################
  20. @router.get("/", response_model=list[ChannelModel])
  21. async def get_channels(user=Depends(get_verified_user)):
  22. return Channels.get_channels()
  23. ############################
  24. # CreateNewChannel
  25. ############################
  26. @router.post("/create", response_model=Optional[ChannelModel])
  27. async def create_new_channel(form_data: ChannelForm, user=Depends(get_admin_user)):
  28. try:
  29. channel = Channels.insert_new_channel(form_data, user.id)
  30. return ChannelModel(**channel.model_dump())
  31. except Exception as e:
  32. log.exception(e)
  33. raise HTTPException(
  34. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  35. )
  36. ############################
  37. # GetChannelMessages
  38. ############################
  39. @router.get("/{id}/messages", response_model=list[MessageModel])
  40. async def get_channel_messages(id: str, page: int = 1, user=Depends(get_verified_user)):
  41. channel = Channels.get_channel_by_id(id)
  42. if not channel:
  43. raise HTTPException(
  44. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  45. )
  46. if not has_access(user.id, type="read", access_control=channel.access_control):
  47. raise HTTPException(
  48. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  49. )
  50. limit = 50
  51. skip = (page - 1) * limit
  52. return Messages.get_messages_by_channel_id(id, skip, limit)
  53. ############################
  54. # PostNewMessage
  55. ############################
  56. @router.post("/{id}/messages/post", response_model=Optional[MessageModel])
  57. async def post_new_message(
  58. id: str, form_data: MessageForm, user=Depends(get_verified_user)
  59. ):
  60. channel = Channels.get_channel_by_id(id)
  61. if not channel:
  62. raise HTTPException(
  63. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  64. )
  65. if not has_access(user.id, type="read", access_control=channel.access_control):
  66. raise HTTPException(
  67. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  68. )
  69. try:
  70. message = Messages.insert_new_message(form_data, channel.id, user.id)
  71. if message:
  72. await sio.emit(
  73. "channel-events",
  74. {
  75. "channel_id": channel.id,
  76. "message_id": message.id,
  77. "data": {"message": message.model_dump()},
  78. },
  79. to=f"channel:{channel.id}",
  80. )
  81. return MessageModel(**message.model_dump())
  82. except Exception as e:
  83. log.exception(e)
  84. raise HTTPException(
  85. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  86. )