Selaa lähdekoodia

set Signal user's phone number as topic in DMs

Malte E 3 vuotta sitten
vanhempi
sitoutus
3ddf53c780
2 muutettua tiedostoa jossa 21 lisäystä ja 5 poistoa
  1. 15 1
      mautrix_signal/portal.py
  2. 6 4
      mautrix_signal/puppet.py

+ 15 - 1
mautrix_signal/portal.py

@@ -1515,11 +1515,14 @@ class Portal(DBPortal, BasePortal):
         if self.is_direct:
             if not isinstance(info, (Profile, Address)):
                 raise ValueError(f"Unexpected type for direct chat update_info: {type(info)}")
-            if not self.name:
+            if not self.name or not self.topic:
                 puppet = await self.get_dm_puppet()
                 if not puppet.name:
                     await puppet.update_info(info, source)
                 self.name = puppet.name
+                if puppet.number and not self.topic:
+                    self.topic = puppet.fmt_phone(puppet.number)
+                    self.update_puppet_number(self.topic)
             return
 
         if isinstance(info, GroupV2ID):
@@ -1591,6 +1594,17 @@ class Portal(DBPortal, BasePortal):
             puppet = await self.get_dm_puppet()
         await self.update_puppet_name(puppet.name, save=False)
         await self.update_puppet_avatar(puppet.avatar_hash, puppet.avatar_url, save=False)
+        if puppet.number:
+            await self.update_puppet_number(puppet.fmt_phone(puppet.number), save=False)
+
+    async def update_puppet_number(self, number: str, save: bool = True) -> None:
+        if not self.encrypted and not self.private_chat_portal_meta:
+            return
+
+        changed = await self._update_topic(number)
+        if changed and save:
+            await self.update_bridge_info()
+            await self.update()
 
     async def update_puppet_avatar(
         self, new_hash: str, avatar_url: ContentURI, save: bool = True

+ 6 - 4
mautrix_signal/puppet.py

@@ -164,7 +164,7 @@ class Puppet(DBPuppet, BasePuppet):
 
     async def handle_number_receive(self, number: str) -> None:
         async with self._uuid_lock:
-            if self.number:
+            if self.number == number:
                 return
             self.number = number
             self.by_number[self.number] = self
@@ -231,15 +231,15 @@ class Puppet(DBPuppet, BasePuppet):
             self.log.warning("Failed to migrate power levels", exc_info=True)
 
     async def update_info(self, info: Profile | Address, source: u.User) -> None:
+        update = False
         address = info.address if isinstance(info, Profile) else info
         if address.uuid and not self.uuid:
             await self.handle_uuid_receive(address.uuid)
-        if address.number and not self.number:
+        if address.number and address.number != self.number:
             await self.handle_number_receive(address.number)
+            update = True
         self.log.debug("Updating info with %s (source: %s)", info, source.mxid)
-
         async with self._update_info_lock:
-            update = False
             if isinstance(info, Profile) or self.name is None:
                 update = await self._update_name(info) or update
             if isinstance(info, Profile):
@@ -367,6 +367,8 @@ class Puppet(DBPuppet, BasePuppet):
             try:
                 await portal.update_puppet_name(self.name)
                 await portal.update_puppet_avatar(self.avatar_hash, self.avatar_url)
+                if self.number:
+                    await portal.update_puppet_number(self.fmt_phone(self.number))
             except Exception:
                 self.log.exception(f"Error updating portal meta for {portal.receiver}")