rich_sent_store.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. """Local index of what we've sent (and, for WhatsApp, received) keyed by ``(chat_id, message_id)``.
  2. Telegram does NOT echo a rich message's content back in ``reply_to_message`` (``.text``/``.caption``
  3. empty, ``.api_kwargs`` None), and WhatsApp quotes carry only the quoted message's id (Cloud API) or a
  4. thumbnail stub (Baileys) — never the original bytes. So a reply to something we sent arrives with no
  5. quotable text and no way to re-fetch a quoted attachment. We remember ``message_id -> text`` and
  6. ``message_id -> [(local_path, mime)]`` at send/receive time and look them up by ``reply_to_id`` on
  7. inbound. Best-effort and dependency-free: every operation swallows errors and degrades to a no-op /
  8. ``None`` / ``[]`` so it can never break a send or an inbound message.
  9. """
  10. from __future__ import annotations
  11. import asyncio
  12. import json
  13. import os
  14. import threading
  15. import time
  16. from typing import Optional
  17. from utils import atomic_json_write
  18. _MAX_ENTRIES = 1000
  19. _MAX_TEXT_CHARS = 2000
  20. # ``atomic_json_write`` makes each WRITE atomic, not the load/merge/save triple.
  21. # ``record_async`` runs ``_update`` on worker threads, so two concurrent callers
  22. # (two inbound WhatsApp-Cloud messages, a Telegram send racing an edit) would
  23. # otherwise each load the same pre-state and the later ``os.replace`` drops the
  24. # other key.
  25. _LOCK = threading.Lock()
  26. def _store_path() -> str:
  27. from hermes_constants import get_hermes_home # honors the active profile override
  28. return os.path.join(str(get_hermes_home()), "state", "rich_sent_index.json")
  29. def _load(path: str) -> dict:
  30. try:
  31. with open(path, "r", encoding="utf-8-sig") as fh:
  32. data = json.load(fh)
  33. except (FileNotFoundError, ValueError):
  34. return {}
  35. return data if isinstance(data, dict) else {}
  36. def _update(chat_id, message_id, fields: dict) -> None:
  37. """Merge ``fields`` into the ``(chat_id, message_id)`` entry. No-op on any failure."""
  38. path = _store_path()
  39. with _LOCK:
  40. try:
  41. os.makedirs(os.path.dirname(path), exist_ok=True)
  42. data = _load(path)
  43. key = f"{chat_id}:{message_id}"
  44. entry = data.get(key)
  45. entry = entry if isinstance(entry, dict) else {}
  46. data[key] = {**entry, **fields, "ts": int(time.time())}
  47. if len(data) > _MAX_ENTRIES: # trim oldest by timestamp
  48. for k, _ in sorted(data.items(), key=lambda kv: kv[1].get("ts", 0))[: len(data) - _MAX_ENTRIES]:
  49. data.pop(k, None)
  50. atomic_json_write(path, data, indent=None) # see _LOCK
  51. except Exception:
  52. return
  53. def record(chat_id, message_id, text: Optional[str]) -> None:
  54. """Persist ``text`` for ``(chat_id, message_id)``. No-op on any failure."""
  55. if not text or message_id is None or chat_id is None:
  56. return
  57. _update(chat_id, message_id, {"t": text[:_MAX_TEXT_CHARS]})
  58. def record_media(chat_id, message_id, media: list[tuple[str, str]]) -> None:
  59. """Persist local attachment ``(path, mime)`` pairs for ``(chat_id, message_id)``."""
  60. if not media or message_id is None or chat_id is None:
  61. return
  62. _update(chat_id, message_id, {"m": [[str(p), str(mt or "")] for p, mt in media if p]})
  63. async def record_async(chat_id, message_id, text: Optional[str]) -> None:
  64. """``record`` for coroutine callers: the read-modify-write + ``os.replace``
  65. runs on a worker thread so the event loop is not stalled by the filesystem."""
  66. await asyncio.to_thread(record, chat_id, message_id, text)
  67. async def record_media_async(chat_id, message_id, media: list[tuple[str, str]]) -> None:
  68. """``record_media`` for coroutine callers; see ``record_async``."""
  69. await asyncio.to_thread(record_media, chat_id, message_id, media)
  70. def _entry(chat_id, message_id) -> dict:
  71. if message_id is None or chat_id is None:
  72. return {}
  73. entry = _load(_store_path()).get(f"{chat_id}:{message_id}")
  74. return entry if isinstance(entry, dict) else {}
  75. def lookup(chat_id, message_id) -> Optional[str]:
  76. """Return stored text for ``(chat_id, message_id)`` or ``None``."""
  77. return _entry(chat_id, message_id).get("t") or None
  78. def lookup_media(chat_id, message_id) -> list[tuple[str, str]]:
  79. """Return stored ``(path, mime)`` pairs whose file still exists (attachments may be temp files)."""
  80. pairs = _entry(chat_id, message_id).get("m") or []
  81. return [(p, mt) for p, mt in pairs if isinstance(p, str) and os.path.isfile(p)]