浏览代码

send: add m.notice when send to Signal fails

Sumner Evans 3 年之前
父节点
当前提交
3f1d87b1e7
共有 2 个文件被更改,包括 28 次插入8 次删除
  1. 17 5
      mausignald/signald.py
  2. 11 3
      mautrix_signal/portal.py

+ 17 - 5
mausignald/signald.py

@@ -144,11 +144,23 @@ class SignaldClient(SignaldRPCClient):
         serialized_quote = quote.serialize() if quote else None
         serialized_attachments = [attachment.serialize() for attachment in (attachments or [])]
         serialized_mentions = [mention.serialize() for mention in (mentions or [])]
-        await self.request_v1("send", username=username, messageBody=body,
-                              attachments=serialized_attachments, quote=serialized_quote,
-                              mentions=serialized_mentions, timestamp=timestamp,
-                              **self._recipient_to_args(recipient))
-        # TODO return something?
+        resp = await self.request_v1("send", username=username, messageBody=body,
+                                     attachments=serialized_attachments, quote=serialized_quote,
+                                     mentions=serialized_mentions, timestamp=timestamp,
+                                     **self._recipient_to_args(recipient))
+        errors = []
+        for result in resp.get("results", []):
+            number = result.get("address", {}).get("number")
+            if result.get("networkFailure", False):
+                errors.append(f"Network failure occurred while sending message to {number}.")
+            if result.get("unregisteredFailure", False):
+                errors.append(f"Unregistered failure occurred while sending message to {number}.")
+            if result.get("identityFailure", ""):
+                errors.append(
+                    f"Identity failure occurred while sending message to {number}. New identity: "
+                    f"{result['identityFailure']}")
+        if errors:
+            raise Exception("\n".join(errors))
 
     async def send_receipt(self, username: str, sender: Address, timestamps: List[int],
                            when: Optional[int] = None, read: bool = False) -> None:

+ 11 - 3
mautrix_signal/portal.py

@@ -286,9 +286,17 @@ class Portal(DBPortal, BasePortal):
             self.log.debug(f"Unknown msgtype {message.msgtype} in Matrix message {event_id}")
             return
         self.log.debug(f"Sending Matrix message {event_id} to Signal with timestamp {request_id}")
-        await self.signal.send(username=sender.username, recipient=self.chat_id, body=text,
-                               mentions=mentions, quote=quote, attachments=attachments,
-                               timestamp=request_id)
+        try:
+            await self.signal.send(username=sender.username, recipient=self.chat_id, body=text,
+                                   mentions=mentions, quote=quote, attachments=attachments,
+                                   timestamp=request_id)
+        except Exception as e:
+            await self._send_message(
+                self.main_intent,
+                TextMessageEventContent(
+                    msgtype=MessageType.NOTICE,
+                    body=f"\u26a0 Your message was not bridged: {e}"))
+            return
         msg = DBMessage(mxid=event_id, mx_room=self.mxid, sender=sender.address,
                         timestamp=request_id,
                         signal_chat_id=self.chat_id, signal_receiver=self.receiver)