From 51bc1f30da8071dd8daf4526548511a7bf25fc90 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 30 Nov 2017 20:56:25 +0200 Subject: [PATCH] Handle avatar and chat title changes --- README.md | 6 +++--- src/portal.js | 31 +++++++++++++++++++++++++++---- src/telegram-puppet.js | 1 + 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 48b49254..285a1617 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ A Telegram chat will be created once the bridge is stable enough. * [ ] Pinning messages * [ ] Power level * [ ] Membership actions (invite, kick, join, leave) - * [ ] Group/channel name/description changes + * [ ] Room metadata changes * Telegram → Matrix * [x] Plaintext messages * [x] Formatted messages @@ -54,8 +54,8 @@ A Telegram chat will be created once the bridge is stable enough. * [ ] Pinning messages * [ ] Admin status * [x] Membership actions (invite, kick, join, leave) - * [ ] Group/channel name/description changes - * [x] Initial group/channel name/description + * [x] Chat metadata changes + * [x] Initial chat metadata * [ ] Message edits * Initiating chats * [x] Automatic portal creation for groups/channels at startup diff --git a/src/portal.js b/src/portal.js index 06293012..ec37525d 100644 --- a/src/portal.js +++ b/src/portal.js @@ -93,12 +93,13 @@ class Portal { return uploaded } - async updateAvatar(telegramPOV, chat) { - if (!chat.photo || this.peer.type === "user") { + async updateAvatar(telegramPOV, photo) { + if (!photo || !photo.location || this.peer.type === "user") { return false } - const photo = chat.photo.photo_big + photo = photo.location + if (this.photo && this.avatarURL && this.photo.dc_id === photo.dc_id && this.photo.volume_id === photo.volume_id && @@ -171,6 +172,26 @@ class Portal { telegramUser = await this.app.getTelegramUser(evt.action.user_id) telegramUser.intent.leave(this.roomID) break + case "messageActionChatEditPhoto": + const sizes = evt.action.photo.sizes + let largestSize = sizes[0] + let largestSizePixels = largestSize.w * largestSize.h + for (const size of sizes) { + const pixels = size.w * size.h + if (pixels > largestSizePixels) { + largestSizePixels = pixels + largestSize = size + } + } + // TODO once permissions are synced, make the avatar change event come from the user who changed the avatar + await this.updateAvatar(evt.source, largestSize) + break + case "messageActionChatEditTitle": + this.peer.title = evt.action.title + await this.save() + const intent = await this.getMainIntent() + await intent.setRoomName(this.roomID, this.peer.title) + break default: console.log("Unhandled service message of type", evt.action._) console.log(evt.action) @@ -363,7 +384,9 @@ class Portal { if (this.peer.type !== "user") { try { await this.syncTelegramUsers(telegramPOV, users) - await this.updateAvatar(telegramPOV, info) + if (info.photo && info.photo.photo_big) { + await this.updateAvatar(telegramPOV, info.photo.photo_big) + } } catch (err) { console.error(err) if (err instanceof Error) { diff --git a/src/telegram-puppet.js b/src/telegram-puppet.js index ab31a1b7..daf96e1e 100644 --- a/src/telegram-puppet.js +++ b/src/telegram-puppet.js @@ -324,6 +324,7 @@ class TelegramPuppet { await portal.handleTelegramServiceMessage({ from, to, + source: this, action: update.action, }) return