feat: add sent report history lookup

This commit is contained in:
nyan404 committed 2026-10-18 18:56:23 +03:00
1 parent 546fa37fce
commit 3ebab303a5
12 files changed
+393 -18

No files matched your search

+37
View File
@@ -389,6 +389,43 @@ class Handler(BaseHTTPRequestHandler):
if path in ("/sudoku", "/sudoku/"):
self._file(WEB_ROOT / "sudoku.html")
return
if path == "/api/reports":
query = parse_qs(urlparse(self.path).query, keep_blank_values=True)
raw_character_id = query.get("character_id", [""])[0].strip()
if not re.fullmatch(r"\d{1,20}", raw_character_id) or int(raw_character_id) <= 0:
self._json(400, {"error": "Укажите положительный ID кота"})
return
character_id = int(raw_character_id)
if character_id > 9_223_372_036_854_775_807:
self._json(400, {"error": "ID кота слишком большой"})
return
try:
records = history_store().list_character_reports(character_id, limit=100)
except ValueError as exc:
self._json(400, {"error": str(exc)})
return
except Exception:
self._json(500, {"error": "Не удалось загрузить отчёты"})
return
items = []
for item in records:
attachments = []
for photo in (item.get("input") or {}).get("attachments", []):
if not isinstance(photo, dict):
continue
candidate = photo.get("mirror_url") or photo.get("url")
if not isinstance(candidate, str):
continue
image_path = urlparse(candidate).path
object_name = image_path.removeprefix("/api/images/")
if image_path.startswith("/api/images/") and IMAGE_OBJECT_NAME.fullmatch(object_name):
attachments.append({"url": image_path})
items.append({
"id": item["id"], "mode": item["mode"], "message": item["message"],
"created_at": item["created_at"], "attachments": attachments,
})
self._json(200, {"items": items, "limit": 100})
return
if path == "/api/sudoku/games":
try:
nickname = parse_qs(urlparse(self.path).query).get("nickname", [""])[0]