admin.nomadro.com gets sidebar L1/L2 social ops (paginated lists + entity detail); API adds soft-delete archives/stats and shared nomadro_pb for social/coffee/notify/work. Co-authored-by: Cursor <cursoragent@cursor.com>
200 lines
5.0 KiB
Python
200 lines
5.0 KiB
Python
"""Fire Novu-domain notify_svc events from social (likes, coffee, board, city, rooms)."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
import os
|
|
import urllib.error
|
|
import urllib.parse
|
|
import urllib.request
|
|
from datetime import datetime
|
|
from typing import Any
|
|
from zoneinfo import ZoneInfo
|
|
|
|
log = logging.getLogger("social_svc.notify_bridge")
|
|
|
|
NOTIFY_INTERNAL_URL = os.getenv(
|
|
"NOTIFY_INTERNAL_URL",
|
|
os.getenv("NOTIFY_API_URL", "http://127.0.0.1:8023"),
|
|
).rstrip("/")
|
|
|
|
|
|
def _request(
|
|
method: str,
|
|
path: str,
|
|
body: dict[str, Any] | None = None,
|
|
timeout: float = 4.0,
|
|
) -> dict[str, Any] | None:
|
|
url = f"{NOTIFY_INTERNAL_URL}{path}"
|
|
data = json.dumps(body).encode("utf-8") if body is not None else None
|
|
headers = {"Accept": "application/json"}
|
|
if data is not None:
|
|
headers["Content-Type"] = "application/json"
|
|
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=timeout) as res:
|
|
raw = res.read().decode("utf-8", errors="replace")
|
|
return json.loads(raw) if raw else {}
|
|
except (urllib.error.URLError, TimeoutError, json.JSONDecodeError, OSError) as exc:
|
|
log.warning("notify %s %s failed: %s", method, path, exc)
|
|
return None
|
|
|
|
|
|
def ensure_subscriber(guest_id: str, name: str = "游民") -> None:
|
|
_request(
|
|
"POST",
|
|
"/api/notify/v1/subscribers",
|
|
{"subscriber_id": guest_id, "first_name": (name or "游民")[:40]},
|
|
)
|
|
|
|
|
|
def in_quiet_hours(channels: dict[str, Any] | None, *, dnd: bool = False) -> bool:
|
|
if dnd:
|
|
return True
|
|
if not channels:
|
|
return False
|
|
start = str(channels.get("quiet_start") or "").strip()
|
|
end = str(channels.get("quiet_end") or "").strip()
|
|
if not start or not end:
|
|
return False
|
|
tz_name = str(channels.get("timezone") or "Asia/Shanghai").strip() or "Asia/Shanghai"
|
|
try:
|
|
tz = ZoneInfo(tz_name)
|
|
except Exception:
|
|
tz = ZoneInfo("Asia/Shanghai")
|
|
now = datetime.now(tz)
|
|
try:
|
|
sh, sm = [int(x) for x in start.split(":")[:2]]
|
|
eh, em = [int(x) for x in end.split(":")[:2]]
|
|
except Exception:
|
|
return False
|
|
minutes = now.hour * 60 + now.minute
|
|
a = sh * 60 + sm
|
|
b = eh * 60 + em
|
|
if a == b:
|
|
return False
|
|
if a < b:
|
|
return a <= minutes < b
|
|
return minutes >= a or minutes < b
|
|
|
|
|
|
def _load_prefs(subscriber_id: str) -> tuple[dict[str, Any], bool]:
|
|
raw = _request(
|
|
"GET",
|
|
f"/api/notify/v1/subscribers/{urllib.parse.quote(subscriber_id)}/preferences",
|
|
)
|
|
if not raw:
|
|
return {}, False
|
|
prefs = raw.get("preferences") or raw
|
|
channels = prefs.get("channels") if isinstance(prefs.get("channels"), dict) else prefs
|
|
if not isinstance(channels, dict):
|
|
channels = {}
|
|
dnd = bool(prefs.get("dnd") or channels.get("dnd"))
|
|
return channels, dnd
|
|
|
|
|
|
def trigger(
|
|
workflow_id: str,
|
|
subscriber_id: str,
|
|
payload: dict[str, Any] | None = None,
|
|
*,
|
|
name: str = "游民",
|
|
respect_quiet: bool = True,
|
|
) -> dict[str, Any] | None:
|
|
ensure_subscriber(subscriber_id, name)
|
|
channels, dnd = _load_prefs(subscriber_id)
|
|
payload = dict(payload or {})
|
|
if respect_quiet and in_quiet_hours(channels, dnd=dnd):
|
|
payload["quietHours"] = True
|
|
return _request(
|
|
"POST",
|
|
"/api/notify/v1/events/trigger",
|
|
{
|
|
"workflow_id": workflow_id,
|
|
"subscriber_id": subscriber_id,
|
|
"payload": {**payload, "category": payload.get("category") or "social"},
|
|
},
|
|
)
|
|
|
|
|
|
def notify_like(to_guest: str, from_name: str) -> None:
|
|
trigger(
|
|
"social-like",
|
|
to_guest,
|
|
{"actor": from_name, "firstName": from_name, "ctaUrl": "/social/people"},
|
|
name=from_name,
|
|
)
|
|
|
|
|
|
def notify_coffee_invite(to_guest: str, host_name: str, title: str) -> None:
|
|
trigger(
|
|
"coffee-invite",
|
|
to_guest,
|
|
{"firstName": host_name, "title": title, "ctaUrl": "/social/coffeechat"},
|
|
name=host_name,
|
|
)
|
|
|
|
|
|
def notify_board_join(owner_guest: str, actor_name: str, board_title: str) -> None:
|
|
trigger(
|
|
"board-join",
|
|
owner_guest,
|
|
{
|
|
"firstName": actor_name,
|
|
"title": board_title,
|
|
"category": "project",
|
|
"ctaUrl": "/social/work/boards",
|
|
},
|
|
name=actor_name,
|
|
)
|
|
|
|
|
|
def notify_city_online(to_guest: str, city: str, peer_name: str) -> None:
|
|
trigger(
|
|
"city-online",
|
|
to_guest,
|
|
{"firstName": peer_name, "city": city, "ctaUrl": "/social/people"},
|
|
name=peer_name,
|
|
)
|
|
|
|
|
|
def notify_room_live(to_guest: str, room_title: str) -> None:
|
|
trigger(
|
|
"room-live",
|
|
to_guest,
|
|
{"title": room_title, "ctaUrl": "/social/meetup"},
|
|
)
|
|
|
|
|
|
def notify_report_result(reporter_guest: str, note: str, status: str) -> None:
|
|
trigger(
|
|
"report-result",
|
|
reporter_guest,
|
|
{
|
|
"status": status,
|
|
"resultNote": note,
|
|
"category": "system",
|
|
"ctaUrl": "/social/notifications",
|
|
},
|
|
respect_quiet=False,
|
|
)
|
|
|
|
|
|
def fire_city_digest(
|
|
subscriber_id: str,
|
|
city: str,
|
|
digest_text: str,
|
|
*,
|
|
weekly: bool = False,
|
|
) -> None:
|
|
trigger(
|
|
"city-digest-weekly" if weekly else "city-digest-daily",
|
|
subscriber_id,
|
|
{
|
|
"city": city,
|
|
"digestText": digest_text,
|
|
"firstName": "游民",
|
|
"ctaUrl": "/social/people",
|
|
},
|
|
)
|