forked from iarv/Remote-Terminal-for-MeshCore
Force DM contact onto radio before send
This commit is contained in:
@@ -58,21 +58,21 @@ async def send_direct_message(request: SendDirectMessageRequest) -> Message:
|
||||
status_code=404, detail=f"Contact not found in database: {request.destination}"
|
||||
)
|
||||
|
||||
# Check if contact is on radio, if not add it
|
||||
# Always add/update the contact on radio before sending.
|
||||
# The library cache (get_contact_by_key_prefix) can be stale after radio reboot,
|
||||
# so we can't rely on it to know if the firmware has the contact.
|
||||
# add_contact is idempotent - updates if exists, adds if not.
|
||||
contact_data = db_contact.to_radio_dict()
|
||||
logger.debug("Ensuring contact %s is on radio before sending", db_contact.public_key[:12])
|
||||
add_result = await mc.commands.add_contact(contact_data)
|
||||
if add_result.type == EventType.ERROR:
|
||||
logger.warning("Failed to add contact to radio: %s", add_result.payload)
|
||||
# Continue anyway - might still work if contact exists
|
||||
|
||||
# Get the contact from the library cache (may have updated info like path)
|
||||
contact = mc.get_contact_by_key_prefix(db_contact.public_key[:12])
|
||||
if not contact:
|
||||
logger.info("Adding contact %s to radio before sending", db_contact.public_key[:12])
|
||||
contact_data = db_contact.to_radio_dict()
|
||||
add_result = await mc.commands.add_contact(contact_data)
|
||||
if add_result.type == EventType.ERROR:
|
||||
logger.warning("Failed to add contact to radio: %s", add_result.payload)
|
||||
# Continue anyway - might still work
|
||||
|
||||
# Get the contact from radio again
|
||||
contact = mc.get_contact_by_key_prefix(db_contact.public_key[:12])
|
||||
if not contact:
|
||||
# Use the contact_data we built as fallback
|
||||
contact = contact_data
|
||||
contact = contact_data
|
||||
|
||||
logger.info("Sending direct message to %s", db_contact.public_key[:12])
|
||||
|
||||
|
||||
@@ -129,6 +129,11 @@ class TestMessagesEndpoint:
|
||||
mock_mc = MagicMock()
|
||||
mock_mc.get_contact_by_key_prefix.return_value = {"public_key": "a" * 64}
|
||||
|
||||
mock_add_result = MagicMock()
|
||||
mock_add_result.type = MagicMock()
|
||||
mock_add_result.type.name = "OK"
|
||||
mock_mc.commands.add_contact = AsyncMock(return_value=mock_add_result)
|
||||
|
||||
mock_send_result = MagicMock()
|
||||
mock_send_result.type = MagicMock()
|
||||
mock_send_result.type.name = "OK"
|
||||
|
||||
Reference in New Issue
Block a user