Skip to content

Commit

Permalink
Merge remote-tracking branch 'maltee1/number_as_topic'
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Aug 19, 2022
2 parents 960b589 + 3ddf53c commit be0b0e4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
6 changes: 2 additions & 4 deletions mautrix_signal/db/puppet.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from mausignald.types import Address
from mautrix.types import ContentURI, SyncToken, UserID
from mautrix.util.async_db import Database
from mautrix.util.async_db import Connection, Database

fake_db = Database.create("") if TYPE_CHECKING else None

Expand Down Expand Up @@ -96,9 +96,7 @@ async def _set_number(self, number: str) -> None:
await self._update_number_to_uuid(conn, number, str(self.uuid))

@staticmethod
async def _update_number_to_uuid(
conn: asyncpg.Connection, old_number: str, new_uuid: str
) -> None:
async def _update_number_to_uuid(conn: Connection, old_number: str, new_uuid: str) -> None:
try:
async with conn.transaction():
await conn.execute(
Expand Down
18 changes: 17 additions & 1 deletion mautrix_signal/portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1768,11 +1768,16 @@ async def update_info(self, source: u.User, info: ChatInfo) -> None:
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)
if self.mxid:
# This is only for automatically updating the topic in existing portals
await self.update_puppet_number(self.topic)
return

if isinstance(info, GroupV2ID):
Expand Down Expand Up @@ -1844,6 +1849,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
12 changes: 8 additions & 4 deletions mautrix_signal/puppet.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,10 @@ 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
if self.number:
self.by_number.pop(self.number, None)
self.number = number
self.by_number[self.number] = self
await self._set_number(number)
Expand Down Expand Up @@ -231,15 +233,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 +369,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

0 comments on commit be0b0e4

Please sign in to comment.