Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

5.0 KB
Raw Blame
from __future__ import annotations

from typing import Annotated

from fastapi import APIRouter, Depends, HTTPException, Response
from sqlalchemy.ext.asyncio import AsyncSession

from switch_core.bridges.resource.service import ResourceService
from switch_core.db.models import User
from switch_core.db.stores.room_store import RoomStore
from switch_core.gateway.auth import (
    get_current_user,
    get_tenant_is_admin,
    readable_room_ids,
    require_room_access,
)
from switch_core.gateway.dependencies import (
    get_resource_service,
    get_room_store,
    get_session,
)
from switch_core.gateway.schemas import (
    InboundLinkedRoomDetail,
    LinkedRoomCreateRequest,
    LinkedRoomDetail,
    RoomGraphResponse,
)

router = APIRouter()


@router.get("/linked-rooms/graph")
async def get_room_link_graph(
    session: Annotated[AsyncSession, Depends(get_session)],
    resource_service: Annotated[ResourceService, Depends(get_resource_service)],
    room_store: Annotated[RoomStore, Depends(get_room_store)],
    user: Annotated[User, Depends(get_current_user)],
    is_admin: Annotated[bool, Depends(get_tenant_is_admin)],
) -> RoomGraphResponse:
    readable = await room_store.list_readable(
        session, user.id, is_admin=is_admin, include_archived=True
    )
    payload = await resource_service.get_room_link_graph(session, readable)
    return RoomGraphResponse(**payload)


@router.get("/rooms/{room_id}/linked-rooms")
async def list_linked_rooms(
    room_id: str,
    session: Annotated[AsyncSession, Depends(get_session)],
    resource_service: Annotated[ResourceService, Depends(get_resource_service)],
    room_store: Annotated[RoomStore, Depends(get_room_store)],
    user: Annotated[User, Depends(get_current_user)],
    is_admin: Annotated[bool, Depends(get_tenant_is_admin)],
) -> list[LinkedRoomDetail]:
    await require_room_access(session, room_store, room_id, user, "read", is_admin)
    rows = await resource_service.list_linked_rooms_for_room(session, room_id)
    readable = await readable_room_ids(session, room_store, user, is_admin)
    return [
        LinkedRoomDetail(**row) for row in rows if row["target_room_id"] in readable
    ]


@router.get("/rooms/{room_id}/linked-rooms/inbound")
async def list_inbound_linked_rooms(
    room_id: str,
    session: Annotated[AsyncSession, Depends(get_session)],
    resource_service: Annotated[ResourceService, Depends(get_resource_service)],
    room_store: Annotated[RoomStore, Depends(get_room_store)],
    user: Annotated[User, Depends(get_current_user)],
    is_admin: Annotated[bool, Depends(get_tenant_is_admin)],
) -> list[InboundLinkedRoomDetail]:
    await require_room_access(session, room_store, room_id, user, "read", is_admin)
    rows = await resource_service.list_inbound_linked_rooms(session, room_id)
    readable = await readable_room_ids(session, room_store, user, is_admin)
    return [
        InboundLinkedRoomDetail(**row)
        for row in rows
        if row["source_room_id"] in readable
    ]


@router.post("/rooms/{room_id}/linked-rooms", status_code=201)
async def create_linked_room(
    room_id: str,
    req: LinkedRoomCreateRequest,
    session: Annotated[AsyncSession, Depends(get_session)],
    resource_service: Annotated[ResourceService, Depends(get_resource_service)],
    room_store: Annotated[RoomStore, Depends(get_room_store)],
    user: Annotated[User, Depends(get_current_user)],
    is_admin: Annotated[bool, Depends(get_tenant_is_admin)],
) -> LinkedRoomDetail:
    await require_room_access(session, room_store, room_id, user, "write", is_admin)
    # The link shows the target's name and description, so it must be readable.
    await require_room_access(
        session, room_store, req.target_room_id, user, "read", is_admin
    )
    try:
        row = await resource_service.attach_linked_room(
            session,
            source_room_id=room_id,
            target_room_id=req.target_room_id,
            label=req.label,
        )
    except LookupError as e:
        raise HTTPException(status_code=404, detail=str(e)) from e
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e)) from e
    await session.commit()
    return LinkedRoomDetail(**row)


@router.delete("/rooms/{room_id}/linked-rooms/{target_room_id}", status_code=204)
async def delete_linked_room(
    room_id: str,
    target_room_id: str,
    session: Annotated[AsyncSession, Depends(get_session)],
    resource_service: Annotated[ResourceService, Depends(get_resource_service)],
    room_store: Annotated[RoomStore, Depends(get_room_store)],
    user: Annotated[User, Depends(get_current_user)],
    is_admin: Annotated[bool, Depends(get_tenant_is_admin)],
) -> Response:
    await require_room_access(session, room_store, room_id, user, "write", is_admin)
    removed = await resource_service.detach_linked_room(
        session, source_room_id=room_id, target_room_id=target_room_id
    )
    if not removed:
        raise HTTPException(
            status_code=404,
            detail=f"No link from {room_id} to {target_room_id}",
        )
    await session.commit()
    return Response(status_code=204)