Parcourir la source

puppet: add contact info to all member events

Signed-off-by: Sumner Evans <sumner@beeper.com>
Sumner Evans il y a 2 ans
Parent
commit
608145e597
2 fichiers modifiés avec 36 ajouts et 0 suppressions
  1. 2 0
      mautrix_signal/__main__.py
  2. 34 0
      mautrix_signal/puppet.py

+ 2 - 0
mautrix_signal/__main__.py

@@ -38,6 +38,8 @@ SYNC_JITTER = 10
 class SignalBridge(Bridge):
     module = "mautrix_signal"
     name = "mautrix-signal"
+    beeper_service_name = "signal"
+    beeper_network_name = "signal"
     command = "python -m mautrix-signal"
     description = "A Matrix-Signal puppeting bridge."
     repo_url = "https://github.com/mautrix/signal"

+ 34 - 0
mautrix_signal/puppet.py

@@ -59,6 +59,7 @@ class Puppet(DBPuppet, BasePuppet):
     hs_domain: str
     mxid_template: SimpleTemplate[str]
 
+    bridge: SignalBridge
     config: Config
     signal: signal.SignalHandler
 
@@ -114,6 +115,7 @@ class Puppet(DBPuppet, BasePuppet):
 
     @classmethod
     def init_cls(cls, bridge: "SignalBridge") -> AsyncIterable[Awaitable[None]]:
+        cls.bridge = bridge
         cls.config = bridge.config
         cls.loop = bridge.loop
         cls.signal = bridge.signal
@@ -193,11 +195,16 @@ class Puppet(DBPuppet, BasePuppet):
     async def update_info(self, info: Profile | Address, source: u.User) -> None:
         update = False
         address = info.address if isinstance(info, Profile) else info
+        number_updated = False
         if address.number and address.number != self.number:
             await self.handle_number_receive(address.number)
             update = True
+            number_updated = True
+
         self.log.debug("Updating info with %s (source: %s)", info, source.mxid)
         async with self._update_info_lock:
+            update = await self._update_contact_info(force=number_updated) or update
+
             if isinstance(info, Profile) or self.name is None:
                 update = await self._update_name(info) or update
             if isinstance(info, Profile):
@@ -209,6 +216,33 @@ class Puppet(DBPuppet, BasePuppet):
                 await self.update()
                 background_task.create(self._try_update_portal_meta())
 
+    async def _update_contact_info(self, force: bool = False) -> bool:
+        if not self.bridge.homeserver_software.is_hungry:
+            return False
+
+        if self.contact_info_set and not force:
+            return False
+
+        try:
+            identifiers = [f"signal:{self.uuid}"]
+            if self.number:
+                identifiers.append(f"tel:{self.number}")
+            await self.default_mxid_intent.beeper_update_profile(
+                {
+                    "com.beeper.bridge.identifiers": identifiers,
+                    "com.beeper.bridge.remote_id": str(self.uuid),
+                    "com.beeper.bridge.service": "signal",
+                    "com.beeper.bridge.network": "signal",
+                    "com.beeper.bridge.is_bridge_bot": False,
+                    "com.beeper.bridge.is_bot": False,
+                }
+            )
+            self.contact_info_set = True
+        except Exception:
+            self.log.exception("Error updating contact info")
+            self.contact_info_set = False
+        return True
+
     @staticmethod
     def fmt_phone(number: str) -> str:
         if phonenumbers is None: