Reformat and remove stale debugging

This commit is contained in:
2026-02-16 19:50:17 +00:00
parent 658ab10647
commit f3ced1bfd8
10 changed files with 122 additions and 105 deletions

View File

@@ -621,6 +621,7 @@ class SignalClient(ClientBase):
async def _drain_runtime_commands(self):
"""Process queued runtime commands (e.g., web UI sends via composite router)."""
from core.clients import transport
# Process a small burst each loop to keep sends responsive.
for _ in range(5):
command = transport.pop_runtime_command(self.service)
@@ -631,6 +632,7 @@ class SignalClient(ClientBase):
async def _execute_runtime_command(self, command):
"""Execute a single runtime command like send_message_raw."""
from core.clients import transport
command_id = str((command or {}).get("id") or "").strip()
action = str((command or {}).get("action") or "").strip()
payload = dict((command or {}).get("payload") or {})
@@ -672,7 +674,9 @@ class SignalClient(ClientBase):
return
if action == "notify_xmpp_sent":
person_identifier_id = str(payload.get("person_identifier_id") or "").strip()
person_identifier_id = str(
payload.get("person_identifier_id") or ""
).strip()
text = str(payload.get("text") or "")
if not person_identifier_id:
transport.set_runtime_command_result(
@@ -683,7 +687,9 @@ class SignalClient(ClientBase):
return
try:
identifier = await sync_to_async(
lambda: PersonIdentifier.objects.filter(id=person_identifier_id).select_related("user", "person").first()
lambda: PersonIdentifier.objects.filter(id=person_identifier_id)
.select_related("user", "person")
.first()
)()
if identifier is None:
transport.set_runtime_command_result(

View File

@@ -685,7 +685,7 @@ class WhatsAppClient(ClientBase):
payload = dict((command or {}).get("payload") or {})
if not command_id:
return
self.log.info(
self.log.debug(
"whatsapp runtime command start: id=%s action=%s",
command_id,
action,
@@ -718,7 +718,7 @@ class WhatsAppClient(ClientBase):
else int(time.time() * 1000),
},
)
self.log.info(
self.log.debug(
"whatsapp runtime command ok: id=%s action=%s",
command_id,
action,
@@ -800,7 +800,9 @@ class WhatsAppClient(ClientBase):
return
if action == "notify_xmpp_sent":
person_identifier_id = str(payload.get("person_identifier_id") or "").strip()
person_identifier_id = str(
payload.get("person_identifier_id") or ""
).strip()
text = str(payload.get("text") or "")
if not person_identifier_id:
transport.set_runtime_command_result(
@@ -811,7 +813,9 @@ class WhatsAppClient(ClientBase):
return
try:
identifier = await sync_to_async(
lambda: PersonIdentifier.objects.filter(id=person_identifier_id).select_related("user", "person").first()
lambda: PersonIdentifier.objects.filter(id=person_identifier_id)
.select_related("user", "person")
.first()
)()
if identifier is None:
transport.set_runtime_command_result(
@@ -1424,13 +1428,13 @@ class WhatsAppClient(ClientBase):
# Read contact-like rows directly from the session sqlite DB instead.
contacts, source, lid_map = await self._sync_contacts_from_sqlite()
if not contacts:
self.log.info("whatsapp contacts sync empty (%s)", source or "unknown")
self.log.debug("whatsapp contacts sync empty (%s)", source or "unknown")
self._publish_state(
last_event="contacts_sync_empty",
contacts_source=source or "unknown",
)
return
self.log.info(
self.log.debug(
"whatsapp contacts synced: count=%s source=%s",
len(contacts),
source or "unknown",
@@ -2600,7 +2604,11 @@ class WhatsAppClient(ClientBase):
)
if is_transient and attempt < 1:
# If runtime rejected string target, try to build protobuf JID for retry.
if jid_obj is None and self._build_jid is not None and "@" in jid_str:
if (
jid_obj is None
and self._build_jid is not None
and "@" in jid_str
):
local_part, server_part = jid_str.split("@", 1)
try:
maybe_retry_jid = self._build_jid(local_part, server_part)
@@ -2740,10 +2748,12 @@ class WhatsAppClient(ClientBase):
async def send_message_to_contact(self, contact_jid: str, text: str) -> bool:
"""Send a text message to a WhatsApp contact."""
try:
jid = build_jid(contact_jid.split("@")[0], contact_jid.split("@")[1])
jid = self._to_jid(contact_jid)
if not jid or self._client is None:
return False
# neonize.send_message() accepts either a Message protobuf or a plain string
# If passing a string, it auto-converts to Message(conversation=text)
response = self.client.send_message(jid, text)
response = self._client.send_message(jid, text)
return response is not None
except Exception as e:
self.log.error(f"Failed to send WhatsApp message: {e}")
@@ -2753,8 +2763,10 @@ class WhatsAppClient(ClientBase):
async def send_structured_message(self, contact_jid: str, message: Message) -> bool:
"""Send a structured Message protobuf to a WhatsApp contact."""
try:
jid = build_jid(contact_jid.split("@")[0], contact_jid.split("@")[1])
response = self.client.send_message(jid, message)
jid = self._to_jid(contact_jid)
if not jid or self._client is None:
return False
response = self._client.send_message(jid, message)
return response is not None
except Exception as e:
self.log.error(f"Failed to send structured WhatsApp message: {e}")

View File

@@ -155,15 +155,15 @@ class XMPPComponent(ComponentXMPP):
try:
# Lookup user in Django
self.log.info(f"User {sender_username}")
self.log.debug("Resolving XMPP sender user=%s", sender_username)
user = User.objects.get(username=sender_username)
# Find Person object with name=person_name.lower()
self.log.info(f"Name {person_name.title()}")
self.log.debug("Resolving XMPP recipient person=%s", person_name.title())
person = Person.objects.get(user=user, name=person_name.title())
# Ensure a PersonIdentifier exists for this user, person, and service
self.log.info(f"Identifier {service}")
self.log.debug("Resolving XMPP identifier service=%s", service)
identifier = PersonIdentifier.objects.get(
user=user, person=person, service=service
)
@@ -580,13 +580,13 @@ class XMPPComponent(ComponentXMPP):
iq["roster"]["items"] = {jid: {"name": name or jid}}
iq.send()
self.log.info(f"Updated roster: Added {jid} ({name})")
self.log.debug("Updated roster: added %s (%s)", jid, name)
def on_chatstate_active(self, msg):
"""
Handle when a user is actively engaged in the chat.
"""
self.log.info(f"Chat state: Active from {msg['from']}.")
self.log.debug("Chat state active from %s", msg["from"])
self.get_identifier(msg)
@@ -594,7 +594,7 @@ class XMPPComponent(ComponentXMPP):
"""
Handle when a user is typing a message.
"""
self.log.info(f"Chat state: Composing from {msg['from']}.")
self.log.debug("Chat state composing from %s", msg["from"])
identifier = self.get_identifier(msg)
if identifier:
@@ -609,7 +609,7 @@ class XMPPComponent(ComponentXMPP):
"""
Handle when a user has paused typing.
"""
self.log.info(f"Chat state: Paused from {msg['from']}.")
self.log.debug("Chat state paused from %s", msg["from"])
identifier = self.get_identifier(msg)
if identifier:
@@ -624,7 +624,7 @@ class XMPPComponent(ComponentXMPP):
"""
Handle when a user is inactive in the chat.
"""
self.log.info(f"Chat state: Inactive from {msg['from']}.")
self.log.debug("Chat state inactive from %s", msg["from"])
self.get_identifier(msg)
@@ -632,7 +632,7 @@ class XMPPComponent(ComponentXMPP):
"""
Handle when a user has left the chat.
"""
self.log.info(f"Chat state: Gone from {msg['from']}.")
self.log.debug("Chat state gone from %s", msg["from"])
self.get_identifier(msg)
@@ -640,37 +640,37 @@ class XMPPComponent(ComponentXMPP):
"""
Handle when a user becomes available.
"""
self.log.info(f"Presence available from {pres['from']}")
self.log.debug("Presence available from %s", pres["from"])
def on_presence_dnd(self, pres):
"""
Handle when a user sets 'Do Not Disturb' status.
"""
self.log.info(f"User {pres['from']} is now in 'Do Not Disturb' mode.")
self.log.debug("Presence dnd from %s", pres["from"])
def on_presence_xa(self, pres):
"""
Handle when a user sets 'Extended Away' status.
"""
self.log.info(f"User {pres['from']} is now 'Extended Away'.")
self.log.debug("Presence extended-away from %s", pres["from"])
def on_presence_chat(self, pres):
"""
Handle when a user is actively available for chat.
"""
self.log.info(f"User {pres['from']} is now available for chat.")
self.log.debug("Presence chat-available from %s", pres["from"])
def on_presence_away(self, pres):
"""
Handle when a user sets 'Away' status.
"""
self.log.info(f"User {pres['from']} is now 'Away'.")
self.log.debug("Presence away from %s", pres["from"])
def on_presence_unavailable(self, pres):
"""
Handle when a user goes offline or unavailable.
"""
self.log.info(f"User {pres['from']} is now unavailable.")
self.log.debug("Presence unavailable from %s", pres["from"])
def on_presence_subscribe(self, pres):
"""
@@ -681,7 +681,7 @@ class XMPPComponent(ComponentXMPP):
sender_jid = str(pres["from"]).split("/")[0] # Bare JID (user@domain)
recipient_jid = str(pres["to"]).split("/")[0]
self.log.info(
self.log.debug(
f"Received subscription request from {sender_jid} to {recipient_jid}"
)
@@ -699,36 +699,36 @@ class XMPPComponent(ComponentXMPP):
service = None
# Lookup user in Django
self.log.info(f"User {user_username}")
self.log.debug("Resolving subscription user=%s", user_username)
user = User.objects.get(username=user_username)
# Find Person object with name=person_name.lower()
self.log.info(f"Name {person_name.title()}")
self.log.debug("Resolving subscription person=%s", person_name.title())
person = Person.objects.get(user=user, name=person_name.title())
# Ensure a PersonIdentifier exists for this user, person, and service
self.log.info(f"Identifier {service}")
self.log.debug("Resolving subscription identifier service=%s", service)
PersonIdentifier.objects.get(user=user, person=person, service=service)
component_jid = f"{person_name.lower()}|{service}@{self.boundjid.bare}"
# Accept the subscription
self.send_presence(ptype="subscribed", pto=sender_jid, pfrom=component_jid)
self.log.info(
self.log.debug(
f"Accepted subscription from {sender_jid}, sent from {component_jid}"
)
# Send a presence request **from the recipient to the sender** (ASKS THEM TO ACCEPT BACK)
# self.send_presence(ptype="subscribe", pto=sender_jid, pfrom=component_jid)
# self.log.info(f"Sent presence subscription request from {component_jid} to {sender_jid}")
# Add sender to roster
# self.update_roster(sender_jid, name=sender_jid.split("@")[0])
# self.log.info(f"Added {sender_jid} to roster.")
# Send presence update to sender **from the correct JID**
self.send_presence(ptype="available", pto=sender_jid, pfrom=component_jid)
self.log.info(f"Sent presence update from {component_jid} to {sender_jid}")
self.log.debug(
"Sent presence update from %s to %s", component_jid, sender_jid
)
except (User.DoesNotExist, Person.DoesNotExist, PersonIdentifier.DoesNotExist):
# If any lookup fails, reject the subscription
@@ -744,25 +744,25 @@ class XMPPComponent(ComponentXMPP):
"""
Handle successful subscription confirmations.
"""
self.log.info(f"Subscription to {pres['from']} was accepted.")
self.log.debug("Subscription to %s accepted", pres["from"])
def on_presence_unsubscribe(self, pres):
"""
Handle when a user unsubscribes from presence updates.
"""
self.log.info(f"User {pres['from']} has unsubscribed from presence updates.")
self.log.debug("Presence unsubscribe from %s", pres["from"])
def on_presence_unsubscribed(self, pres):
"""
Handle when a user's unsubscription request is confirmed.
"""
self.log.info(f"Unsubscription from {pres['from']} confirmed.")
self.log.debug("Presence unsubscribed confirmation from %s", pres["from"])
def on_roster_subscription_request(self, pres):
"""
Handle roster subscription requests.
"""
self.log.info(f"New roster subscription request from {pres['from']}.")
self.log.debug("Roster subscription request from %s", pres["from"])
async def session_start(self, *args):
self.log.info("XMPP session started")
@@ -793,8 +793,6 @@ class XMPPComponent(ComponentXMPP):
# self.log.error("No XEP-0363 upload service found.")
# return None
# self.log.info(f"Upload service: {upload_service}")
upload_service_jid = str(
getattr(settings, "XMPP_UPLOAD_SERVICE", "")
or getattr(settings, "XMPP_UPLOAD_JID", "")
@@ -851,8 +849,6 @@ class XMPPComponent(ComponentXMPP):
def sym(value):
msg.reply(f"[>] {value}").send()
# self.log.info(f"Received message: {msg}")
# Extract sender JID (full format: user@domain/resource)
sender_jid = str(msg["from"])
@@ -878,7 +874,9 @@ class XMPPComponent(ComponentXMPP):
body = msg["body"] if msg["body"] else ""
attachments = []
self.log.info(f"Full XMPP Message: {ET.tostring(msg.xml, encoding='unicode')}")
self.log.debug(
"Received XMPP stanza: %s", ET.tostring(msg.xml, encoding="unicode")
)
# Extract attachments from standard XMPP payloads.
for att in msg.xml.findall(".//{urn:xmpp:attachments}attachment"):
@@ -933,7 +931,7 @@ class XMPPComponent(ComponentXMPP):
if attachment_urls:
body = "\n".join(attachment_urls)
self.log.info(f"Extracted {len(attachments)} attachments from XMPP message.")
self.log.debug("Extracted %s attachments from XMPP message", len(attachments))
# Log extracted information with variable name annotations
log_message = (
f"Sender JID: {sender_jid}, Sender Username: {sender_username}, Sender Domain: {sender_domain}, "
@@ -941,7 +939,7 @@ class XMPPComponent(ComponentXMPP):
f"Recipient JID: {recipient_jid}, Recipient Username: {recipient_username}, Recipient Domain: {recipient_domain}, "
f"Body: {body or '[No Body]'}"
)
self.log.info(log_message)
self.log.debug(log_message)
# Ensure recipient domain matches our configured component
expected_domain = settings.XMPP_JID # 'jews.zm.is' in your config
@@ -959,14 +957,14 @@ class XMPPComponent(ComponentXMPP):
return
if recipient_jid == settings.XMPP_JID:
self.log.info("Message to JID")
self.log.debug("Handling command message sent to gateway JID")
if body.startswith("."):
# Messaging the gateway directly
if body == ".contacts":
# Lookup Person objects linked to sender
persons = Person.objects.filter(user=sender_user)
if not persons.exists():
self.log.info(f"No contacts found for {sender_username}")
self.log.debug("No contacts found for %s", sender_username)
sym("No contacts found.")
return
@@ -989,7 +987,7 @@ class XMPPComponent(ComponentXMPP):
else:
sym("No such command")
else:
self.log.info("Other message")
self.log.debug("Handling routed message to contact")
if "|" in recipient_username:
recipient_name, recipient_service = recipient_username.split("|")
@@ -1029,7 +1027,7 @@ class XMPPComponent(ComponentXMPP):
identifier=identifier,
user=identifier.user,
)
self.log.info(f"Component history store message {body}")
self.log.debug("Storing outbound XMPP message in history")
await history.store_message(
session=session,
sender="XMPP",
@@ -1037,7 +1035,7 @@ class XMPPComponent(ComponentXMPP):
ts=int(now().timestamp() * 1000),
outgoing=True,
)
self.log.info("Stored a message sent from XMPP in the history.")
self.log.debug("Stored outbound XMPP message in history")
manipulations = Manipulation.objects.filter(
group__people=identifier.person,
@@ -1045,7 +1043,7 @@ class XMPPComponent(ComponentXMPP):
mode="mutate",
enabled=True,
)
self.log.info(f"MANIP11 {manipulations}")
self.log.debug("Found %s active manipulations", manipulations.count())
if not manipulations:
await self.ur.stopped_typing(
"xmpp",
@@ -1056,7 +1054,7 @@ class XMPPComponent(ComponentXMPP):
body,
attachments,
)
self.log.info("Message sent unaltered")
self.log.debug("Message sent unaltered")
return
manip = manipulations.first()
@@ -1068,9 +1066,9 @@ class XMPPComponent(ComponentXMPP):
manip,
chat_history,
)
self.log.info("Running XMPP context prompt")
self.log.debug("Running XMPP context prompt")
result = await ai.run_prompt(prompt, manip.ai)
self.log.info(f"RESULT {result}")
self.log.debug("Generated mutated response for XMPP message")
await history.store_own_message(
session=session,
text=result,
@@ -1085,7 +1083,7 @@ class XMPPComponent(ComponentXMPP):
result,
attachments,
)
self.log.info("Message sent with modifications")
self.log.debug("Message sent with modifications")
async def request_upload_slots(self, recipient_jid, attachments):
"""Requests upload slots for multiple attachments concurrently."""
@@ -1120,8 +1118,8 @@ class XMPPComponent(ComponentXMPP):
f"Upload failed: {response.status} {await response.text()}"
)
return None
self.log.info(
f"Successfully uploaded {att['filename']} to {upload_url}"
self.log.debug(
"Successfully uploaded %s to %s", att["filename"], upload_url
)
# Send XMPP message immediately after successful upload
@@ -1148,7 +1146,7 @@ class XMPPComponent(ComponentXMPP):
url_element.text = attachment_url
msg.xml.append(oob_element)
self.log.info(f"Sending XMPP message: {msg.xml}")
self.log.debug("Sending XMPP message: %s", msg.xml)
msg.send()
async def send_chat_state(self, recipient_jid, sender_jid, started):
@@ -1158,7 +1156,7 @@ class XMPPComponent(ComponentXMPP):
msg.xml.append(
ET.Element(f"{{http://jabber.org/protocol/chatstates}}{state_tag}")
)
self.log.info(
self.log.debug(
"Sending XMPP chat-state %s: %s -> %s",
state_tag,
sender_jid,
@@ -1193,7 +1191,7 @@ class XMPPComponent(ComponentXMPP):
# Step 2: Request upload slots concurrently
valid_uploads = await self.request_upload_slots(recipient_jid, attachments)
self.log.info("Got upload slots")
self.log.debug("Got upload slots")
if not valid_uploads:
self.log.warning("No valid upload slots obtained.")
return []