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
This commit is contained in:
Tulir Asokan
2018-02-05 22:36:11 +02:00
parent 1161c75f3e
commit 7434393e1f
2 changed files with 25 additions and 5 deletions
+20 -1
View File
@@ -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
+5 -4
View File
@@ -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: