新增 Twitter 和 Viber 两个渠道扩展。 Twitter 渠道扩展功能模块: - auth: OAuth 认证管理 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - session: 会话管理 - tweets: 推文管理 - social: 社交互动 - reactions: 表情反应 - media: 媒体资源处理 Viber 渠道扩展功能模块: - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - rate_limiter: 速率限制 - media: 媒体资源处理
48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
import time
|
|
from collections import OrderedDict
|
|
|
|
|
|
class ViberEventDeduplicator:
|
|
def __init__(self, max_size: int = 4096, ttl_seconds: int = 3600):
|
|
self._cache: OrderedDict[str, float] = OrderedDict()
|
|
self._max_size = max_size
|
|
self._ttl = ttl_seconds
|
|
|
|
def is_duplicate(self, event_key: str) -> bool:
|
|
if not event_key:
|
|
return False
|
|
|
|
now = time.monotonic()
|
|
self._evict_expired(now)
|
|
|
|
if event_key in self._cache:
|
|
return True
|
|
|
|
self._cache[event_key] = now
|
|
while len(self._cache) > self._max_size:
|
|
self._cache.popitem(last=False)
|
|
|
|
return False
|
|
|
|
def _evict_expired(self, now: float) -> None:
|
|
expired = [k for k, v in self._cache.items() if now - v > self._ttl]
|
|
for k in expired:
|
|
del self._cache[k]
|
|
|
|
def reset(self) -> None:
|
|
self._cache.clear()
|
|
|
|
|
|
def build_event_dedupe_key(account_id: str, callback: dict) -> str:
|
|
message_token = callback.get("message_token")
|
|
if message_token:
|
|
return f"{account_id}|token:{message_token}"
|
|
|
|
event_type = callback.get("event", "unknown")
|
|
sender = callback.get("sender", {})
|
|
sender_id = sender.get("id", "")
|
|
timestamp = callback.get("timestamp", 0)
|
|
return f"{account_id}|{event_type}|{sender_id}|{timestamp}"
|