voice_mention.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. """Park unmentioned MSC3245 voice messages until the sender's bare @mention claims them.
  2. Element X sends a mention typed while recording as a SEPARATE ``m.text`` event after the
  3. voice event (which carries an empty ``m.mentions``). Under ``require_mention`` the voice is
  4. parked instead of forgotten; a bare mention from the same sender in the same room within
  5. the window claims it. Unmentioned voices are never downloaded or transcribed while parked.
  6. """
  7. from __future__ import annotations
  8. import asyncio
  9. import time
  10. from typing import Dict, List, Optional, Tuple
  11. CLAIM_WINDOW_SECONDS = 120.0
  12. # How long a bare mention waits for a voice from the same /sync batch that is still being gated.
  13. SETTLE_TIMEOUT_SECONDS = 5.0
  14. # Voices a sender can have parked per room at once (oldest by arrival dropped beyond this). Known
  15. # limit: if this many (or more) newer voices park while a bare mention is still settling, the
  16. # older voice it was owed is dropped and the mention is answered as text, as on main.
  17. MAX_PARKED_PER_SENDER = 4
  18. # (voice event_id, content, relates_to)
  19. ParkedVoice = tuple[str, dict, dict]
  20. def has_voice_marker(content: dict) -> bool:
  21. """The single MSC3245 voice-message check (shared with the adapter's media classifier)."""
  22. return content.get("org.matrix.msc3245.voice") is not None
  23. def is_voice_event(content: dict) -> bool:
  24. return content.get("msgtype") == "m.audio" and has_voice_marker(content)
  25. class VoiceGate:
  26. """One voice being gated; ``seq`` orders voices from the same sender by arrival."""
  27. __slots__ = ("done", "seq")
  28. def __init__(self, seq: int) -> None:
  29. self.seq = seq
  30. self.done = asyncio.Event()
  31. class ParkedVoices:
  32. def __init__(self) -> None:
  33. # (room_id, sender) -> parked voices as (parked_at, seq, voice), ordered by seq, bounded.
  34. self._parked: dict[tuple[str, str], list[tuple[float, int, ParkedVoice]]] = {}
  35. # (room_id, sender) -> every voice of that sender still being gated. mautrix runs one
  36. # /sync batch's events as concurrent tasks, so a voice may still be awaiting room
  37. # identity when its bare mention is handled -- and a sender can have several in flight.
  38. self._inflight: dict[tuple[str, str], list[VoiceGate]] = {}
  39. # (room_id, sender) -> seq of the last claimed voice while gates were in flight, so an
  40. # older voice finishing late never parks after (and outlives) that claim.
  41. self._floor: dict[tuple[str, str], int] = {}
  42. self._next_seq = 0
  43. def _prune(self) -> None:
  44. cutoff = time.monotonic() - CLAIM_WINDOW_SECONDS
  45. kept = {k: [e for e in v if e[0] >= cutoff] for k, v in self._parked.items()}
  46. self._parked = {k: v for k, v in kept.items() if v}
  47. def pending(self, room_id: str, sender: str) -> bool:
  48. """Cheap pre-check: an unexpired voice is parked or one is still being gated."""
  49. key = (room_id, sender)
  50. if key in self._inflight:
  51. return True
  52. cutoff = time.monotonic() - CLAIM_WINDOW_SECONDS
  53. return any(e[0] >= cutoff for e in self._parked.get(key, ()))
  54. def mark(self) -> int:
  55. """Arrival limit for a bare mention: only voices that began before this may be claimed."""
  56. return self._next_seq
  57. def begin(self, room_id: str, sender: str) -> VoiceGate:
  58. """Mark a parkable voice as being gated. Call before the first await; always pair with
  59. ``release`` (idempotent, so it may run early and again in a ``finally``)."""
  60. gate = VoiceGate(self._next_seq)
  61. self._next_seq += 1 # unbounded Python int: never wraps
  62. self._inflight.setdefault((room_id, sender), []).append(gate)
  63. return gate
  64. def release(self, room_id: str, sender: str, gate: VoiceGate) -> None:
  65. gate.done.set()
  66. key = (room_id, sender)
  67. gates = self._inflight.get(key)
  68. if gates and gate in gates:
  69. gates.remove(gate)
  70. if not gates: # no older voice can park any more
  71. del self._inflight[key]
  72. self._floor.pop(key, None)
  73. async def settle(self, room_id: str, sender: str) -> None:
  74. """Wait (bounded) for every concurrently gated voice from this sender to park or drop."""
  75. gates = self._inflight.get((room_id, sender))
  76. if gates:
  77. waits = [asyncio.ensure_future(g.done.wait()) for g in gates]
  78. try:
  79. await asyncio.wait(waits, timeout=SETTLE_TIMEOUT_SECONDS)
  80. finally:
  81. for w in waits:
  82. w.cancel()
  83. def park(self, room_id: str, sender: str, gate: VoiceGate, event_id: str, content: dict,
  84. relates_to: dict) -> None:
  85. key = (room_id, sender)
  86. if gate.seq <= self._floor.get(key, -1):
  87. return # a newer voice from this sender was already claimed
  88. self._prune()
  89. entries = self._parked.setdefault(key, [])
  90. entries.append((time.monotonic(), gate.seq, (event_id, content, relates_to)))
  91. entries.sort(key=lambda e: e[1])
  92. del entries[:-MAX_PARKED_PER_SENDER]
  93. def claim(self, room_id: str, sender: str, before: int) -> Optional[ParkedVoice]:
  94. """Pop the sender's newest parked voice for this room that began before ``before``
  95. (``mark()`` taken when the bare mention arrived); older ones are dropped, later voices
  96. stay for their own mention. The caller re-dispatches it with ``mention_claimed=True``."""
  97. self._prune()
  98. key = (room_id, sender)
  99. entries = self._parked.get(key, [])
  100. idx = max((i for i, e in enumerate(entries) if e[1] < before), default=None)
  101. if idx is None:
  102. return None
  103. claimed = entries[idx]
  104. del entries[:idx + 1]
  105. if not entries:
  106. del self._parked[key]
  107. if key in self._inflight: # a late older voice must not park after this claim
  108. self._floor[key] = max(self._floor.get(key, -1), claimed[1])
  109. return claimed[2]