gateway: accept v7 contact requests using contactRequestId

The v7 receivedContactRequest event exposes contactRequestId (and contactId_,
the underscore being the generated-API optional marker); /_accept takes the
request id. The adapter looked for contactRequest.contactId, found nothing, and
silently no-oped — so contact requests hung unaccepted. Prefer contactRequestId,
fall back to contactId for legacy events, and trace the accepted ids. Tests for
both shapes and the disabled case.
This commit is contained in:
Denton Social committed 2026-09-26 18:52:58 -05:00
1 parent c92aff5038
commit 221edb827e
2 files changed
+53 -4

No files matched your search

+15 -4
View File
@@ -186,14 +186,25 @@ class SimplexAdapter(GatewayAdapter):
async def _maybe_accept(self, ws, resp: dict) -> None: async def _maybe_accept(self, ws, resp: dict) -> None:
if not self.auto_accept: if not self.auto_accept:
return return
contact_id = (resp.get("contactRequest") or {}).get("contactId") req = resp.get("contactRequest") or {}
if contact_id is None: # v7 names the field contactRequestId_ (the trailing underscore is the
# generated-API optional marker) and /_accept takes the *request* id;
# older events exposed contactId. Fall back so both shapes work.
req_id = req.get("contactRequestId")
if req_id is None:
req_id = req.get("contactId_") or req.get("contactId")
if req_id is None:
return return
self._corr += 1 self._corr += 1
await ws.send( await ws.send(
json.dumps({"corrId": f"sf-{self._corr}", "cmd": f"/_accept {contact_id}"}) json.dumps({"corrId": f"sf-{self._corr}", "cmd": f"/_accept {req_id}"})
)
contact_id = req.get("contactId_") or req.get("contactId")
self._event(
"gateway_accepted",
contact_request_id=str(req_id),
contact_id=str(contact_id) if contact_id is not None else "",
) )
self._event("gateway_accepted", contact_id=str(contact_id))
def _schedule_flush(self, message: InboundMessage, on_inbound) -> None: def _schedule_flush(self, message: InboundMessage, on_inbound) -> None:
chat = message.chat_id chat = message.chat_id
+38
View File
@@ -115,6 +115,44 @@ def test_send_command_is_structured_not_shortcut():
assert command != "/_send @4 hello" assert command != "/_send @4 hello"
# ---- adapter: contact request acceptance ----
class _FakeWS:
def __init__(self):
self.sent = []
async def send(self, data):
self.sent.append(json.loads(data)["cmd"])
def _contact_request_event(req):
return json.dumps(
{"resp": {"type": "receivedContactRequest", "contactRequest": req}}
)
def test_accept_uses_v7_contact_request_id():
# v7 exposes contactRequestId (and contactId_); /_accept takes the request id.
adapter = SimplexAdapter({"auto_accept": True})
ws = _FakeWS()
asyncio.run(adapter._consume(_contact_request_event({"contactRequestId": 7, "contactId_": 9}), lambda m: None, ws))
assert ws.sent == ["/_accept 7"]
def test_accept_falls_back_to_contact_id_for_legacy_events():
adapter = SimplexAdapter({"auto_accept": True})
ws = _FakeWS()
asyncio.run(adapter._consume(_contact_request_event({"contactId": 4}), lambda m: None, ws))
assert ws.sent == ["/_accept 4"]
def test_accept_disabled_sends_nothing():
adapter = SimplexAdapter({"auto_accept": False})
ws = _FakeWS()
asyncio.run(adapter._consume(_contact_request_event({"contactRequestId": 7, "contactId_": 9}), lambda m: None, ws))
assert ws.sent == []
# ---- adapter: batching ---- # ---- adapter: batching ----
def test_batching_concatenates_rapid_messages(): def test_batching_concatenates_rapid_messages():