新增 Tlon 渠道扩展,支持在 Yuxi 平台中集成 Tlon/Urbit 去中心化通讯平台。 包含以下功能模块: - tlon_api: Tlon API 客户端封装 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - sse_client: SSE 客户端 - outbound: 外发消息管理 - send: 消息发送 - security: 安全校验 - auth: 认证管理 - monitor: 渠道状态监控 - status: 会话状态管理 - session: 会话管理 - approval: 审批流程 - channel_mgmt: 频道管理 - channel_ops: 频道操作 - contacts: 联系人管理 - discovery: 服务发现 - doctor: 健康诊断 - expose: 服务暴露 - gallery: 图库管理 - history: 历史记录 - hooks: 钩子管理 - media: 媒体资源处理 - notebook: 笔记本功能 - settings_store: 设置存储 - setup: 初始化设置 - story: 故事功能 - targets: 目标管理 - cite_parser: 引用解析 - utils: 工具函数 - types: 类型定义
92 lines
2.7 KiB
Python
92 lines
2.7 KiB
Python
import time
|
|
import logging
|
|
|
|
from yuxi.channel.extensions.tlon.story import markdown_to_story
|
|
from yuxi.channel.extensions.tlon.utils import format_at_ud
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def add_item(api, nest: str, author: str, title: str, content: str,
|
|
image_url: str = "") -> str:
|
|
item_id = f"{author}/{int(time.time() * 1000)}"
|
|
story = markdown_to_story(content)
|
|
action = {
|
|
"add-curio": {
|
|
"id": item_id,
|
|
"title": title,
|
|
"content": story,
|
|
"author": author,
|
|
"sent": int(time.time() * 1000),
|
|
}
|
|
}
|
|
if image_url:
|
|
action["add-curio"]["image"] = image_url
|
|
|
|
payload = {"channel": {"nest": nest, "action": action}}
|
|
await api.poke(app="heap", mark="heap-action", json_data=payload)
|
|
return item_id
|
|
|
|
|
|
async def edit_item(api, nest: str, item_id: str, title: str = "",
|
|
content: str = "") -> None:
|
|
action = {
|
|
"edit-curio": {
|
|
"id": format_at_ud(item_id),
|
|
}
|
|
}
|
|
if title:
|
|
action["edit-curio"]["title"] = title
|
|
if content:
|
|
action["edit-curio"]["content"] = markdown_to_story(content)
|
|
|
|
payload = {"channel": {"nest": nest, "action": action}}
|
|
await api.poke(app="heap", mark="heap-action", json_data=payload)
|
|
|
|
|
|
async def delete_item(api, nest: str, item_id: str) -> None:
|
|
payload = {
|
|
"channel": {
|
|
"nest": nest,
|
|
"action": {"del-curio": format_at_ud(item_id)},
|
|
}
|
|
}
|
|
await api.poke(app="heap", mark="heap-action", json_data=payload)
|
|
|
|
|
|
async def add_curator(api, nest: str, ship: str) -> None:
|
|
payload = {
|
|
"channel": {
|
|
"nest": nest,
|
|
"action": {"add-curator": ship},
|
|
}
|
|
}
|
|
await api.poke(app="heap", mark="heap-action", json_data=payload)
|
|
|
|
|
|
async def del_curator(api, nest: str, ship: str) -> None:
|
|
payload = {
|
|
"channel": {
|
|
"nest": nest,
|
|
"action": {"del-curator": ship},
|
|
}
|
|
}
|
|
await api.poke(app="heap", mark="heap-action", json_data=payload)
|
|
|
|
|
|
async def fetch_items(api, nest: str, limit: int = 20) -> list[dict]:
|
|
try:
|
|
data = await api.scry(f"/{nest}/curios/v1.json")
|
|
curios = data.get("curios", {})
|
|
result = []
|
|
for cid, curio in curios.items():
|
|
result.append({
|
|
"id": cid,
|
|
"title": curio.get("title", ""),
|
|
"author": curio.get("author", ""),
|
|
"sent": curio.get("sent", 0),
|
|
"image": curio.get("image", ""),
|
|
})
|
|
return sorted(result, key=lambda c: c["sent"], reverse=True)[:limit]
|
|
except Exception:
|
|
return [] |