From ff656f20adbc23ef9f1be632c99c619759184042 Mon Sep 17 00:00:00 2001 From: pdxlocations Date: Sat, 18 Jul 2026 22:49:57 -0700 Subject: [PATCH] Implement local short name retrieval and update reply prefix logic for outgoing messages --- contact/utilities/utils.py | 20 +++++++++++++++++--- tests/test_contact_ui.py | 10 ++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/contact/utilities/utils.py b/contact/utilities/utils.py index f145e5e..d279c88 100644 --- a/contact/utilities/utils.py +++ b/contact/utilities/utils.py @@ -160,12 +160,26 @@ def get_time_ago(timestamp): REPLY_EXCERPT_LENGTH = 5 +def _local_short_name() -> str: + """Return this node's short name without depending on the message database.""" + nodes = getattr(interface_state.interface, "nodes", {}) or {} + for node in nodes.values(): + if node.get("num") == interface_state.myNodeNum: + short_name = node.get("user", {}).get("shortName") + if short_name: + return str(short_name) + return "" + + def build_reply_prefix(prefix: str, message: str) -> str: """Create Contact's local display marker for a native Meshtastic reply.""" sender_text = re.sub(r"^\[[^]]+\]\s*", "", prefix).strip() - sender_text = re.sub(r"^(?:>>|<<)\s*(?:\[[^]]+\]\s*)?", "", sender_text) - sender_match = re.search(r"(.+?)\s*:\s*$", sender_text) - sender = sender_match.group(1).strip() if sender_match else "me" + if sender_text.startswith(config.sent_message_prefix.strip()): + sender = _local_short_name() + else: + sender_text = re.sub(r"^(?:>>|<<)\s*(?:\[[^]]+\]\s*)?", "", sender_text) + sender_match = re.search(r"(.+?)\s*:\s*$", sender_text) + sender = sender_match.group(1).strip() if sender_match else "me" excerpt = " ".join(message.replace("\x00", "").split())[:REPLY_EXCERPT_LENGTH] return f" " diff --git a/tests/test_contact_ui.py b/tests/test_contact_ui.py index 3b964b4..f092415 100644 --- a/tests/test_contact_ui.py +++ b/tests/test_contact_ui.py @@ -1,4 +1,5 @@ import unittest +from types import SimpleNamespace from unittest import mock import contact.ui.default_config as config @@ -110,6 +111,15 @@ class ContactUiTests(unittest.TestCase): self.assertEqual(reply, " ") + def test_build_reply_prefix_uses_me_for_an_outgoing_message(self) -> None: + contact_ui.interface_state.myNodeNum = 123 + contact_ui.interface_state.interface = SimpleNamespace( + nodes={"!0000007b": {"num": 123, "user": {"shortName": "LRY"}}} + ) + reply = contact_ui.build_reply_prefix("[06:27:25] >> Sent[☼]: ", "Thanks Lairy") + + self.assertEqual(reply, " ") + def test_handle_ctrl_r_prefills_reply_for_message_at_cursor(self) -> None: ui_state.current_window = 1 ui_state.channel_list = ["Primary"]