Increase platform abstraction cohesion

This commit is contained in:
2026-03-06 17:47:58 +00:00
parent 438e561da0
commit 8c091b1e6d
55 changed files with 6555 additions and 440 deletions

View File

@@ -193,6 +193,85 @@ def _extract_signal_reaction(envelope):
}
def _extract_signal_edit(envelope):
paths = [
("dataMessage", "editMessage"),
("syncMessage", "sentMessage", "editMessage"),
("syncMessage", "editMessage"),
]
node = None
for path in paths:
candidate = _get_nested(envelope, path)
if isinstance(candidate, dict):
node = candidate
break
if not isinstance(node, dict):
return None
target_ts = node.get("targetSentTimestamp")
if target_ts is None:
target_ts = node.get("targetTimestamp")
if target_ts is None:
target_ts = node.get("targetTs")
try:
target_ts = int(target_ts)
except Exception:
target_ts = 0
if target_ts <= 0:
return None
data_message = node.get("dataMessage") or node.get("message") or {}
new_text = ""
if isinstance(data_message, dict):
for key in ("message", "text", "body", "caption"):
value = str(data_message.get(key) or "").strip()
if value:
new_text = value
break
if not new_text:
new_text = str(node.get("message") or "").strip()
if not new_text:
return None
return {
"target_ts": target_ts,
"new_text": new_text,
"raw": dict(node),
}
def _extract_signal_delete(envelope):
paths = [
("dataMessage", "delete"),
("dataMessage", "remoteDelete"),
("syncMessage", "sentMessage", "delete"),
("syncMessage", "delete"),
]
node = None
for path in paths:
candidate = _get_nested(envelope, path)
if isinstance(candidate, dict):
node = candidate
break
if not isinstance(node, dict):
return None
target_ts = node.get("targetSentTimestamp")
if target_ts is None:
target_ts = node.get("targetTimestamp")
if target_ts is None:
target_ts = node.get("targetTs")
try:
target_ts = int(target_ts)
except Exception:
target_ts = 0
if target_ts <= 0:
return None
return {
"target_ts": target_ts,
"raw": dict(node),
}
def _extract_signal_text(raw_payload, default_text=""):
text = str(default_text or "").strip()
if text:
@@ -1299,6 +1378,8 @@ class SignalClient(ClientBase):
destination_number,
)
reaction_payload = _extract_signal_reaction(envelope)
edit_payload = _extract_signal_edit(envelope)
delete_payload = _extract_signal_delete(envelope)
if identifiers and isinstance(reaction_payload, dict):
source_uuid = str(
envelope.get("sourceUuid") or envelope.get("source") or ""
@@ -1343,6 +1424,61 @@ class SignalClient(ClientBase):
self.log.warning(
"signal raw sync reaction relay to XMPP failed: %s", exc
)
if identifiers and isinstance(edit_payload, dict):
source_uuid = str(
envelope.get("sourceUuid") or envelope.get("source") or ""
).strip()
source_number = str(envelope.get("sourceNumber") or "").strip()
for identifier in identifiers:
try:
await history.apply_message_edit(
identifier.user,
identifier,
target_message_id="",
target_ts=int(edit_payload.get("target_ts") or 0),
new_text=str(edit_payload.get("new_text") or ""),
source_service="signal",
actor=(source_uuid or source_number or ""),
payload=edit_payload.get("raw") or {},
)
except Exception as exc:
self.log.warning(
"signal raw sync edit history apply failed: %s", exc
)
transport.update_runtime_state(
self.service,
last_inbound_ok_ts=int(time.time() * 1000),
last_inbound_exception_type="",
last_inbound_exception_message="",
)
return
if identifiers and isinstance(delete_payload, dict):
source_uuid = str(
envelope.get("sourceUuid") or envelope.get("source") or ""
).strip()
source_number = str(envelope.get("sourceNumber") or "").strip()
for identifier in identifiers:
try:
await history.apply_message_delete(
identifier.user,
identifier,
target_message_id="",
target_ts=int(delete_payload.get("target_ts") or 0),
source_service="signal",
actor=(source_uuid or source_number or ""),
payload=delete_payload.get("raw") or {},
)
except Exception as exc:
self.log.warning(
"signal raw sync delete history apply failed: %s", exc
)
transport.update_runtime_state(
self.service,
last_inbound_ok_ts=int(time.time() * 1000),
last_inbound_exception_type="",
last_inbound_exception_message="",
)
return
if identifiers and text:
ts_raw = (
sync_sent_message.get("timestamp")
@@ -1427,8 +1563,14 @@ class SignalClient(ClientBase):
identifiers = await self._resolve_signal_identifiers(source_uuid, source_number)
reaction_payload = _extract_signal_reaction(envelope)
if (not identifiers) and isinstance(reaction_payload, dict):
# Sync reactions from our own linked device can arrive with source=our
edit_payload = _extract_signal_edit(envelope)
delete_payload = _extract_signal_delete(envelope)
if (not identifiers) and (
isinstance(reaction_payload, dict)
or isinstance(edit_payload, dict)
or isinstance(delete_payload, dict)
):
# Sync events from our own linked device can arrive with source=our
# account and destination=<contact>. Resolve by destination as fallback.
destination_uuid = str(
envelope.get("destinationServiceId")
@@ -1497,6 +1639,49 @@ class SignalClient(ClientBase):
last_inbound_exception_message="",
)
return
if isinstance(edit_payload, dict):
for identifier in identifiers:
try:
await history.apply_message_edit(
identifier.user,
identifier,
target_message_id="",
target_ts=int(edit_payload.get("target_ts") or 0),
new_text=str(edit_payload.get("new_text") or ""),
source_service="signal",
actor=(source_uuid or source_number or ""),
payload=edit_payload.get("raw") or {},
)
except Exception as exc:
self.log.warning("signal raw edit history apply failed: %s", exc)
transport.update_runtime_state(
self.service,
last_inbound_ok_ts=int(time.time() * 1000),
last_inbound_exception_type="",
last_inbound_exception_message="",
)
return
if isinstance(delete_payload, dict):
for identifier in identifiers:
try:
await history.apply_message_delete(
identifier.user,
identifier,
target_message_id="",
target_ts=int(delete_payload.get("target_ts") or 0),
source_service="signal",
actor=(source_uuid or source_number or ""),
payload=delete_payload.get("raw") or {},
)
except Exception as exc:
self.log.warning("signal raw delete history apply failed: %s", exc)
transport.update_runtime_state(
self.service,
last_inbound_ok_ts=int(time.time() * 1000),
last_inbound_exception_type="",
last_inbound_exception_message="",
)
return
text = _extract_signal_text(payload, str(data_message.get("message") or "").strip())
if not text: