75 lines
2.2 KiB
Python
75 lines
2.2 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 post_note(api, nest: str, author: str, title: str, content: str,
|
||
|
|
cover_url: str = "") -> str:
|
||
|
|
note_id = f"{author}/{int(time.time() * 1000)}"
|
||
|
|
story = markdown_to_story(content)
|
||
|
|
action = {
|
||
|
|
"post": {
|
||
|
|
"add": {
|
||
|
|
"id": note_id,
|
||
|
|
"title": title,
|
||
|
|
"content": story,
|
||
|
|
"author": author,
|
||
|
|
"sent": int(time.time() * 1000),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if cover_url:
|
||
|
|
action["post"]["add"]["image"] = cover_url
|
||
|
|
|
||
|
|
payload = {"channel": {"nest": nest, "action": action}}
|
||
|
|
await api.poke(app="diary", mark="diary-action", json_data=payload)
|
||
|
|
return note_id
|
||
|
|
|
||
|
|
|
||
|
|
async def edit_note(api, nest: str, note_id: str, title: str = "",
|
||
|
|
content: str = "") -> None:
|
||
|
|
action = {
|
||
|
|
"post": {
|
||
|
|
"edit": {
|
||
|
|
"id": format_at_ud(note_id),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if title:
|
||
|
|
action["post"]["edit"]["title"] = title
|
||
|
|
if content:
|
||
|
|
action["post"]["edit"]["content"] = markdown_to_story(content)
|
||
|
|
|
||
|
|
payload = {"channel": {"nest": nest, "action": action}}
|
||
|
|
await api.poke(app="diary", mark="diary-action", json_data=payload)
|
||
|
|
|
||
|
|
|
||
|
|
async def delete_note(api, nest: str, note_id: str) -> None:
|
||
|
|
payload = {
|
||
|
|
"channel": {
|
||
|
|
"nest": nest,
|
||
|
|
"action": {"post": {"del": format_at_ud(note_id)}},
|
||
|
|
}
|
||
|
|
}
|
||
|
|
await api.poke(app="diary", mark="diary-action", json_data=payload)
|
||
|
|
|
||
|
|
|
||
|
|
async def fetch_notes(api, nest: str, limit: int = 20) -> list[dict]:
|
||
|
|
try:
|
||
|
|
data = await api.scry(f"/{nest}/notes/v1.json")
|
||
|
|
notes = data.get("notes", {})
|
||
|
|
result = []
|
||
|
|
for nid, note in notes.items():
|
||
|
|
result.append({
|
||
|
|
"id": nid,
|
||
|
|
"title": note.get("title", ""),
|
||
|
|
"author": note.get("author", ""),
|
||
|
|
"sent": note.get("sent", 0),
|
||
|
|
})
|
||
|
|
return sorted(result, key=lambda n: n["sent"], reverse=True)[:limit]
|
||
|
|
except Exception:
|
||
|
|
return []
|