Spaces:
Running
Running
File size: 8,379 Bytes
db5f035 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | # Copyright (c) 2026 CrewHub. All rights reserved.
# Proprietary and confidential. See LICENSE for details.
"""Channel API routes — CRUD for multi-channel gateway connections."""
from uuid import UUID
from fastapi import APIRouter, Depends, Query
from sqlalchemy.ext.asyncio import AsyncSession
from src.core.audit import audit_log
from src.core.auth import resolve_db_user_id
from src.database import get_db
from src.schemas.channel import (
ChannelAnalytics,
ChannelCreate,
ChannelListResponse,
ChannelResponse,
ChannelTestResult,
ChannelUpdate,
)
from src.services.channel_service import ChannelService
router = APIRouter(prefix="/channels", tags=["channels"])
@router.post("/validate-token")
async def validate_token(
data: ChannelCreate,
owner_id: UUID = Depends(resolve_db_user_id),
db: AsyncSession = Depends(get_db),
):
"""Validate platform credentials without creating a channel."""
service = ChannelService(db)
result = await service._validate_token(data.platform, data.credentials)
return {
"valid": True,
"platform_bot_id": result.get("platform_bot_id"),
"bot_name": result.get("bot_name"),
}
@router.post("/", response_model=ChannelResponse, status_code=201)
async def create_channel(
data: ChannelCreate,
owner_id: UUID = Depends(resolve_db_user_id),
db: AsyncSession = Depends(get_db),
):
service = ChannelService(db)
ch = await service.create_channel(data, owner_id)
await audit_log(db, action="channel.create", actor_user_id=str(owner_id), target_type="channel", target_id=ch.id)
await db.commit()
await db.refresh(ch)
stats = await service._get_today_stats(ch.id)
return {**ch.__dict__, **stats}
@router.get("/", response_model=ChannelListResponse)
async def list_channels(
owner_id: UUID = Depends(resolve_db_user_id),
db: AsyncSession = Depends(get_db),
):
service = ChannelService(db)
enriched, total = await service.list_channels(owner_id)
channels = [{**ch.__dict__, **stats} for ch, stats in enriched]
return {"channels": channels, "total": total}
@router.get("/{channel_id}", response_model=ChannelResponse)
async def get_channel(
channel_id: UUID,
owner_id: UUID = Depends(resolve_db_user_id),
db: AsyncSession = Depends(get_db),
):
service = ChannelService(db)
ch, stats = await service.get_channel(channel_id, owner_id)
return {**ch.__dict__, **stats}
@router.patch("/{channel_id}", response_model=ChannelResponse)
async def update_channel(
channel_id: UUID,
data: ChannelUpdate,
owner_id: UUID = Depends(resolve_db_user_id),
db: AsyncSession = Depends(get_db),
):
service = ChannelService(db)
ch = await service.update_channel(channel_id, owner_id, data)
await audit_log(db, action="channel.update", actor_user_id=str(owner_id), target_type="channel", target_id=channel_id)
await db.commit()
await db.refresh(ch)
stats = await service._get_today_stats(ch.id)
return {**ch.__dict__, **stats}
@router.delete("/{channel_id}", status_code=204)
async def delete_channel(
channel_id: UUID,
owner_id: UUID = Depends(resolve_db_user_id),
db: AsyncSession = Depends(get_db),
):
service = ChannelService(db)
await service.delete_channel(channel_id, owner_id)
await audit_log(db, action="channel.delete", actor_user_id=str(owner_id), target_type="channel", target_id=channel_id)
await db.commit()
@router.get("/{channel_id}/analytics", response_model=ChannelAnalytics)
async def get_analytics(
channel_id: UUID,
days: int = Query(7, ge=1, le=90),
owner_id: UUID = Depends(resolve_db_user_id),
db: AsyncSession = Depends(get_db),
):
service = ChannelService(db)
return await service.get_analytics(channel_id, owner_id, days)
@router.post("/{channel_id}/rotate-token", response_model=ChannelResponse)
async def rotate_channel_token(
channel_id: UUID,
credentials: dict,
user_id: UUID = Depends(resolve_db_user_id),
db: AsyncSession = Depends(get_db),
) -> ChannelResponse:
"""Rotate bot token for an existing channel connection."""
service = ChannelService(db)
conn = await service.rotate_token(channel_id, user_id, credentials)
await audit_log(db, action="channel.rotate_token", actor_user_id=str(user_id), target_type="channel", target_id=channel_id)
await db.commit()
await db.refresh(conn)
stats = await service._get_today_stats(conn.id)
return ChannelResponse.model_validate({**conn.__dict__, **stats})
@router.post("/{channel_id}/test", response_model=ChannelTestResult)
async def test_channel(
channel_id: UUID,
owner_id: UUID = Depends(resolve_db_user_id),
db: AsyncSession = Depends(get_db),
):
# Placeholder — gateway not deployed in Phase 1
return {
"success": False,
"message": "Gateway not yet deployed. Channel is registered and pending.",
}
@router.get("/{channel_id}/contacts")
async def get_channel_contacts(
channel_id: UUID,
limit: int = Query(50, ge=1, le=200),
offset: int = Query(0, ge=0),
user_id: UUID = Depends(resolve_db_user_id),
db: AsyncSession = Depends(get_db),
):
service = ChannelService(db)
result = await service.get_contacts(channel_id, user_id, limit, offset)
await audit_log(db, action="channel.view_contacts", actor_user_id=str(user_id), target_type="channel", target_id=channel_id)
return result
@router.get("/{channel_id}/contacts/{user_hash}/messages")
async def get_contact_messages(
channel_id: UUID,
user_hash: str,
cursor: str | None = None,
limit: int = Query(20, ge=1, le=100),
user_id: UUID = Depends(resolve_db_user_id),
db: AsyncSession = Depends(get_db),
):
service = ChannelService(db)
# Filter messages by specific user hash
result = await service.get_channel_messages(channel_id, user_id, cursor=cursor, limit=limit)
if result:
result["messages"] = [m for m in result["messages"] if m["platform_user_id_hash"] == user_hash]
return result
@router.post("/{channel_id}/contacts/{user_hash}/block")
async def block_contact(
channel_id: UUID,
user_hash: str,
reason: str = "",
user_id: UUID = Depends(resolve_db_user_id),
db: AsyncSession = Depends(get_db),
):
service = ChannelService(db)
await service.block_contact(channel_id, user_id, user_hash, user_id, reason or None)
await audit_log(db, action="channel.block_contact", actor_user_id=str(user_id), target_type="channel_contact", target_id=channel_id, new_value={"user_hash": user_hash, "reason": reason})
await db.commit()
return {"status": "blocked", "user_hash": user_hash}
@router.delete("/{channel_id}/contacts/{user_hash}/block")
async def unblock_contact(
channel_id: UUID,
user_hash: str,
user_id: UUID = Depends(resolve_db_user_id),
db: AsyncSession = Depends(get_db),
):
service = ChannelService(db)
await service.unblock_contact(channel_id, user_id, user_hash)
await audit_log(db, action="channel.unblock_contact", actor_user_id=str(user_id), target_type="channel_contact", target_id=channel_id, new_value={"user_hash": user_hash})
await db.commit()
return {"status": "unblocked", "user_hash": user_hash}
@router.delete("/{channel_id}/contacts/{user_hash}/messages")
async def delete_contact_data(
channel_id: UUID,
user_hash: str,
user_id: UUID = Depends(resolve_db_user_id),
db: AsyncSession = Depends(get_db),
):
"""GDPR Article 17 — right to erasure for platform end-users."""
service = ChannelService(db)
result = await service.delete_contact_data(channel_id, user_id, user_hash)
await audit_log(db, action="channel.gdpr_erasure", actor_user_id=str(user_id), target_type="channel_contact", target_id=channel_id, new_value={"user_hash": user_hash, "deleted_messages": result["deleted_messages"]})
await db.commit()
return result
@router.get("/{channel_id}/messages")
async def get_channel_messages(
channel_id: UUID,
direction: str | None = None,
cursor: str | None = None,
limit: int = Query(20, ge=1, le=100),
user_id: UUID = Depends(resolve_db_user_id),
db: AsyncSession = Depends(get_db),
):
service = ChannelService(db)
return await service.get_channel_messages(channel_id, user_id, direction, cursor, limit)
|