From 7434393e1fc2f78894ece2f2691b3b9c4a20545e Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 5 Feb 2018 22:36:11 +0200 Subject: [PATCH] Use username for invite link and fix other things Fixes #50 Other things: * Display flood wait errors from commands * (Maybe) Fix removing room aliases when removing channel username --- mautrix_telegram/commands.py | 21 ++++++++++++++++++++- mautrix_telegram/portal.py | 9 +++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/mautrix_telegram/commands.py b/mautrix_telegram/commands.py index c24c225a..ea3d3340 100644 --- a/mautrix_telegram/commands.py +++ b/mautrix_telegram/commands.py @@ -32,6 +32,24 @@ def command_handler(func): return func +def format_duration(seconds): + def pluralize(count, singular): return singular if count == 1 else singular + "s" + + def include(count, word): return f"{count} {pluralize(count, word)}" if count > 0 else "" + + minutes, seconds = divmod(seconds, 60) + hours, minutes = divmod(minutes, 60) + days, hours = divmod(hours, 24) + parts = [a for a in [ + include(days, "day"), + include(hours, "hour"), + include(minutes, "minute"), + include(seconds, "second")] if a] + if len(parts) > 2: + return "{} and {}".format(", ".join(parts[:-1]), parts[-1]) + return " and ".join(parts) + + class CommandHandler: def __init__(self, context): self.az, self.db, log, self.config = context @@ -47,6 +65,8 @@ class CommandHandler: with self.handler(sender, room, command, args, is_management, is_portal) as handle_command: try: handle_command(self, sender, args) + except FloodWaitError as e: + self.reply(f"Flood error: Please wait {format_duration(e.seconds)}") except Exception: self.reply("Fatal error while handling command. Check logs for more details.") self.log.exception(f"Fatal error handling command " @@ -420,7 +440,6 @@ class CommandHandler: except UsernameInvalidError: return self.reply("Invalid username") - # endregion # region Command-related commands @command_handler diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index 0334c255..bef409b7 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -223,7 +223,7 @@ class Portal: if self.username != username: if self.username: self.main_intent.remove_room_alias(self._get_room_alias()) - self.username = username + self.username = username or None if self.username: self.main_intent.add_room_alias(self.mxid, self._get_room_alias()) return True @@ -282,6 +282,8 @@ class Portal: elif self.peer_type == "chat": link = user.client(ExportChatInviteRequest(chat_id=self.tgid)) elif self.peer_type == "channel": + if self.username: + return f"https://t.me/{self.username}" link = user.client( ExportInviteRequest(channel=self.get_input_entity(user))) else: @@ -484,9 +486,8 @@ class Portal: if self.peer_type != "channel": raise ValueError("Only channels and supergroups have usernames.") success = source.client(UpdateUsernameRequest(self.get_input_entity(source), username)) - if success: - if self.update_username(username): - self.save() + if self.update_username(username): + self.save() def create_telegram_chat(self, source, supergroup=False): if not self.mxid: