From 60e35c1bb9ba3c5e17c0073e0ad68d92ace67ce7 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sat, 10 Aug 2019 14:24:26 +0300 Subject: [PATCH] Add command to sync specific portal --- mautrix_telegram/commands/portal/misc.py | 21 +++++++++++++++++++++ mautrix_telegram/portal/metadata.py | 17 ++++++++++------- mautrix_telegram/puppet.py | 4 +++- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/mautrix_telegram/commands/portal/misc.py b/mautrix_telegram/commands/portal/misc.py index d42000c2..bc74b4d2 100644 --- a/mautrix_telegram/commands/portal/misc.py +++ b/mautrix_telegram/commands/portal/misc.py @@ -37,6 +37,27 @@ async def sync_state(evt: CommandEvent) -> EventID: await evt.reply("Synchronization complete") +@command_handler(needs_admin=False, needs_puppeting=False, needs_auth=False, + help_section=SECTION_MISC) +async def sync_full(evt: CommandEvent) -> EventID: + portal = po.Portal.get_by_mxid(evt.room_id) + if not portal: + return await evt.reply("This is not a portal room.") + + if evt.args[0] == "--usebot" and evt.sender.is_admin: + src = evt.tgbot + else: + src = evt.tgbot if await evt.sender.needs_relaybot(portal) else evt.sender + + try: + entity = await src.client.get_entity(portal.peer) + except ValueError: + return await evt.reply("Failed to get portal info from Telegram.") + + await portal.update_matrix_room(src, entity) + return await evt.reply("Portal synced successfully.") + + @command_handler(name="id", needs_admin=False, needs_puppeting=False, needs_auth=False, help_section=SECTION_MISC, help_text="Get the ID of the Telegram chat where this room is bridged.") diff --git a/mautrix_telegram/portal/metadata.py b/mautrix_telegram/portal/metadata.py index e1b0cd84..e5f3ccea 100644 --- a/mautrix_telegram/portal/metadata.py +++ b/mautrix_telegram/portal/metadata.py @@ -188,10 +188,12 @@ class PortalMetadata(BasePortal, ABC): await self.main_intent.invite_user(self.mxid, users, check_cache=True) async def update_matrix_room(self, user: 'AbstractUser', entity: Union[TypeChat, User], - direct: bool, puppet: p.Puppet = None, + direct: bool = None, puppet: p.Puppet = None, levels: PowerLevelStateEventContent = None, users: List[User] = None, participants: List[TypeParticipant] = None) -> None: + if direct is None: + direct = self.peer_type == "user" try: await self._update_matrix_room(user, entity, direct, puppet, levels, users, participants) @@ -199,10 +201,10 @@ class PortalMetadata(BasePortal, ABC): self.log.exception("Fatal error updating Matrix room") async def _update_matrix_room(self, user: 'AbstractUser', entity: Union[TypeChat, User], - direct: bool, puppet: p.Puppet = None, - levels: PowerLevelStateEventContent = None, - users: List[User] = None, - participants: List[TypeParticipant] = None) -> None: + direct: bool, puppet: p.Puppet = None, + levels: PowerLevelStateEventContent = None, + users: List[User] = None, + participants: List[TypeParticipant] = None) -> None: if not direct: await self.update_info(user, entity) if not users or not participants: @@ -526,8 +528,9 @@ class PortalMetadata(BasePortal, ABC): if self.peer_type == "channel": changed = await self._update_username(entity.username) or changed - # TODO update about text - # changed = self.update_about(entity.about) or changed + + if hasattr(entity, "about"): + changed = self._update_about(entity.about) or changed changed = await self._update_title(entity.title) or changed diff --git a/mautrix_telegram/puppet.py b/mautrix_telegram/puppet.py index 3da92731..cb3c9d9f 100644 --- a/mautrix_telegram/puppet.py +++ b/mautrix_telegram/puppet.py @@ -192,7 +192,9 @@ class Puppet(CustomPuppetMixin): def _filter_name(name: str) -> str: if not name: return "" - whitespace = ("\t\n\r\v\f \u00a0\u034f\u180e\u2063\u202f\u205f\u2800\u3000\u3164\ufeff\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u200c\u200d\u200e\u200f") + whitespace = ("\t\n\r\v\f \u00a0\u034f\u180e\u2063\u202f\u205f\u2800\u3000\u3164\ufeff" + "\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b" + "\u200c\u200d\u200e\u200f") name = "".join(c for c in name.strip(whitespace) if unicodedata.category(c) != 'Cf') return name