소스 검색

Merge remote-tracking branch 'maltee1/matrix_location'

Tulir Asokan 2 년 전
부모
커밋
7ca3ce8e33
4개의 변경된 파일31개의 추가작업 그리고 1개의 파일을 삭제
  1. 1 1
      ROADMAP.md
  2. 1 0
      mautrix_signal/config.py
  3. 5 0
      mautrix_signal/example-config.yaml
  4. 24 0
      mautrix_signal/portal.py

+ 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

@@ -102,6 +102,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

@@ -300,6 +300,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

@@ -69,6 +69,7 @@ from mautrix.types import (
     FileInfo,
     ImageInfo,
     JoinRule,
+    LocationMessageEventContent,
     MediaMessageEventContent,
     Membership,
     MessageEvent,
@@ -455,6 +456,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
@@ -562,6 +569,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: