Malte E 2 жил өмнө
parent
commit
51d71e98d3

+ 1 - 1
ROADMAP.md

@@ -10,7 +10,7 @@
       * [x] Audio files
       * [x] Files
       * [x] Gifs
-      * [ ] Locations
+      * [x] Locations
       * [ ] Stickers
   * [x] Message reactions
   * [x] Message redactions

+ 1 - 0
mautrix_signal/config.py

@@ -101,6 +101,7 @@ class Config(BaseBridgeConfig):
         copy("bridge.relay.enabled")
         copy_dict("bridge.relay.message_formats")
         copy("bridge.bridge_matrix_leave")
+        copy("bridge.location_format")
 
     def _get_permissions(self, key: str) -> Permissions:
         level = self["bridge.permissions"].get(key, "")

+ 5 - 0
mautrix_signal/example-config.yaml

@@ -297,6 +297,11 @@ bridge:
             m.video: '$sender_displayname sent a video'
             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'
+
 # Python logging configuration.
 #
 # See section 16.7.2 of the Python documentation for more info:

+ 24 - 0
mautrix_signal/portal.py

@@ -67,6 +67,7 @@ from mautrix.types import (
     EventType,
     FileInfo,
     ImageInfo,
+    LocationMessageEventContent,
     MediaMessageEventContent,
     Membership,
     MessageEvent,
@@ -453,6 +454,12 @@ class Portal(DBPortal, BasePortal):
             attachments = [attachment]
             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
         else:
             self.log.debug(f"Unknown msgtype {message.msgtype} in Matrix message {event_id}")
             return
@@ -560,6 +567,23 @@ 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: