Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set Signal user's phone number as topic in DMs #282

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion mautrix_signal/portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1515,11 +1515,14 @@ async def update_info(
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):
Expand Down Expand Up @@ -1591,6 +1594,17 @@ async def update_info_from_puppet(self, puppet: p.Puppet | None = None) -> None:
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
Expand Down
10 changes: 6 additions & 4 deletions mautrix_signal/puppet.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ async def handle_uuid_receive(self, uuid: UUID) -> None:

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
Expand Down Expand Up @@ -231,15 +231,15 @@ async def _migrate_powers(
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):
Expand Down Expand Up @@ -367,6 +367,8 @@ async def _update_portal_meta(self) -> None:
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}")

Expand Down