123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712 |
- import json
- import logging
- from typing import Optional
- from fastapi import APIRouter, Depends, HTTPException, Request, status, BackgroundTasks
- from pydantic import BaseModel
- from open_webui.socket.main import sio, get_user_ids_from_room
- from open_webui.models.users import Users, UserNameResponse
- from open_webui.models.channels import Channels, ChannelModel, ChannelForm
- from open_webui.models.messages import (
- Messages,
- MessageModel,
- MessageResponse,
- MessageForm,
- )
- from open_webui.config import ENABLE_ADMIN_CHAT_ACCESS, ENABLE_ADMIN_EXPORT
- from open_webui.constants import ERROR_MESSAGES
- from open_webui.env import SRC_LOG_LEVELS
- from open_webui.utils.auth import get_admin_user, get_verified_user
- from open_webui.utils.access_control import has_access, get_users_with_access
- from open_webui.utils.webhook import post_webhook
- log = logging.getLogger(__name__)
- log.setLevel(SRC_LOG_LEVELS["MODELS"])
- router = APIRouter()
- ############################
- # GetChatList
- ############################
- @router.get("/", response_model=list[ChannelModel])
- async def get_channels(user=Depends(get_verified_user)):
- if user.role == "admin":
- return Channels.get_channels()
- else:
- return Channels.get_channels_by_user_id(user.id)
- ############################
- # CreateNewChannel
- ############################
- @router.post("/create", response_model=Optional[ChannelModel])
- async def create_new_channel(form_data: ChannelForm, user=Depends(get_admin_user)):
- try:
- channel = Channels.insert_new_channel(None, form_data, user.id)
- return ChannelModel(**channel.model_dump())
- except Exception as e:
- log.exception(e)
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
- )
- ############################
- # GetChannelById
- ############################
- @router.get("/{id}", response_model=Optional[ChannelModel])
- async def get_channel_by_id(id: str, user=Depends(get_verified_user)):
- channel = Channels.get_channel_by_id(id)
- if not channel:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
- )
- if user.role != "admin" and not has_access(
- user.id, type="read", access_control=channel.access_control
- ):
- raise HTTPException(
- status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
- )
- return ChannelModel(**channel.model_dump())
- ############################
- # UpdateChannelById
- ############################
- @router.post("/{id}/update", response_model=Optional[ChannelModel])
- async def update_channel_by_id(
- id: str, form_data: ChannelForm, user=Depends(get_admin_user)
- ):
- channel = Channels.get_channel_by_id(id)
- if not channel:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
- )
- try:
- channel = Channels.update_channel_by_id(id, form_data)
- return ChannelModel(**channel.model_dump())
- except Exception as e:
- log.exception(e)
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
- )
- ############################
- # DeleteChannelById
- ############################
- @router.delete("/{id}/delete", response_model=bool)
- async def delete_channel_by_id(id: str, user=Depends(get_admin_user)):
- channel = Channels.get_channel_by_id(id)
- if not channel:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
- )
- try:
- Channels.delete_channel_by_id(id)
- return True
- except Exception as e:
- log.exception(e)
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
- )
- ############################
- # GetChannelMessages
- ############################
- class MessageUserResponse(MessageResponse):
- user: UserNameResponse
- @router.get("/{id}/messages", response_model=list[MessageUserResponse])
- async def get_channel_messages(
- id: str, skip: int = 0, limit: int = 50, user=Depends(get_verified_user)
- ):
- channel = Channels.get_channel_by_id(id)
- if not channel:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
- )
- if user.role != "admin" and not has_access(
- user.id, type="read", access_control=channel.access_control
- ):
- raise HTTPException(
- status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
- )
- message_list = Messages.get_messages_by_channel_id(id, skip, limit)
- users = {}
- messages = []
- for message in message_list:
- if message.user_id not in users:
- user = Users.get_user_by_id(message.user_id)
- users[message.user_id] = user
- replies = Messages.get_replies_by_message_id(message.id)
- latest_reply_at = replies[0].created_at if replies else None
- messages.append(
- MessageUserResponse(
- **{
- **message.model_dump(),
- "reply_count": len(replies),
- "latest_reply_at": latest_reply_at,
- "reactions": Messages.get_reactions_by_message_id(message.id),
- "user": UserNameResponse(**users[message.user_id].model_dump()),
- }
- )
- )
- return messages
- ############################
- # PostNewMessage
- ############################
- async def send_notification(name, webui_url, channel, message, active_user_ids):
- users = get_users_with_access("read", channel.access_control)
- for user in users:
- if user.id in active_user_ids:
- continue
- else:
- if user.settings:
- webhook_url = user.settings.ui.get("notifications", {}).get(
- "webhook_url", None
- )
- if webhook_url:
- post_webhook(
- name,
- webhook_url,
- f"#{channel.name} - {webui_url}/channels/{channel.id}\n\n{message.content}",
- {
- "action": "channel",
- "message": message.content,
- "title": channel.name,
- "url": f"{webui_url}/channels/{channel.id}",
- },
- )
- @router.post("/{id}/messages/post", response_model=Optional[MessageModel])
- async def post_new_message(
- request: Request,
- id: str,
- form_data: MessageForm,
- background_tasks: BackgroundTasks,
- user=Depends(get_verified_user),
- ):
- channel = Channels.get_channel_by_id(id)
- if not channel:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
- )
- if user.role != "admin" and not has_access(
- user.id, type="read", access_control=channel.access_control
- ):
- raise HTTPException(
- status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
- )
- try:
- message = Messages.insert_new_message(form_data, channel.id, user.id)
- if message:
- event_data = {
- "channel_id": channel.id,
- "message_id": message.id,
- "data": {
- "type": "message",
- "data": MessageUserResponse(
- **{
- **message.model_dump(),
- "reply_count": 0,
- "latest_reply_at": None,
- "reactions": Messages.get_reactions_by_message_id(
- message.id
- ),
- "user": UserNameResponse(**user.model_dump()),
- }
- ).model_dump(),
- },
- "user": UserNameResponse(**user.model_dump()).model_dump(),
- "channel": channel.model_dump(),
- }
- await sio.emit(
- "channel-events",
- event_data,
- to=f"channel:{channel.id}",
- )
- if message.parent_id:
- # If this message is a reply, emit to the parent message as well
- parent_message = Messages.get_message_by_id(message.parent_id)
- if parent_message:
- await sio.emit(
- "channel-events",
- {
- "channel_id": channel.id,
- "message_id": parent_message.id,
- "data": {
- "type": "message:reply",
- "data": MessageUserResponse(
- **{
- **parent_message.model_dump(),
- "user": UserNameResponse(
- **Users.get_user_by_id(
- parent_message.user_id
- ).model_dump()
- ),
- }
- ).model_dump(),
- },
- "user": UserNameResponse(**user.model_dump()).model_dump(),
- "channel": channel.model_dump(),
- },
- to=f"channel:{channel.id}",
- )
- active_user_ids = get_user_ids_from_room(f"channel:{channel.id}")
- background_tasks.add_task(
- send_notification,
- request.app.state.WEBUI_NAME,
- request.app.state.config.WEBUI_URL,
- channel,
- message,
- active_user_ids,
- )
- return MessageModel(**message.model_dump())
- except Exception as e:
- log.exception(e)
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
- )
- ############################
- # GetChannelMessage
- ############################
- @router.get("/{id}/messages/{message_id}", response_model=Optional[MessageUserResponse])
- async def get_channel_message(
- id: str, message_id: str, user=Depends(get_verified_user)
- ):
- channel = Channels.get_channel_by_id(id)
- if not channel:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
- )
- if user.role != "admin" and not has_access(
- user.id, type="read", access_control=channel.access_control
- ):
- raise HTTPException(
- status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
- )
- message = Messages.get_message_by_id(message_id)
- if not message:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
- )
- if message.channel_id != id:
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
- )
- return MessageUserResponse(
- **{
- **message.model_dump(),
- "user": UserNameResponse(
- **Users.get_user_by_id(message.user_id).model_dump()
- ),
- }
- )
- ############################
- # GetChannelThreadMessages
- ############################
- @router.get(
- "/{id}/messages/{message_id}/thread", response_model=list[MessageUserResponse]
- )
- async def get_channel_thread_messages(
- id: str,
- message_id: str,
- skip: int = 0,
- limit: int = 50,
- user=Depends(get_verified_user),
- ):
- channel = Channels.get_channel_by_id(id)
- if not channel:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
- )
- if user.role != "admin" and not has_access(
- user.id, type="read", access_control=channel.access_control
- ):
- raise HTTPException(
- status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
- )
- message_list = Messages.get_messages_by_parent_id(id, message_id, skip, limit)
- users = {}
- messages = []
- for message in message_list:
- if message.user_id not in users:
- user = Users.get_user_by_id(message.user_id)
- users[message.user_id] = user
- messages.append(
- MessageUserResponse(
- **{
- **message.model_dump(),
- "reply_count": 0,
- "latest_reply_at": None,
- "reactions": Messages.get_reactions_by_message_id(message.id),
- "user": UserNameResponse(**users[message.user_id].model_dump()),
- }
- )
- )
- return messages
- ############################
- # UpdateMessageById
- ############################
- @router.post(
- "/{id}/messages/{message_id}/update", response_model=Optional[MessageModel]
- )
- async def update_message_by_id(
- id: str, message_id: str, form_data: MessageForm, user=Depends(get_verified_user)
- ):
- channel = Channels.get_channel_by_id(id)
- if not channel:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
- )
- if user.role != "admin" and not has_access(
- user.id, type="read", access_control=channel.access_control
- ):
- raise HTTPException(
- status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
- )
- message = Messages.get_message_by_id(message_id)
- if not message:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
- )
- if message.channel_id != id:
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
- )
- try:
- message = Messages.update_message_by_id(message_id, form_data)
- message = Messages.get_message_by_id(message_id)
- if message:
- await sio.emit(
- "channel-events",
- {
- "channel_id": channel.id,
- "message_id": message.id,
- "data": {
- "type": "message:update",
- "data": MessageUserResponse(
- **{
- **message.model_dump(),
- "user": UserNameResponse(
- **user.model_dump()
- ).model_dump(),
- }
- ).model_dump(),
- },
- "user": UserNameResponse(**user.model_dump()).model_dump(),
- "channel": channel.model_dump(),
- },
- to=f"channel:{channel.id}",
- )
- return MessageModel(**message.model_dump())
- except Exception as e:
- log.exception(e)
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
- )
- ############################
- # AddReactionToMessage
- ############################
- class ReactionForm(BaseModel):
- name: str
- @router.post("/{id}/messages/{message_id}/reactions/add", response_model=bool)
- async def add_reaction_to_message(
- id: str, message_id: str, form_data: ReactionForm, user=Depends(get_verified_user)
- ):
- channel = Channels.get_channel_by_id(id)
- if not channel:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
- )
- if user.role != "admin" and not has_access(
- user.id, type="read", access_control=channel.access_control
- ):
- raise HTTPException(
- status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
- )
- message = Messages.get_message_by_id(message_id)
- if not message:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
- )
- if message.channel_id != id:
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
- )
- try:
- Messages.add_reaction_to_message(message_id, user.id, form_data.name)
- message = Messages.get_message_by_id(message_id)
- await sio.emit(
- "channel-events",
- {
- "channel_id": channel.id,
- "message_id": message.id,
- "data": {
- "type": "message:reaction:add",
- "data": {
- **message.model_dump(),
- "user": UserNameResponse(
- **Users.get_user_by_id(message.user_id).model_dump()
- ).model_dump(),
- "name": form_data.name,
- },
- },
- "user": UserNameResponse(**user.model_dump()).model_dump(),
- "channel": channel.model_dump(),
- },
- to=f"channel:{channel.id}",
- )
- return True
- except Exception as e:
- log.exception(e)
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
- )
- ############################
- # RemoveReactionById
- ############################
- @router.post("/{id}/messages/{message_id}/reactions/remove", response_model=bool)
- async def remove_reaction_by_id_and_user_id_and_name(
- id: str, message_id: str, form_data: ReactionForm, user=Depends(get_verified_user)
- ):
- channel = Channels.get_channel_by_id(id)
- if not channel:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
- )
- if user.role != "admin" and not has_access(
- user.id, type="read", access_control=channel.access_control
- ):
- raise HTTPException(
- status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
- )
- message = Messages.get_message_by_id(message_id)
- if not message:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
- )
- if message.channel_id != id:
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
- )
- try:
- Messages.remove_reaction_by_id_and_user_id_and_name(
- message_id, user.id, form_data.name
- )
- message = Messages.get_message_by_id(message_id)
- await sio.emit(
- "channel-events",
- {
- "channel_id": channel.id,
- "message_id": message.id,
- "data": {
- "type": "message:reaction:remove",
- "data": {
- **message.model_dump(),
- "user": UserNameResponse(
- **Users.get_user_by_id(message.user_id).model_dump()
- ).model_dump(),
- "name": form_data.name,
- },
- },
- "user": UserNameResponse(**user.model_dump()).model_dump(),
- "channel": channel.model_dump(),
- },
- to=f"channel:{channel.id}",
- )
- return True
- except Exception as e:
- log.exception(e)
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
- )
- ############################
- # DeleteMessageById
- ############################
- @router.delete("/{id}/messages/{message_id}/delete", response_model=bool)
- async def delete_message_by_id(
- id: str, message_id: str, user=Depends(get_verified_user)
- ):
- channel = Channels.get_channel_by_id(id)
- if not channel:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
- )
- if user.role != "admin" and not has_access(
- user.id, type="read", access_control=channel.access_control
- ):
- raise HTTPException(
- status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
- )
- message = Messages.get_message_by_id(message_id)
- if not message:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
- )
- if message.channel_id != id:
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
- )
- try:
- Messages.delete_message_by_id(message_id)
- await sio.emit(
- "channel-events",
- {
- "channel_id": channel.id,
- "message_id": message.id,
- "data": {
- "type": "message:delete",
- "data": {
- **message.model_dump(),
- "user": UserNameResponse(**user.model_dump()).model_dump(),
- },
- },
- "user": UserNameResponse(**user.model_dump()).model_dump(),
- "channel": channel.model_dump(),
- },
- to=f"channel:{channel.id}",
- )
- if message.parent_id:
- # If this message is a reply, emit to the parent message as well
- parent_message = Messages.get_message_by_id(message.parent_id)
- if parent_message:
- await sio.emit(
- "channel-events",
- {
- "channel_id": channel.id,
- "message_id": parent_message.id,
- "data": {
- "type": "message:reply",
- "data": MessageUserResponse(
- **{
- **parent_message.model_dump(),
- "user": UserNameResponse(
- **Users.get_user_by_id(
- parent_message.user_id
- ).model_dump()
- ),
- }
- ).model_dump(),
- },
- "user": UserNameResponse(**user.model_dump()).model_dump(),
- "channel": channel.model_dump(),
- },
- to=f"channel:{channel.id}",
- )
- return True
- except Exception as e:
- log.exception(e)
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
- )
|