mirror of
https://github.com/pdxlocations/contact.git
synced 2026-08-07 09:22:51 +02:00
Implement local short name retrieval and update reply prefix logic for outgoing messages
This commit is contained in:
@@ -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"<Re: {sender}: {excerpt}> "
|
||||
|
||||
|
||||
@@ -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, "<Re: B1G1: This > ")
|
||||
|
||||
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, "<Re: LRY: Thank> ")
|
||||
|
||||
def test_handle_ctrl_r_prefills_reply_for_message_at_cursor(self) -> None:
|
||||
ui_state.current_window = 1
|
||||
ui_state.channel_list = ["Primary"]
|
||||
|
||||
Reference in New Issue
Block a user