Fix Signal compose live updates and self-chat direction

This commit is contained in:
2026-02-22 11:03:08 +00:00
parent 65cd647f01
commit 0f36b2dde7
4 changed files with 124 additions and 13 deletions

View File

@@ -1,5 +1,6 @@
import asyncio
import json
import re
import time
from urllib.parse import quote_plus, urlparse
@@ -198,10 +199,18 @@ def _identifier_candidates(*values):
seen = set()
for value in values:
cleaned = str(value or "").strip()
if not cleaned or cleaned in seen:
if not cleaned:
continue
seen.add(cleaned)
out.append(cleaned)
candidates = [cleaned]
digits = re.sub(r"[^0-9]", "", cleaned)
# Add basic E.164 variants for phone-shaped values.
if digits and cleaned.count("-") < 4:
candidates.extend([digits, f"+{digits}"])
for candidate in candidates:
if not candidate or candidate in seen:
continue
seen.add(candidate)
out.append(candidate)
return out
@@ -349,12 +358,32 @@ class HandleMessage(Command):
ts = c.message.timestamp
source_value = c.message.source
envelope = raw.get("envelope", {})
destination_number = sent_message.get("destination")
bot_uuid = str(getattr(c.bot, "bot_uuid", "") or "").strip()
bot_phone = str(getattr(c.bot, "phone_number", "") or "").strip()
source_uuid_norm = str(source_uuid or "").strip()
source_number_norm = str(source_number or "").strip()
dest_norm = str(dest or "").strip()
destination_number_norm = str(destination_number or "").strip()
bot_phone_digits = re.sub(r"[^0-9]", "", bot_phone)
source_phone_digits = re.sub(r"[^0-9]", "", source_number_norm)
dest_phone_digits = re.sub(r"[^0-9]", "", destination_number_norm or dest_norm)
# Message originating from us
same_recipient = source_uuid == dest
is_from_bot = source_uuid == c.bot.bot_uuid
is_to_bot = dest == c.bot.bot_uuid or dest is None
is_from_bot = bool(bot_uuid and source_uuid_norm and source_uuid_norm == bot_uuid)
if (not is_from_bot) and bot_phone_digits and source_phone_digits:
is_from_bot = source_phone_digits == bot_phone_digits
# For non-sync incoming events destination is usually absent and points to us.
is_to_bot = bool(bot_uuid and dest_norm and dest_norm == bot_uuid)
if (not is_to_bot) and bot_phone_digits and dest_phone_digits:
is_to_bot = dest_phone_digits == bot_phone_digits
if (not is_to_bot) and (not dest_norm) and (not destination_number_norm):
is_to_bot = True
reply_to_self = same_recipient and is_from_bot # Reply
reply_to_others = is_to_bot and not same_recipient # Reply
@@ -363,7 +392,6 @@ class HandleMessage(Command):
envelope_source_uuid = envelope.get("sourceUuid")
envelope_source_number = envelope.get("sourceNumber")
envelope_source = envelope.get("source")
destination_number = sent_message.get("destination")
primary_identifier = dest if is_from_bot else source_uuid
if dest or destination_number:
@@ -450,6 +478,20 @@ class HandleMessage(Command):
len(identifiers),
)
for identifier in identifiers:
try:
await history.apply_reaction(
identifier.user,
identifier,
target_message_id="",
target_ts=int(reaction_payload.get("target_ts") or 0),
emoji=str(reaction_payload.get("emoji") or ""),
source_service="signal",
actor=(source_uuid or source_number or ""),
remove=bool(reaction_payload.get("remove")),
payload=reaction_payload.get("raw") or {},
)
except Exception as exc:
log.warning("Signal reaction history apply failed: %s", exc)
try:
await self.ur.xmpp.client.apply_external_reaction(
identifier.user,