channels.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. if user.role == "admin":
  23. return Channels.get_channels()
  24. else:
  25. return Channels.get_channels_by_user_id(user.id)
  26. ############################
  27. # CreateNewChannel
  28. ############################
  29. @router.post("/create", response_model=Optional[ChannelModel])
  30. async def create_new_channel(form_data: ChannelForm, user=Depends(get_admin_user)):
  31. try:
  32. channel = Channels.insert_new_channel(form_data, user.id)
  33. return ChannelModel(**channel.model_dump())
  34. except Exception as e:
  35. log.exception(e)
  36. raise HTTPException(
  37. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  38. )
  39. ############################
  40. # GetChannelMessages
  41. ############################
  42. @router.get("/{id}/messages", response_model=list[MessageModel])
  43. async def get_channel_messages(id: str, page: int = 1, user=Depends(get_verified_user)):
  44. channel = Channels.get_channel_by_id(id)
  45. if not channel:
  46. raise HTTPException(
  47. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  48. )
  49. if not has_access(user.id, type="read", access_control=channel.access_control):
  50. raise HTTPException(
  51. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  52. )
  53. limit = 50
  54. skip = (page - 1) * limit
  55. return Messages.get_messages_by_channel_id(id, skip, limit)
  56. ############################
  57. # PostNewMessage
  58. ############################
  59. @router.post("/{id}/messages/post", response_model=Optional[MessageModel])
  60. async def post_new_message(
  61. id: str, form_data: MessageForm, user=Depends(get_verified_user)
  62. ):
  63. channel = Channels.get_channel_by_id(id)
  64. if not channel:
  65. raise HTTPException(
  66. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  67. )
  68. if not has_access(user.id, type="read", access_control=channel.access_control):
  69. raise HTTPException(
  70. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  71. )
  72. try:
  73. message = Messages.insert_new_message(form_data, channel.id, user.id)
  74. if message:
  75. await sio.emit(
  76. "channel-events",
  77. {
  78. "channel_id": channel.id,
  79. "message_id": message.id,
  80. "data": {"type": "message", "data": message.model_dump()},
  81. },
  82. to=f"channel:{channel.id}",
  83. )
  84. return MessageModel(**message.model_dump())
  85. except Exception as e:
  86. log.exception(e)
  87. raise HTTPException(
  88. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  89. )