2026-05-28 04:37:27 +05:30
|
|
|
"""External chat binding helpers."""
|
2026-05-27 23:37:54 +05:30
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from datetime import UTC, datetime
|
|
|
|
|
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
from app.db import (
|
|
|
|
|
ChatVisibility,
|
2026-05-28 04:37:27 +05:30
|
|
|
ExternalChatBinding,
|
2026-06-01 12:36:53 +05:30
|
|
|
ExternalChatBindingState,
|
2026-05-27 23:37:54 +05:30
|
|
|
NewChatThread,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_or_create_thread_for_binding(
|
|
|
|
|
session: AsyncSession,
|
2026-05-28 04:37:27 +05:30
|
|
|
binding: ExternalChatBinding,
|
2026-05-27 23:37:54 +05:30
|
|
|
) -> NewChatThread:
|
2026-05-28 04:37:27 +05:30
|
|
|
if binding.new_chat_thread_id is not None:
|
2026-05-27 23:37:54 +05:30
|
|
|
result = await session.execute(
|
2026-05-28 04:37:27 +05:30
|
|
|
select(NewChatThread).where(NewChatThread.id == binding.new_chat_thread_id)
|
2026-05-27 23:37:54 +05:30
|
|
|
)
|
|
|
|
|
thread = result.scalars().first()
|
|
|
|
|
if thread is not None and not thread.archived:
|
|
|
|
|
return thread
|
|
|
|
|
|
2026-06-01 12:36:53 +05:30
|
|
|
source = str((binding.external_metadata or {}).get("platform") or "").strip()
|
|
|
|
|
if not source:
|
|
|
|
|
kind = str((binding.external_metadata or {}).get("kind") or "")
|
|
|
|
|
source = "slack" if kind.startswith("slack_") else "telegram"
|
|
|
|
|
|
2026-05-27 23:37:54 +05:30
|
|
|
thread = NewChatThread(
|
2026-06-01 12:36:53 +05:30
|
|
|
title=f"{source.title()} chat",
|
2026-05-27 23:37:54 +05:30
|
|
|
search_space_id=binding.search_space_id,
|
|
|
|
|
created_by_id=binding.user_id,
|
|
|
|
|
visibility=ChatVisibility.PRIVATE,
|
2026-06-01 12:36:53 +05:30
|
|
|
source=source,
|
2026-05-28 04:37:27 +05:30
|
|
|
external_chat_binding_id=binding.id,
|
2026-05-27 23:37:54 +05:30
|
|
|
)
|
|
|
|
|
session.add(thread)
|
|
|
|
|
await session.flush()
|
2026-05-28 04:37:27 +05:30
|
|
|
binding.new_chat_thread_id = thread.id
|
2026-05-27 23:37:54 +05:30
|
|
|
return thread
|
|
|
|
|
|
|
|
|
|
|
2026-05-28 04:37:27 +05:30
|
|
|
def suspend_binding(binding: ExternalChatBinding, reason: str) -> None:
|
2026-05-27 23:37:54 +05:30
|
|
|
now = datetime.now(UTC)
|
2026-05-28 04:37:27 +05:30
|
|
|
binding.state = ExternalChatBindingState.SUSPENDED
|
2026-05-27 23:37:54 +05:30
|
|
|
binding.suspended_at = now
|
|
|
|
|
binding.suspended_reason = reason
|
|
|
|
|
|
|
|
|
|
|
2026-05-28 04:37:27 +05:30
|
|
|
def revoke_binding(binding: ExternalChatBinding) -> None:
|
2026-05-27 23:37:54 +05:30
|
|
|
now = datetime.now(UTC)
|
2026-05-28 04:37:27 +05:30
|
|
|
binding.state = ExternalChatBindingState.REVOKED
|
2026-05-27 23:37:54 +05:30
|
|
|
binding.revoked_at = now
|
2026-05-28 04:37:27 +05:30
|
|
|
binding.new_chat_thread_id = None
|
2026-05-27 23:37:54 +05:30
|
|
|
|
|
|
|
|
|
2026-05-28 04:37:27 +05:30
|
|
|
def resume_binding(binding: ExternalChatBinding) -> None:
|
|
|
|
|
binding.state = ExternalChatBindingState.BOUND
|
2026-05-27 23:37:54 +05:30
|
|
|
binding.suspended_at = None
|
|
|
|
|
binding.suspended_reason = None
|
|
|
|
|
|