From da20d3e93d6c53f8272f580de99e208e0e580db3 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Sat, 21 Nov 2026 02:01:09 -0500 Subject: [PATCH] Mentions play always, on, or as a message; the open conversation can play too A mention in a DM plays the DM sound. The sound keys of this unpushed work are not retired: they never left this machine. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 7 +- docs/guide/features.md | 16 ++-- meshterm/core/preferences.py | 41 +++++---- meshterm/services/chat_service.py | 41 +++++---- meshterm/services/sound.py | 11 ++- tests/test_chat.py | 134 +++++++++++++++++++++--------- tests/test_sound.py | 12 ++- 7 files changed, 183 insertions(+), 79 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fb3e2b..c07c89b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,8 +23,11 @@ can change how things behave. It does not only add new things. - **MeshTerm plays sounds for the messages that you want to know about.** A DM, a mention of your node, a channel message, a room post, and a low battery each have their own sound, from Android's set (the Android Open Source Project, under the same licence as - MeshTerm). Each event has its own switch, all on except the packet tick. A muted channel - is silent, also for a mention, and so is the conversation that is open on the screen. + MeshTerm). Each event has its own switch, all on except the packet tick, and a muted + channel is silent. A mention plays *always* by default, also on a muted channel. It can + also follow the rules of other messages, or count as a regular message. A mention in a + DM plays the DM sound. *On-screen chat* (on by default) decides whether the open + conversation plays its sounds. Ctrl+S mutes or unmutes the sounds from any screen, and the header shows 🔊 or 🔇. The new *Sound* group of preferences has the theme (Android, terminal bell, or no sound), a volume slider of ten steps that plays each step, a short tick for each packet heard (off diff --git a/docs/guide/features.md b/docs/guide/features.md index 16758f2..96b1574 100644 --- a/docs/guide/features.md +++ b/docs/guide/features.md @@ -85,17 +85,23 @@ has its own sound, and its own switch on the Preferences page: | Event | When it plays | Default | | --- | --- | --- | | Direct message | A DM arrives | on | -| Mention | A message mentions your node (`@[Your name]`). With this switch off, the message plays the sound of its kind | on | +| Mention | A message in a channel or a room mentions your node (`@[Your name]`). A mention in a DM plays the DM sound | always | | Channel message | A message arrives on a channel | on | | Room post | A room sends a new post | on | | Battery low | The battery goes below the **Low level** (25%). It plays one time, and again only after the battery is 5 points higher or charges | on | | Packet tick | A very short tick for each packet heard that plays no other sound, at most four times each second | off | -Two things are always silent: +A channel that you muted (🔕) is silent, because the mute turns off all of its +notifications. The **On-screen chat** switch (on by default) decides whether the +conversation that is open on the screen plays its sounds too. -- The conversation that is open on the screen, because you already see it. -- A channel that you muted (🔕), also for a mention. The mute turns off all of the - notifications of the channel. +**Mention** has three values: + +- **always** (the default): the mention sound plays, also on a muted channel, with the + **Channel message** or **Room post** switch off, and with **On-screen chat** off. +- **on**: the mention sound plays, under the same rules as other messages. +- **as a message**: a mention is a channel message or a room post, with their sound and + their rules. **Ctrl+S** mutes or unmutes the sounds from any screen. The header shows 🔊 while the sounds play and 🔇 while they are muted. The **Sound** row above Quit in the main menu diff --git a/meshterm/core/preferences.py b/meshterm/core/preferences.py index 4984447..1b8756f 100644 --- a/meshterm/core/preferences.py +++ b/meshterm/core/preferences.py @@ -180,6 +180,13 @@ _SOUND_THEME_CHOICES: dict[str, str] = { #: player failed. _FLASH_CHOICES: dict[str, str] = {"off": "off", "on": "on", "silent": "when silent"} +#: How a mention of our node plays (JP, 2026-11-21). ``always`` plays the mention sound +#: also where the message would make no sound: on a muted channel, with the switch of its +#: kind off, and in the open conversation. ``on`` plays the mention sound under the same +#: rules as other messages. ``regular`` treats the mention as a channel message or a room +#: post. A mention in a DM is never a mention here: it plays the DM sound. +_MENTION_CHOICES: dict[str, str] = {"always": "always", "on": "on", "regular": "as a message"} + #: The sources of the place of a node (refer to :mod:`meshterm.services.places`). With #: ``tiles_nominatim``, the map tiles give the first answer at once, and Nominatim corrects it #: later, when MeshTerm is online, because only Nominatim knows the boundaries of a place. @@ -569,14 +576,16 @@ PREFERENCES: tuple[PrefSpec, ...] = ( PrefSpec( key="sound_mention", label="Mention", - help="When a message names you", + help="Always: also muted channels", question=( - "Play the mention sound when a message mentions your node. Off: the message" - " plays the sound of its kind" + "How a mention of your node plays. Always: the mention sound, also on a muted" + " channel. On: the mention sound, under the rules of other messages. As a" + " message: the sound of a channel message or a room post" ), group="Sound", - value_type="bool", - default=True, + value_type="enum", + default="always", + choices=_MENTION_CHOICES, platforms=_SOUND_PLATFORMS, ), PrefSpec( @@ -599,6 +608,17 @@ PREFERENCES: tuple[PrefSpec, ...] = ( default=True, platforms=_SOUND_PLATFORMS, ), + PrefSpec( + key="sound_on_screen", + label="On-screen chat", + help="The open conversation too", + question="Play sounds for the conversation that is open on the screen", + group="Sound", + value_type="bool", + # On (JP, 2026-11-21): the user can look away from a screen that stays open. + default=True, + platforms=_SOUND_PLATFORMS, + ), PrefSpec( key="sound_battery", label="Battery low", @@ -857,17 +877,6 @@ RETIRED: frozenset[str] = frozenset( # and later the weekly flood advert (``weekly_flood_advert``) replaced them. "advert_direct_hours", "advert_flood_hours", - # The first form of the sound preferences, replaced on the day they were made - # (2026-11-20): a sound on/off switch that the mute and the "no sound" theme - # replaced, and a volume in percent that a slider of ten steps replaced. - "sounds", - "sound_volume_pct", - # The sound of each kind of conversation (never, mentions only, or every message) - # and the mention override, replaced on 2026-11-21 by one switch for each event. - "dm_sound", - "channel_sound", - "room_sound", - "mention_sound_always", } ) diff --git a/meshterm/services/chat_service.py b/meshterm/services/chat_service.py index 151bddc..b1ed9b0 100644 --- a/meshterm/services/chat_service.py +++ b/meshterm/services/chat_service.py @@ -486,34 +486,47 @@ class ChatService: The rule (JP, 2026-11-21): - * The open conversation makes no sound, the same as its unread count: the user - already sees the message. - * A muted channel (``muted``, refer to MuteStore) makes no sound, also for a - mention. A mute turns off the notifications of the channel, and a sound is one. - * A mention of our node plays the mention sound. Other messages play the sound of - their kind: a DM, a channel message, or a room post. + * 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. + * ``always`` plays the mention sound whatever the rules below say: on a muted + channel, with the switch of its kind off, and in the open conversation. + * The open conversation plays nothing when ``sound_on_screen`` is off. + * A muted channel (``muted``, refer to MuteStore) plays nothing. A mute turns off the + notifications of the channel, and a sound is one. + * ``on`` plays the mention sound. ``regular`` plays the sound of the kind, as for any + other message: a channel message or a room post. * Each event has its own switch (refer to - :data:`~meshterm.services.sound.EVENT_PREFERENCE`). When the mention switch is - off, a mention plays the sound of its kind instead. When the switch of the kind is - off too, nothing plays. + :data:`~meshterm.services.sound.EVENT_PREFERENCE`), which the sound service + checks. A failure here must never stop the recording of messages, so it only goes to the log. """ - if key == self._active or muted: - return sound = getattr(self._ctx, "sound", None) if sound is None: return try: + prefs = self._ctx.preferences if message.is_channel: event = SoundEvent.CHANNEL_MESSAGE elif message.is_post: event = SoundEvent.ROOM_POST else: event = SoundEvent.DIRECT_MESSAGE - if sound.event_on(SoundEvent.MENTION) and await self._mentions_us(message.text): - event = SoundEvent.MENTION - sound.notify(event) + rule = prefs.sound_mention + mention = ( + event is not SoundEvent.DIRECT_MESSAGE + and rule != "regular" + and await self._mentions_us(message.text) + ) + if mention and rule == "always": + sound.notify(SoundEvent.MENTION) + return + if key == self._active and not prefs.sound_on_screen: + return + if muted: + return + sound.notify(SoundEvent.MENTION if mention else event) except Exception as exc: # noqa: BLE001 - a sound must never stop the recorder self._ctx.log.debug("chat: sound failed: %s", exc) diff --git a/meshterm/services/sound.py b/meshterm/services/sound.py index 62ebdd2..82018e4 100644 --- a/meshterm/services/sound.py +++ b/meshterm/services/sound.py @@ -96,10 +96,10 @@ TICK_DELAY_S = 0.15 BATTERY_REARM_PCT = 5 #: The preference that turns each event on or off (JP, 2026-11-21). All are on by default, -#: except the packet tick. +#: except the packet tick. The mention has a rule of three values instead +#: (``sound_mention``), and :meth:`SoundService.event_on` reads it. EVENT_PREFERENCE: dict[SoundEvent, str] = { SoundEvent.DIRECT_MESSAGE: "sound_dm", - SoundEvent.MENTION: "sound_mention", SoundEvent.CHANNEL_MESSAGE: "sound_channel", SoundEvent.ROOM_POST: "sound_room", SoundEvent.BATTERY_LOW: "sound_battery", @@ -323,7 +323,12 @@ class SoundService: return self._prefs.sound_theme != "none" def event_on(self, event: SoundEvent) -> bool: - """Whether the preference of an event turns it on (refer to EVENT_PREFERENCE).""" + """Whether the preference of an event turns it on (refer to EVENT_PREFERENCE). + + The mention is on unless its rule treats a mention as a regular message. + """ + if event is SoundEvent.MENTION: + return self._prefs.sound_mention != "regular" return bool(self._prefs.get(EVENT_PREFERENCE[event])) def toggle(self) -> bool: diff --git a/tests/test_chat.py b/tests/test_chat.py index f4f384c..60a3cc4 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -11,6 +11,7 @@ import asyncio import logging import re from pathlib import Path +from types import SimpleNamespace import pytest from rich.cells import cell_len @@ -2783,25 +2784,27 @@ async def test_badge_still_respects_a_muted_channel(tmp_path: Path, repo: Reposi class _FakeSound: - """Records each event that the chat service asks to play, and follows the switches.""" + """Records each event that the chat service asks to play, under the real rules. - def __init__(self, prefs: Preferences) -> None: - """Read the switches from ``prefs``.""" - from meshterm.services.sound import EVENT_PREFERENCE + It is the real sound service with its output replaced, so that the switches of the + events and the mention rule are the real ones. + """ + + def __init__(self, ctx) -> None: # noqa: ANN001 - the test context + """Wrap the real service around ``ctx``, with an output that records.""" + from meshterm.services.sound import SoundService - self._prefs = prefs - self._switch = EVENT_PREFERENCE self.events: list = [] + self._service = SoundService(ctx, player=SimpleNamespace(play=lambda path: True)) + self._service._emit = self._record # type: ignore[method-assign] - def event_on(self, event) -> bool: # noqa: ANN001 - a SoundEvent - """The switch of the event.""" - return bool(self._prefs.get(self._switch[event])) + def _record(self, event, volume=None) -> bool: # noqa: ANN001 - a SoundEvent + self.events.append(event) + return True def notify(self, event) -> bool: # noqa: ANN001 - a SoundEvent - """Record the event when its switch is on, as the service does.""" - if self.event_on(event): - self.events.append(event) - return True + """Play the event through the real rules of the service.""" + return self._service.notify(event) async def _sound_chat(repo: Repository, tmp_path: Path, **prefs: object): @@ -2815,7 +2818,7 @@ async def _sound_chat(repo: Repository, tmp_path: Path, **prefs: object): } ctx = _GatedContext(device, repo) ctx.preferences = Preferences(values=prefs) - ctx.sound = _FakeSound(ctx.preferences) + ctx.sound = _FakeSound(ctx) chat = ChatService(ctx) await chat.start() return device, ctx, chat, ctx.sound.events @@ -2843,8 +2846,9 @@ async def test_sound_each_kind_plays_its_own_event(tmp_path: Path, repo: Reposit try: await _record(chat, ctx, Message(text="hello", sender="d4e5f6a7")) await _record(chat, ctx, Message(text="Ann: hey", channel=0, is_channel=True)) - text = "Ann: hey @[MockCompanion]" - await _record(chat, ctx, Message(text=text, channel=0, is_channel=True)) + await _record( + chat, ctx, Message(text="Ann: @[MockCompanion] look", channel=0, is_channel=True) + ) assert events == [ SoundEvent.DIRECT_MESSAGE, SoundEvent.CHANNEL_MESSAGE, @@ -2855,6 +2859,19 @@ async def test_sound_each_kind_plays_its_own_event(tmp_path: Path, repo: Reposit await device.disconnect() +async def test_sound_a_mention_in_a_dm_is_a_dm(tmp_path: Path, repo: Repository) -> None: + """A DM is already for us, so a mention in it plays the DM sound (JP, 2026-11-21).""" + from meshterm.services.sound import SoundEvent + + device, ctx, chat, events = await _sound_chat(repo, tmp_path) + try: + await _record(chat, ctx, Message(text="@[MockCompanion] hi", sender="d4e5f6a7")) + assert events == [SoundEvent.DIRECT_MESSAGE] + finally: + await chat.stop() + await device.disconnect() + + async def test_sound_a_switched_off_event_is_silent(tmp_path: Path, repo: Repository) -> None: """``sound_channel = off`` silences channel messages, and DMs still play.""" from meshterm.services.sound import SoundEvent @@ -2869,44 +2886,31 @@ async def test_sound_a_switched_off_event_is_silent(tmp_path: Path, repo: Reposi await device.disconnect() -async def test_sound_a_mention_with_its_switch_off_plays_its_kind( +async def test_sound_the_open_conversation_plays_by_default( tmp_path: Path, repo: Repository ) -> None: - """With ``sound_mention`` off, a mention plays the sound of its kind instead.""" + """The conversation on the screen plays, until ``sound_on_screen`` is off.""" from meshterm.services.sound import SoundEvent - device, ctx, chat, events = await _sound_chat(repo, tmp_path, sound_mention=False) - try: - text = "Ann: @[MockCompanion] look" - await _record(chat, ctx, Message(text=text, channel=0, is_channel=True)) - assert events == [SoundEvent.CHANNEL_MESSAGE] - finally: - await chat.stop() - await device.disconnect() - - -async def test_sound_the_open_conversation_is_silent(tmp_path: Path, repo: Repository) -> None: - """The conversation on the screen plays nothing, the same as its unread count.""" device, ctx, chat, events = await _sound_chat(repo, tmp_path) try: chat.set_active("dm:d4e5f6a7") await _record(chat, ctx, Message(text="hello", sender="d4e5f6a7")) - assert events == [] + assert events == [SoundEvent.DIRECT_MESSAGE] + ctx.preferences.set("sound_on_screen", False) + await _record(chat, ctx, Message(text="again", sender="d4e5f6a7")) + assert events == [SoundEvent.DIRECT_MESSAGE] finally: await chat.stop() await device.disconnect() -async def test_sound_a_muted_channel_is_silent_also_for_a_mention( - tmp_path: Path, repo: Repository -) -> None: - """A mute turns off all notifications of a channel, a mention included (JP, 2026-11-21).""" +async def test_sound_a_muted_channel_is_silent(tmp_path: Path, repo: Repository) -> None: + """A mute turns off the notifications of a channel, and a sound is one.""" device, ctx, chat, events = await _sound_chat(repo, tmp_path) try: _mute_public(ctx, tmp_path) await _record(chat, ctx, Message(text="Ann: hey", channel=0, is_channel=True)) - text = "Ann: @[MockCompanion] look" - await _record(chat, ctx, Message(text=text, channel=0, is_channel=True)) assert events == [] assert chat.unread(_public_key()) == 0 finally: @@ -2914,6 +2918,62 @@ async def test_sound_a_muted_channel_is_silent_also_for_a_mention( await device.disconnect() +async def test_sound_an_always_mention_passes_every_rule(tmp_path: Path, repo: Repository) -> None: + """``always`` plays on a muted channel, with its kind off, and on screen with that off.""" + from meshterm.services.sound import SoundEvent + + device, ctx, chat, events = await _sound_chat( + repo, tmp_path, sound_channel=False, sound_on_screen=False + ) + try: + _mute_public(ctx, tmp_path) + chat.set_active(_public_key()) + await _record( + chat, ctx, Message(text="Ann: @[MockCompanion] look", channel=0, is_channel=True) + ) + assert events == [SoundEvent.MENTION] + finally: + await chat.stop() + await device.disconnect() + + +async def test_sound_an_on_mention_follows_the_rules(tmp_path: Path, repo: Repository) -> None: + """``on`` plays the mention sound, but a muted channel stays silent.""" + from meshterm.services.sound import SoundEvent + + device, ctx, chat, events = await _sound_chat(repo, tmp_path, sound_mention="on") + try: + await _record( + chat, ctx, Message(text="Ann: @[MockCompanion] look", channel=0, is_channel=True) + ) + assert events == [SoundEvent.MENTION] + _mute_public(ctx, tmp_path) + await _record( + chat, ctx, Message(text="Ann: @[MockCompanion] look", channel=0, is_channel=True) + ) + assert events == [SoundEvent.MENTION] + finally: + await chat.stop() + await device.disconnect() + + +async def test_sound_a_mention_as_a_message_plays_its_kind( + tmp_path: Path, repo: Repository +) -> None: + """``regular`` treats a mention as a channel message, with the rules of one.""" + from meshterm.services.sound import SoundEvent + + device, ctx, chat, events = await _sound_chat(repo, tmp_path, sound_mention="regular") + try: + await _record( + chat, ctx, Message(text="Ann: @[MockCompanion] look", channel=0, is_channel=True) + ) + assert events == [SoundEvent.CHANNEL_MESSAGE] + 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) diff --git a/tests/test_sound.py b/tests/test_sound.py index ddecb87..565bfe9 100644 --- a/tests/test_sound.py +++ b/tests/test_sound.py @@ -304,11 +304,12 @@ def test_no_tick_is_scheduled_when_the_tick_is_off(tmp_path: Path) -> None: def test_each_event_has_a_switch_on_by_default_except_the_tick() -> None: - """Each event names a preference. All are on by default, except the packet tick.""" + """Each event names a preference, on by default except the tick. A mention has a rule.""" from meshterm.core.preferences import get_spec from meshterm.services.sound import EVENT_PREFERENCE - assert set(EVENT_PREFERENCE) == set(SoundEvent) + assert set(EVENT_PREFERENCE) == set(SoundEvent) - {SoundEvent.MENTION} + assert get_spec("sound_mention").default == "always" for event, key in EVENT_PREFERENCE.items(): assert get_spec(key).default is (event is not SoundEvent.PACKET_TICK) @@ -326,3 +327,10 @@ def test_the_battery_switch_silences_the_battery_sound(tmp_path: Path) -> None: service, player, _, _, _ = _service(tmp_path, sound_battery=False) service.note_battery(10, charging=False) assert player.played == [] + + +def test_a_mention_as_a_message_turns_the_mention_event_off(tmp_path: Path) -> None: + """With the ``regular`` rule, the mention event plays nothing of its own.""" + service, player, _, _, _ = _service(tmp_path, sound_mention="regular") + assert service.notify(SoundEvent.MENTION) is False + assert player.played == []