Quellcode durchsuchen

Improve location message bridging

Tulir Asokan vor 2 Jahren
Ursprung
Commit
2f569e1b7a
2 geänderte Dateien mit 27 neuen und 25 gelöschten Zeilen
  1. 3 3
      mautrix_signal/example-config.yaml
  2. 24 22
      mautrix_signal/portal.py

+ 3 - 3
mautrix_signal/example-config.yaml

@@ -301,9 +301,9 @@ bridge:
             m.location: '$sender_displayname sent a location'
 
     # Format for generting URLs from location messages for sending to Signal
-    # Google Maps: 'https://www.google.com/maps/place/$lat,$long'
-    # OpenStreepMap: 'https://www.openstreetmap.org/?mlat=$lat&mlon=$long'
-    location_format: 'https://www.google.com/maps/place/$lat,$long'
+    # Google Maps: 'https://www.google.com/maps/place/{lat},{long}'
+    # OpenStreepMap: 'https://www.openstreetmap.org/?mlat={lat}&mlon={long}'
+    location_format: 'https://www.google.com/maps/place/{lat},{long}'
 
 # Python logging configuration.
 #

+ 24 - 22
mautrix_signal/portal.py

@@ -457,11 +457,30 @@ class Portal(DBPortal, BasePortal):
             text = message.body if is_relay else None
             self.log.trace("Formed outgoing attachment %s", attachment)
         elif message.msgtype == MessageType.LOCATION:
-            url = self._matrix_location_to_link(message)
-            body = message.body.replace(message.geo_uri, "")
-            sep = "\n" if len(body) > 0 else ""
-            if url:
-                text = body + sep + url
+            try:
+                lat, long = message.geo_uri[len("geo:") :].split(";")[0].split(",")
+                text = self.config["bridge.location_format"].format(
+                    lat=float(lat), long=float(long)
+                )
+            except (ValueError, KeyError, IndexError) as e:
+                orig_sender.send_remote_checkpoint(
+                    status=MessageSendCheckpointStatus.PERM_FAILURE,
+                    event_id=event_id,
+                    room_id=self.mxid,
+                    event_type=EventType.ROOM_MESSAGE,
+                    message_type=message.msgtype,
+                    error=str(e),
+                )
+                self.log.warning(f"Malformed geo URI in {event_id}: {e}")
+                return
+            extev = message.get("org.matrix.msc3488.location", None)
+            # TODO support relay mode with extensible event location descriptions
+            if extev and not is_relay:
+                body = extev.get("description")
+            else:
+                body = message.body
+            if body:
+                text = f"{body}\n{text}"
         else:
             self.log.debug(f"Unknown msgtype {message.msgtype} in Matrix message {event_id}")
             return
@@ -569,23 +588,6 @@ class Portal(DBPortal, BasePortal):
         }.get(event_type, str(event_type))
         raise last_error_type(f"Failed to send {event_type_name} after {retry_count} retries.")
 
-    def _matrix_location_to_link(
-        self,
-        content: LocationMessageEventContent,
-    ) -> str:
-        try:
-            lat, long = content.geo_uri[len("geo:") :].split(";")[0].split(",")
-        except (KeyError, ValueError):
-            self.log.exception("Failed to parse location")
-            return None
-        try:
-            float(lat)
-            float(long)
-        except Exception:
-            self.log.debug("Error: Invalid location " + content.geo_uri)
-            return None
-        return self.config["bridge.location_format"].replace("$lat", lat).replace("$long", long)
-
     async def handle_matrix_reaction(
         self, sender: u.User, event_id: EventID, reacting_to: EventID, emoji: str
     ) -> None: