channels.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import json
  2. import logging
  3. from typing import Optional
  4. from open_webui.models.channels import Channels, ChannelModel, ChannelForm
  5. from open_webui.models.messages import Messages, MessageModel, MessageForm
  6. from open_webui.config import ENABLE_ADMIN_CHAT_ACCESS, ENABLE_ADMIN_EXPORT
  7. from open_webui.constants import ERROR_MESSAGES
  8. from open_webui.env import SRC_LOG_LEVELS
  9. from fastapi import APIRouter, Depends, HTTPException, Request, status
  10. from pydantic import BaseModel
  11. from open_webui.utils.auth import get_admin_user, get_verified_user
  12. from open_webui.utils.access_control import has_permission
  13. log = logging.getLogger(__name__)
  14. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  15. router = APIRouter()
  16. ############################
  17. # GetChatList
  18. ############################
  19. @router.get("/", response_model=list[ChannelModel])
  20. async def get_channels(user=Depends(get_verified_user)):
  21. return Channels.get_channels()
  22. ############################
  23. # CreateNewChannel
  24. ############################
  25. @router.post("/create", response_model=Optional[ChannelModel])
  26. async def create_new_channel(form_data: ChannelForm, user=Depends(get_admin_user)):
  27. try:
  28. channel = Channels.insert_new_channel(form_data, user.id)
  29. return ChannelModel(**channel.model_dump())
  30. except Exception as e:
  31. log.exception(e)
  32. raise HTTPException(
  33. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  34. )
  35. ############################
  36. # GetChannelMessages
  37. ############################
  38. @router.post("/{id}/messages", response_model=list[MessageModel])
  39. async def get_channel_messages(id: str, page: int = 1, user=Depends(get_verified_user)):
  40. channel = Channels.get_channel_by_id(id)
  41. if not channel:
  42. raise HTTPException(
  43. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  44. )
  45. if not has_permission(channel.access_control, user):
  46. raise HTTPException(
  47. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  48. )
  49. limit = 50
  50. skip = (page - 1) * limit
  51. return Messages.get_messages_by_channel_id(id, skip, limit)
  52. ############################
  53. # PostNewMessage
  54. ############################
  55. @router.post("/{id}/messages/post", response_model=Optional[MessageModel])
  56. async def post_new_message(
  57. id: str, form_data: MessageForm, user=Depends(get_verified_user)
  58. ):
  59. channel = Channels.get_channel_by_id(id)
  60. if not channel:
  61. raise HTTPException(
  62. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  63. )
  64. if not has_permission(channel.access_control, user):
  65. raise HTTPException(
  66. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  67. )
  68. try:
  69. message = Messages.insert_new_message(form_data, channel.id, user.id)
  70. return MessageModel(**message.model_dump())
  71. except Exception as e:
  72. log.exception(e)
  73. raise HTTPException(
  74. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  75. )