Fix chat avatar syncing (ref #15)

This commit is contained in:
Tulir Asokan
2017-12-01 15:46:47 +02:00
parent 678a2a460e
commit 1799732a0c
2 changed files with 35 additions and 5 deletions
+23
View File
@@ -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) {
+12 -5
View File
@@ -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,