ForcePilot/backend/package/yuxi/channel/extensions/nostr/reactions.py
Kris 16455cb303 feat(channel): 添加 Nostr 渠道扩展
新增 Nostr 渠道扩展,支持在 Yuxi 平台中集成 Nostr 去中心化社交协议。

包含以下功能模块:
- bus: 事件总线与中继通信
- account: 账户管理
- key_utils: 密钥工具
- gateway: SSE/WebSocket 网关接入
- outbound: 外发消息管理
- gift_wrap: Gift Wrap 加密
- nip44: NIP-44 加密协议
- config_schema: 配置模式
- defaults: 默认配置
- profile_core: 用户资料核心
- profile_publisher: 资料发布
- event_utils: 事件工具
- deletion: 事件删除
- reactions: 表情反应
- metrics: 指标监控
- seen_tracker: 已读追踪
- session_route: 会话路由
- state_store: 状态存储
2026-05-21 11:31:51 +08:00

46 lines
1.2 KiB
Python

from .event_utils import sign_event
from .defaults import DM_KIND, DM_CHAT_KIND
from .gift_wrap import create_gift_wrap
REACTION_KIND = 7
async def send_reaction(
bus,
target_event_id: str,
reaction: str = "+",
target_pubkey: str | None = None,
) -> dict:
tags = [
["e", target_event_id],
]
if target_pubkey:
tags.append(["p", target_pubkey])
encryption = getattr(bus.options, "encryption", "nip17")
if encryption == "nip04":
from .bus import nip04_encrypt
ciphertext = nip04_encrypt(bus.sk, target_pubkey, reaction)
event = sign_event(
sk=bus.sk,
pubkey=bus.pk,
kind=DM_KIND,
content=ciphertext,
tags=tags,
)
else:
if not target_pubkey:
return {"ok": False, "error": "target_pubkey required for nip17 reaction"}
result = create_gift_wrap(
sender_sk_hex=bus._sk_hex(),
sender_pk=bus.pk,
receiver_pk=target_pubkey,
content=reaction,
kind=DM_CHAT_KIND,
tags=tags,
)
event = result.event
return await bus._publish_event(event)