From aa1d6c089c4b8cd197cc5d157e78b5462bd892a1 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 5 Feb 2018 19:40:13 +0200 Subject: [PATCH] Update Telegram user info automatically --- ROADMAP.md | 11 +++++++++-- mautrix_appservice/intent_api.py | 5 +++++ mautrix_telegram/puppet.py | 2 +- mautrix_telegram/user.py | 11 +++++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index bb99ec69..79639afd 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -25,10 +25,13 @@ * [x] Kicking * [ ] Joining * [ ] Chat name as alias - * [ ] (Maybe) Chat invite link as alias + * [ ] ‡ Chat invite link as alias * [x] Leaving * [x] Room metadata changes (name, topic, avatar) * [x] Initial room metadata + * [ ] User metadata + * [ ] Initial displayname/username/avatar at register + * [ ] ‡ Changes to displayname/avatar * Telegram → Matrix * [x] Plaintext messages * [x] Formatted messages @@ -60,6 +63,9 @@ * [ ] † About text * [ ] † Public channel username * [x] Initial chat metadata (about text missing) + * [x] User metadata + * [x] Initial displayname/avatar + * [x] Changes to displayname/avatar * [x] Supergroup upgrade * Misc * [x] Automatic portal creation @@ -82,4 +88,5 @@ * [x] Getting the Telegram invite link to a Matrix room (`invitelink`) * [x] Clean up and forget a portal room (`deleteportal`) -† Information not automatically sent from source, i.e. implementation may not be possible +† Information not automatically sent from source, i.e. implementation may not be possible +‡ Maybe, i.e. this feature may or may not be implemented at some point diff --git a/mautrix_appservice/intent_api.py b/mautrix_appservice/intent_api.py index f08b3c3e..9e4b06e1 100644 --- a/mautrix_appservice/intent_api.py +++ b/mautrix_appservice/intent_api.py @@ -152,6 +152,11 @@ class IntentAPI: # region User actions + def get_joined_rooms(self): + self.ensure_registered() + response = self.client._send("GET", "/joined_rooms") + return response["joined_rooms"] + def set_display_name(self, name): self.ensure_registered() return self.client.set_display_name(self.mxid, name) diff --git a/mautrix_telegram/puppet.py b/mautrix_telegram/puppet.py index 69251f4b..3b6803cd 100644 --- a/mautrix_telegram/puppet.py +++ b/mautrix_telegram/puppet.py @@ -66,7 +66,7 @@ class Puppet: @staticmethod def get_displayname(info, format=True): data = { - "phone number": info.phone, + "phone number": info.phone if hasattr(info, "phone") else None, "username": info.username, "full name": " ".join([info.first_name or "", info.last_name or ""]).strip(), "full name reversed": " ".join([info.first_name or "", info.last_name or ""]).strip(), diff --git a/mautrix_telegram/user.py b/mautrix_telegram/user.py index 644557d3..e64e687c 100644 --- a/mautrix_telegram/user.py +++ b/mautrix_telegram/user.py @@ -161,6 +161,8 @@ class User: portal = po.Portal.get_by_tgid(update.channel_id, peer_type="channel") if portal and portal.mxid: portal.update_telegram_pin(self, update.id) + elif isinstance(update, (UpdateUserName, UpdateUserPhoto)): + self.update_others_info(update) else: self.log.debug("Unhandled update: %s", update) @@ -181,6 +183,15 @@ class User: sender = pu.Puppet.get(update.user_id) return portal.handle_telegram_typing(sender, update) + def update_others_info(self, update): + puppet = pu.Puppet.get(update.user_id) + if isinstance(update, UpdateUserName): + if puppet.update_displayname(self, update): + puppet.save() + elif isinstance(update, UpdateUserPhoto): + if puppet.update_avatar(self, update.photo.photo_big): + puppet.save() + def update_status(self, update): puppet = pu.Puppet.get(update.user_id) if isinstance(update.status, UserStatusOnline):