Fix issues with multiplatform conversation displays

This commit is contained in:
2026-02-16 11:42:53 +00:00
parent cf651a3bd4
commit a70faa3576
10 changed files with 489 additions and 78 deletions

View File

@@ -1,6 +1,5 @@
import asyncio
import json
import os
from urllib.parse import quote_plus, urlparse
import aiohttp
@@ -25,10 +24,7 @@ if _signal_http_url:
SIGNAL_HOST = parsed.hostname or "signal"
SIGNAL_PORT = parsed.port or 8080
else:
if settings.DEBUG:
SIGNAL_HOST = "127.0.0.1"
else:
SIGNAL_HOST = "signal"
SIGNAL_HOST = "signal"
SIGNAL_PORT = 8080
SIGNAL_URL = f"{SIGNAL_HOST}:{SIGNAL_PORT}"
@@ -241,12 +237,10 @@ class HandleMessage(Command):
"raw_message": c.message.raw_message,
}
raw = json.loads(c.message.raw_message)
dest = (
raw.get("envelope", {})
.get("syncMessage", {})
.get("sentMessage", {})
.get("destinationUuid")
sent_message = (
raw.get("envelope", {}).get("syncMessage", {}).get("sentMessage", {}) or {}
)
dest = sent_message.get("destinationUuid")
account = raw.get("account", "")
source_name = raw.get("envelope", {}).get("sourceName", "")
@@ -271,25 +265,22 @@ class HandleMessage(Command):
envelope_source_uuid = envelope.get("sourceUuid")
envelope_source_number = envelope.get("sourceNumber")
envelope_source = envelope.get("source")
destination_number = (
raw.get("envelope", {})
.get("syncMessage", {})
.get("sentMessage", {})
.get("destination")
)
destination_number = sent_message.get("destination")
primary_identifier = dest if is_from_bot else source_uuid
if is_from_bot:
# Outbound events must route only by destination identity.
# Including the bot's own UUID/number leaks messages across people
# if "self" identifiers are linked anywhere.
identifier_candidates = _identifier_candidates(
dest,
destination_number,
primary_identifier,
)
if dest or destination_number:
# Sync "sentMessage" events are outbound; route by destination only.
# This prevents copying one outbound message into multiple people
# when source fields include the bot's own identifier.
identifier_candidates = _identifier_candidates(dest, destination_number)
elif is_from_bot:
identifier_candidates = _identifier_candidates(primary_identifier)
else:
identifier_candidates = _identifier_candidates(
bot_identifiers = {
str(c.bot.bot_uuid or "").strip(),
str(getattr(c.bot, "phone_number", "") or "").strip(),
}
incoming_candidates = _identifier_candidates(
primary_identifier,
source_uuid,
source_number,
@@ -297,8 +288,12 @@ class HandleMessage(Command):
envelope_source_uuid,
envelope_source_number,
envelope_source,
dest,
)
identifier_candidates = [
value
for value in incoming_candidates
if value and value not in bot_identifiers
]
if not identifier_candidates:
log.warning("No Signal identifier available for message routing.")
return
@@ -598,12 +593,13 @@ class HandleMessage(Command):
class SignalClient(ClientBase):
def __init__(self, ur, *args, **kwargs):
super().__init__(ur, *args, **kwargs)
signal_number = str(getattr(settings, "SIGNAL_NUMBER", "")).strip()
self.client = NewSignalBot(
ur,
self.service,
{
"signal_service": SIGNAL_URL,
"phone_number": "+447700900000",
"phone_number": signal_number,
},
)

View File

@@ -2211,16 +2211,18 @@ class WhatsAppClient(ClientBase):
raw = str(recipient or "").strip()
if not raw:
return ""
if "@" in raw:
return raw
digits = re.sub(r"[^0-9]", "", raw)
if digits:
# Prefer direct JID formatting for phone numbers; Neonize build_jid
# can trigger a usync lookup path that intermittently times out.
return f"{digits}@s.whatsapp.net"
if self._build_jid is not None:
try:
return self._build_jid(raw)
except Exception:
pass
if "@" in raw:
return raw
digits = re.sub(r"[^0-9]", "", raw)
if digits:
return f"{digits}@s.whatsapp.net"
return raw
def _blob_key_to_compose_url(self, blob_key):
@@ -2275,6 +2277,23 @@ class WhatsAppClient(ClientBase):
jid = self._to_jid(recipient)
if not jid:
return False
if not self._connected and hasattr(self._client, "connect"):
try:
await self._maybe_await(self._client.connect())
self._connected = True
self._publish_state(
connected=True,
last_event="send_reconnect_ok",
warning="",
last_error="",
)
except Exception as exc:
self._publish_state(
connected=False,
last_event="send_reconnect_failed",
last_error=str(exc),
warning=f"WhatsApp reconnect before send failed: {exc}",
)
sent_any = False
sent_ts = 0
@@ -2318,16 +2337,50 @@ class WhatsAppClient(ClientBase):
self.log.warning("whatsapp attachment send failed: %s", exc)
if text:
try:
response = await self._maybe_await(self._client.send_message(jid, text))
sent_any = True
except TypeError:
response = await self._maybe_await(
self._client.send_message(jid, message=text)
)
sent_any = True
except Exception as exc:
self.log.warning("whatsapp text send failed: %s", exc)
response = None
last_error = None
for attempt in range(3):
try:
response = await self._maybe_await(self._client.send_message(jid, text))
sent_any = True
last_error = None
break
except TypeError:
try:
response = await self._maybe_await(
self._client.send_message(jid, message=text)
)
sent_any = True
last_error = None
break
except Exception as exc:
last_error = exc
except Exception as exc:
last_error = exc
error_text = str(last_error or "").lower()
is_transient = "usync query" in error_text or "timed out" in error_text
if is_transient and attempt < 2:
if hasattr(self._client, "connect"):
try:
await self._maybe_await(self._client.connect())
self._connected = True
self._publish_state(
connected=True,
last_event="send_retry_reconnect_ok",
warning="",
)
except Exception as reconnect_exc:
self._publish_state(
connected=False,
last_event="send_retry_reconnect_failed",
last_error=str(reconnect_exc),
)
await asyncio.sleep(0.8 * (attempt + 1))
continue
break
if last_error is not None and not sent_any:
self.log.warning("whatsapp text send failed: %s", last_error)
return False
sent_ts = max(
sent_ts,