From 1799732a0cd217f1aa6497a6586545f60658b6e8 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 1 Dec 2017 15:46:47 +0200 Subject: [PATCH] Fix chat avatar syncing (ref #15) --- src/matrix-user.js | 23 +++++++++++++++++++++++ src/portal.js | 17 ++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/matrix-user.js b/src/matrix-user.js index 1f309fee..f0b4d72a 100644 --- a/src/matrix-user.js +++ b/src/matrix-user.js @@ -178,6 +178,29 @@ class MatrixUser { async syncChats({ createRooms = true } = {}) { const dialogs = await this.telegramPuppet.client("messages.getDialogs", {}) let changed = false + + for (const user of dialogs.users) { + if (!user.self) { + continue + } + // Automatically create Saved Messages room + const peer = new TelegramPeer("user", user.id, { + receiverID: user.id, + accessHash: user.access_hash, + }) + const portal = await this.app.getPortalByPeer(peer) + if (createRooms) { + try { + await portal.createMatrixRoom(this.telegramPuppet, { + invite: [this.userID], + }) + } catch (err) { + console.error(err) + console.error(err.stack) + } + } + } + this.chats = [] for (const dialog of dialogs.chats) { if (dialog._ === "chatForbidden" || dialog.deactivated) { diff --git a/src/portal.js b/src/portal.js index ed93760e..01cba7ce 100644 --- a/src/portal.js +++ b/src/portal.js @@ -43,6 +43,8 @@ class Portal { } const portal = new Portal(app, entry.data.roomID, TelegramPeer.fromSubentry(entry.data.peer)) + portal.photo = entry.data.photo + portal.avatarURL = entry.data.avatarURL if (portal.peer.type === "channel") { portal.accessHashes = new Map(entry.data.accessHashes) } @@ -94,12 +96,10 @@ class Portal { } async updateAvatar(telegramPOV, photo) { - if (!photo || !photo.location || this.peer.type === "user") { + if (!photo || this.peer.type === "user") { return false } - photo = photo.location - if (this.photo && this.avatarURL && this.photo.dc_id === photo.dc_id && this.photo.volume_id === photo.volume_id && @@ -184,7 +184,7 @@ class Portal { } } // TODO once permissions are synced, make the avatar change event come from the user who changed the avatar - await this.updateAvatar(evt.source, largestSize) + await this.updateAvatar(evt.source, largestSize.location) break case "messageActionChatEditTitle": this.peer.title = evt.action.title @@ -363,7 +363,9 @@ class Portal { room = await user.intent.createRoom({ createAsClient: true, options: { - //name: user.getDisplayName(), + name: this.peer.id === this.peer.receiverID + ? "Saved Messages (Telegram)" + : undefined, //user.getDisplayName(), topic: "Telegram private chat", visibility: "private", invite, @@ -412,6 +414,9 @@ class Portal { const user = await this.app.getTelegramUser(this.peer.id) await user.updateInfo(telegramPOV, dialog) } + if (dialog.photo && dialog.photo.photo_big) { + changed = await this.updateAvatar(telegramPOV, dialog.photo.photo_big) || changed + } changed = this.peer.updateInfo(dialog) || changed if (changed) { this.save() @@ -427,6 +432,8 @@ class Portal { data: { roomID: this.roomID, peer: this.peer.toSubentry(), + photo: this.photo, + avatarURL: this.avatarURL, accessHashes: this.peer.type === "channel" ? Array.from(this.accessHashes) : undefined,