No sound for what was already there: the companion's queue and a room's catch-up
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
da20d3e93d
commit
73eed72391
4 files changed
+105
-1
No files matched your search
@@ -486,6 +486,12 @@ class ChatService:
|
||||
|
||||
The rule (JP, 2026-11-21):
|
||||
|
||||
* A message from the queue of the companion (``waited``) makes no sound, a mention
|
||||
included. The companion keeps what arrived while MeshTerm was not connected, and
|
||||
gives all of it at the connect. It is old news, and one sound for each message
|
||||
was a burst of sounds at each start.
|
||||
* A room post of the catch-up after a login makes no sound, for the same reason
|
||||
(refer to :meth:`~meshterm.services.rooms.RoomService.catching_up`).
|
||||
* A mention of our node in a channel or a room follows ``sound_mention``. A
|
||||
mention in a DM is not a mention here: the message is already for us, so it plays
|
||||
the DM sound.
|
||||
@@ -503,7 +509,10 @@ class ChatService:
|
||||
A failure here must never stop the recording of messages, so it only goes to the log.
|
||||
"""
|
||||
sound = getattr(self._ctx, "sound", None)
|
||||
if sound is None:
|
||||
if sound is None or message.waited:
|
||||
return
|
||||
rooms = getattr(self._ctx, "rooms", None)
|
||||
if message.is_post and rooms is not None and rooms.catching_up(message.sender):
|
||||
return
|
||||
try:
|
||||
prefs = self._ctx.preferences
|
||||
|
||||
@@ -33,6 +33,14 @@ if TYPE_CHECKING:
|
||||
from ..context import AppContext
|
||||
|
||||
|
||||
#: How long a room must send nothing before its catch-up counts as finished, in seconds.
|
||||
#: After a login, a room sends the posts that it kept for us, one after another. They are
|
||||
#: posts that were already there, so they play no sound (JP, 2026-11-21: "Reading what
|
||||
#: already was there shouldn't trigger sounds"). The room has no mark for the last post of
|
||||
#: its catch-up, and its clock is often wrong, so the end is a pause instead of a time.
|
||||
CATCH_UP_QUIET_S = 8.0
|
||||
|
||||
|
||||
class RoomService:
|
||||
"""Joins rooms, remembers what they let us do, and decides when to log in again."""
|
||||
|
||||
@@ -51,6 +59,9 @@ class RoomService:
|
||||
#: The login in progress to each room, and the password that it carries, indexed by
|
||||
#: peer.
|
||||
self._inflight: dict[str, tuple[asyncio.Future, str]] = {}
|
||||
#: The rooms that are sending their catch-up after a login, with the time of the last
|
||||
#: sign of it (the login or a post), on the monotonic clock, indexed by peer.
|
||||
self._catching_up: dict[str, float] = {}
|
||||
|
||||
@staticmethod
|
||||
def _peer(room: Contact) -> str:
|
||||
@@ -127,6 +138,33 @@ class RoomService:
|
||||
if peer:
|
||||
self._heard[peer.lower()] = time.monotonic()
|
||||
|
||||
def catching_up(self, peer: str | None, now: float | None = None) -> bool:
|
||||
"""True if a post from ``peer`` is part of the catch-up after a login.
|
||||
|
||||
A post that comes less than :data:`CATCH_UP_QUIET_S` after the login or after the
|
||||
last post of the catch-up is part of it, and it extends the catch-up. The first pause
|
||||
longer than that ends it. A post that carries the prefix of the room at any width
|
||||
counts, as for :meth:`heard_recently`.
|
||||
|
||||
Args:
|
||||
peer: The key prefix of the room, as the sender of the post carried it.
|
||||
now: The monotonic time now. Tests give it.
|
||||
|
||||
Returns:
|
||||
``True`` while the room sends what it already had.
|
||||
"""
|
||||
if not peer:
|
||||
return False
|
||||
peer = peer.lower()
|
||||
now = time.monotonic() if now is None else now
|
||||
for known, last in list(self._catching_up.items()):
|
||||
if known.startswith(peer) or peer.startswith(known):
|
||||
if now - last < CATCH_UP_QUIET_S:
|
||||
self._catching_up[known] = now
|
||||
return True
|
||||
del self._catching_up[known]
|
||||
return False
|
||||
|
||||
def heard_recently(self, room: Contact) -> bool:
|
||||
"""True if ``room`` was heard within ``room_relogin_minutes``: then no login is necessary.
|
||||
|
||||
@@ -220,6 +258,9 @@ class RoomService:
|
||||
async def _login_once(self, room: Contact, password: str, *, flood: bool) -> RoomLogin:
|
||||
"""One login exchange, and what its result means for the stores and the session."""
|
||||
device = await self._ctx.device()
|
||||
# The room can start its catch-up as soon as it has the login, before the reply
|
||||
# reaches us. Thus the catch-up starts here, and the accept below starts it again.
|
||||
self._catching_up[self._peer(room)] = time.monotonic()
|
||||
if flood:
|
||||
await device.reset_route(room)
|
||||
# The route in the contact cache is not valid now. The next read gets it again
|
||||
@@ -239,4 +280,5 @@ class RoomService:
|
||||
peer = self._peer(room)
|
||||
self._access[peer] = login.access
|
||||
self._heard[peer] = time.monotonic()
|
||||
self._catching_up[peer] = time.monotonic()
|
||||
return login
|
||||
@@ -2974,6 +2974,27 @@ async def test_sound_a_mention_as_a_message_plays_its_kind(
|
||||
await device.disconnect()
|
||||
|
||||
|
||||
async def test_sound_the_backlog_of_the_companion_is_silent(
|
||||
tmp_path: Path, repo: Repository
|
||||
) -> None:
|
||||
"""Messages that waited in the companion before the connect play nothing, mentions too."""
|
||||
from meshterm.services.sound import SoundEvent
|
||||
|
||||
device, ctx, chat, events = await _sound_chat(repo, tmp_path)
|
||||
try:
|
||||
await _record(chat, ctx, Message(text="hello", sender="d4e5f6a7", waited=True))
|
||||
text = "Ann: @[MockCompanion] look"
|
||||
await _record(chat, ctx, Message(text=text, channel=0, is_channel=True, waited=True))
|
||||
assert events == []
|
||||
await _record(chat, ctx, Message(text="live", sender="d4e5f6a7"))
|
||||
assert events == [SoundEvent.DIRECT_MESSAGE]
|
||||
# The backlog is still stored and counted: only the sound is skipped.
|
||||
assert chat.unread("dm:d4e5f6a7") == 2
|
||||
finally:
|
||||
await chat.stop()
|
||||
await device.disconnect()
|
||||
|
||||
|
||||
async def test_sound_a_conversation_with_no_row_is_silent(tmp_path: Path, repo: Repository) -> None:
|
||||
"""A message that the Chat picker cannot list (a repeater reply) plays nothing."""
|
||||
device, ctx, chat, events = await _sound_chat(repo, tmp_path)
|
||||
@@ -4182,3 +4203,22 @@ def test_public_offer_add_writes_slot_zero(monkeypatch: pytest.MonkeyPatch) -> N
|
||||
added = asyncio.run(_add_public_channel(ctx))
|
||||
assert writes == [(0, "Public", DEFAULT_PUBLIC_SECRET)]
|
||||
assert added is not None and added.on_device and added.channel_idx == 0
|
||||
|
||||
|
||||
async def test_sound_a_room_catch_up_is_silent(tmp_path: Path, repo: Repository) -> None:
|
||||
"""The posts that a room sends after a login were already there, so they play nothing."""
|
||||
from meshterm.services.sound import SoundEvent
|
||||
|
||||
device, ctx, chat, events = await _sound_chat(repo, tmp_path)
|
||||
try:
|
||||
catching_up = [True]
|
||||
ctx.rooms = SimpleNamespace(catching_up=lambda peer: catching_up[0])
|
||||
post = Message(text="old news", sender="aabbccddeeff", author="11223344")
|
||||
await chat._play_sound(post, "dm:aabbccddeeff")
|
||||
assert events == []
|
||||
catching_up[0] = False
|
||||
await chat._play_sound(post, "dm:aabbccddeeff")
|
||||
assert events == [SoundEvent.ROOM_POST]
|
||||
finally:
|
||||
await chat.stop()
|
||||
await device.disconnect()
|
||||
@@ -334,3 +334,16 @@ def test_a_mention_as_a_message_turns_the_mention_event_off(tmp_path: Path) -> N
|
||||
service, player, _, _, _ = _service(tmp_path, sound_mention="regular")
|
||||
assert service.notify(SoundEvent.MENTION) is False
|
||||
assert player.played == []
|
||||
|
||||
|
||||
def test_a_room_catch_up_lasts_until_a_pause() -> None:
|
||||
"""After a login, posts are part of the catch-up until the room pauses 8 s."""
|
||||
from meshterm.services.rooms import CATCH_UP_QUIET_S, RoomService
|
||||
|
||||
rooms = RoomService(SimpleNamespace()) # type: ignore[arg-type]
|
||||
assert rooms.catching_up("a1b2c3d4e5f6", now=10.0) is False # no login yet
|
||||
rooms._catching_up["a1b2c3d4e5f6"] = 100.0 # the login
|
||||
assert rooms.catching_up("A1B2C3D4", now=103.0) is True # a shorter prefix matches
|
||||
assert rooms.catching_up("a1b2c3d4e5f6", now=103.0 + CATCH_UP_QUIET_S - 1) is True
|
||||
assert rooms.catching_up("a1b2c3d4e5f6", now=200.0) is False # the pause ended it
|
||||
assert rooms.catching_up("a1b2c3d4e5f6", now=201.0) is False
|
||||
Reference in new issue
Block a user