45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
|
|
import logging
|
||
|
|
|
||
|
|
from yuxi.channel.extensions.tlon.utils import normalize_ship
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
async def expose_publish(api, author: str, title: str, content: str,
|
||
|
|
tags: list[str] | None = None) -> str:
|
||
|
|
post_id = f"{author}/{int(__import__('time').time() * 1000)}"
|
||
|
|
payload = {
|
||
|
|
"publish": {
|
||
|
|
"id": post_id,
|
||
|
|
"title": title,
|
||
|
|
"content": content,
|
||
|
|
"author": author,
|
||
|
|
"tags": tags or [],
|
||
|
|
}
|
||
|
|
}
|
||
|
|
await api.poke(app="expose", mark="expose-action", json_data=payload)
|
||
|
|
return post_id
|
||
|
|
|
||
|
|
|
||
|
|
async def expose_unpublish(api, post_id: str) -> None:
|
||
|
|
payload = {"unpublish": post_id}
|
||
|
|
await api.poke(app="expose", mark="expose-action", json_data=payload)
|
||
|
|
|
||
|
|
|
||
|
|
async def expose_fetch(api, limit: int = 20) -> list[dict]:
|
||
|
|
try:
|
||
|
|
data = await api.scry("/expose/posts/v1.json")
|
||
|
|
posts = data.get("posts", {})
|
||
|
|
result = []
|
||
|
|
for pid, post in posts.items():
|
||
|
|
result.append({
|
||
|
|
"id": pid,
|
||
|
|
"title": post.get("title", ""),
|
||
|
|
"author": post.get("author", ""),
|
||
|
|
"content": post.get("content", ""),
|
||
|
|
"tags": post.get("tags", []),
|
||
|
|
"published": post.get("published", 0),
|
||
|
|
})
|
||
|
|
return sorted(result, key=lambda p: p.get("published", 0), reverse=True)[:limit]
|
||
|
|
except Exception:
|
||
|
|
return []
|