deer-flow/backend/app/gateway/routers/im_notification_preferences.py
Totoro c994826d06
feat(channels): generalize group notifications and add opt-in task/goal IM notices (#6903)
* feat(channels): generalize IM group targets and add task/goal run notifications

- Generalize proactive group notification capability (proactive_group_notifications), binding validation, and scheduled task group selection across Feishu, QQ, and WeCom while keeping WeChat personal-only.
- Add QQ group active notification delivery via POST /v2/groups/{group_openid}/messages (msg_type=2) and WeCom group active notification delivery via WS Client.sendMessage(chatid, body) with bot_id:group:chatid workspace isolation.
- Generalize notification outbox sources via migration 0043_runtime_im_notices (source_kind, source_key, run_id, nullable task_id/task_run_id) with backward-compatible SQLite/PostgreSQL upgrades.
- Add opt-in ordinary task & goal run IM notifications (run_completed, run_failed, human_input_requested) on the shared outbox, delivery worker, renderer, and settings UI.

* fix(notifications): address review feedback and group/runtime notification edge cases

* test(migrations): update CURRENT_HEAD to 0043_runtime_im_notices across migration chain tests

* fix(channels): preserve group binding names and legacy authorization

Carry owner-chosen group names through existing connect state, keep private bindings out of legacy group fallback, and restore completion metadata retries. Cover real binding and worker finalization paths and synchronize notification docs.

---------

Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
2026-10-15 10:30:55 +08:00

50 lines
2.0 KiB
Python

"""Owner-scoped proactive IM opt-outs, independent of browser notifications."""
from fastapi import APIRouter, Header, HTTPException, Request
from pydantic import BaseModel, ConfigDict
from deerflow.persistence.engine import get_session_factory
from deerflow.persistence.user.im_notifications import ImNotificationPreferencesRepository
router = APIRouter(prefix="/api/channels/notification-preferences", tags=["channel-connections"])
class ImNotificationPreferences(BaseModel):
model_config = ConfigDict(extra="forbid", strict=True)
feishu: bool | None = None
wecom: bool | None = None
wechat: bool | None = None
qq: bool | None = None
run_completed: bool | None = None
run_failed: bool | None = None
human_input_requested: bool | None = None
def _owner(request: Request, expected_user_id: str) -> str:
user = getattr(request.state, "user", None)
if user is None:
raise HTTPException(401, "Authentication required")
owner = str(user.id)
if owner != expected_user_id:
raise HTTPException(409, "The signed-in account changed; reload this page")
return owner
def _repository() -> ImNotificationPreferencesRepository:
sessions = get_session_factory()
if sessions is None:
raise HTTPException(503, "Notification preference persistence is unavailable")
return ImNotificationPreferencesRepository(sessions)
@router.get("", response_model=ImNotificationPreferences, response_model_exclude_none=True)
async def get_preferences(request: Request, x_expected_user_id: str = Header(...)) -> dict[str, bool]:
owner = _owner(request, x_expected_user_id)
return await _repository().get(owner)
@router.patch("", response_model=ImNotificationPreferences, response_model_exclude_none=True)
async def patch_preferences(body: ImNotificationPreferences, request: Request, x_expected_user_id: str = Header(...)) -> dict[str, bool]:
owner = _owner(request, x_expected_user_id)
return await _repository().patch(owner, body.model_dump(exclude_unset=True))