From 127b7d41fa4a5aa57b498cdac1c5ad4f20fb7fa5 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Mon, 3 May 2021 10:06:35 +1000 Subject: [PATCH] fix update of avatar image for opengroupv2 --- ts/models/conversation.ts | 39 ++++++++++--------- .../opengroupV2/OpenGroupManagerV2.ts | 4 ++ .../opengroupV2/OpenGroupServerPoller.ts | 17 ++++---- ts/opengroup/opengroupV2/OpenGroupUpdate.ts | 14 ++++--- 4 files changed, 43 insertions(+), 31 deletions(-) diff --git a/ts/models/conversation.ts b/ts/models/conversation.ts index d584ac4d3..2b78ad2fa 100644 --- a/ts/models/conversation.ts +++ b/ts/models/conversation.ts @@ -1082,7 +1082,11 @@ export class ConversationModel extends Backbone.Model { await this.updateProfileName(); } - public async setLokiProfile(newProfile: { displayName?: string | null; avatar?: string }) { + public async setLokiProfile(newProfile: { + displayName?: string | null; + avatar?: string; + avatarHash?: string; + }) { if (!_.isEqual(this.get('profile'), newProfile)) { this.set({ profile: newProfile }); await this.commit(); @@ -1091,7 +1095,7 @@ export class ConversationModel extends Backbone.Model { // a user cannot remove an avatar. Only change it // if you change this behavior, double check all setLokiProfile calls (especially the one in EditProfileDialog) if (newProfile.avatar) { - await this.setProfileAvatar({ path: newProfile.avatar }); + await this.setProfileAvatar({ path: newProfile.avatar }, newProfile.avatarHash); } await this.updateProfileName(); @@ -1216,25 +1220,22 @@ export class ConversationModel extends Backbone.Model { } // Not sure if we care about updating the database } - public async setGroupNameAndAvatar(name: string, avatarPath: string) { - const currentName = this.get('name'); - const profileAvatar = this.get('avatar'); - if (profileAvatar !== avatarPath || currentName !== name) { - // only update changed items - if (profileAvatar !== avatarPath) { - this.set({ avatar: avatarPath }); - } - if (currentName !== name) { - this.set({ name }); - } - // save - await this.commit(); - } - } - public async setProfileAvatar(avatar: any) { + + public async setProfileAvatar(avatar: any, avatarHash?: string) { const profileAvatar = this.get('avatar'); + const existingHash = this.get('avatarHash'); + let shouldCommit = false; if (profileAvatar !== avatar) { this.set({ avatar }); + shouldCommit = true; + } + + if (existingHash !== avatarHash) { + this.set({ avatarHash }); + shouldCommit = true; + } + + if (shouldCommit) { await this.commit(); } } @@ -1453,7 +1454,7 @@ export class ConversationModel extends Backbone.Model { return avatar; } - if (avatar && avatar.path && typeof avatar.path === 'string') { + if (typeof avatar?.path === 'string') { const { getAbsoluteAttachmentPath } = window.Signal.Migrations; return getAbsoluteAttachmentPath(avatar.path) as string; diff --git a/ts/opengroup/opengroupV2/OpenGroupManagerV2.ts b/ts/opengroup/opengroupV2/OpenGroupManagerV2.ts index 8610da583..b42e6cb7a 100644 --- a/ts/opengroup/opengroupV2/OpenGroupManagerV2.ts +++ b/ts/opengroup/opengroupV2/OpenGroupManagerV2.ts @@ -20,6 +20,10 @@ export class OpenGroupManagerV2 { private static instance: OpenGroupManagerV2; + /** + * The map of opengroup pollers, by serverUrl. + * A single poller polls for every room on the specified serverUrl + */ private readonly pollers: Map = new Map(); private isPolling = false; diff --git a/ts/opengroup/opengroupV2/OpenGroupServerPoller.ts b/ts/opengroup/opengroupV2/OpenGroupServerPoller.ts index 286a8242d..a063e7f15 100644 --- a/ts/opengroup/opengroupV2/OpenGroupServerPoller.ts +++ b/ts/opengroup/opengroupV2/OpenGroupServerPoller.ts @@ -71,15 +71,11 @@ export class OpenGroupServerPoller { this.previewPerRoomPoll, pollForRoomAvatarInterval ); - - // first refresh of avatar rooms is in a day, force it now just in case - global.setTimeout(this.previewPerRoomPoll, SECONDS * 30); } /** * Add a room to the polled room for this server. * If a request is already in progress, it will be added only on the next run. - * The interval is always ticking, even doing nothing except realizing it has nothing to do */ public addRoomToPoll(room: OpenGroupRequestCommonType) { if (room.serverUrl !== this.serverUrl) { @@ -90,6 +86,10 @@ export class OpenGroupServerPoller { return; } this.roomIdsToPoll.add(room.roomId); + + // if we are not already polling right now, trigger a polling + void this.compactPoll(); + void this.previewPerRoomPoll(); } public removeRoomFromPoll(room: OpenGroupRequestCommonType) { @@ -374,7 +374,7 @@ const handleBase64AvatarUpdate = async ( if (newHash !== existingHash) { // write the file to the disk (automatically encrypted), // ArrayBuffer - const { getAbsoluteAttachmentPath, processNewAttachment } = window.Signal.Migrations; + const { processNewAttachment } = window.Signal.Migrations; const upgradedAttachment = await processNewAttachment({ isRaw: true, @@ -382,11 +382,14 @@ const handleBase64AvatarUpdate = async ( url: `${serverUrl}/${res.roomId}`, }); // update the hash on the conversationModel + await convo.setLokiProfile({ + displayName: convo.getName() || window.i18n('unknown'), + avatar: upgradedAttachment.path, + avatarHash: newHash, + }); convo.set({ - avatar: await getAbsoluteAttachmentPath(upgradedAttachment.path), avatarHash: newHash, }); - // trigger the write to db and refresh the UI await convo.commit(); } diff --git a/ts/opengroup/opengroupV2/OpenGroupUpdate.ts b/ts/opengroup/opengroupV2/OpenGroupUpdate.ts index 0e1bb8a90..06c051c5d 100644 --- a/ts/opengroup/opengroupV2/OpenGroupUpdate.ts +++ b/ts/opengroup/opengroupV2/OpenGroupUpdate.ts @@ -2,6 +2,8 @@ import { ApiV2 } from '.'; import { getV2OpenGroupRoom } from '../../data/opengroups'; import { ConversationModel } from '../../models/conversation'; import { downloadAttachmentOpenGroupV2 } from '../../receiver/attachments'; +import { sha256 } from '../../session/crypto'; +import { fromArrayBufferToBase64 } from '../../session/utils/String'; import { arrayBufferFromFile } from '../../types/Attachment'; import { AttachmentUtil } from '../../util'; @@ -68,12 +70,14 @@ export async function updateOpenGroupV2(convo: ConversationModel, groupName: str isRaw: true, url: pathname, }); - // TODO on our opengroupv2 we don't have a way to know when the file changed on the server. - // maybe we should download it once in a while even if we don't know if the file changed? - convo.set('avatarPointer', pathname); - + // FIXME audric update of roomname on the server? window.log.warn('TODO update of roomName'); - await convo.setGroupNameAndAvatar(convo.get('name') || 'Unknown', upgraded.path); + const newHash = sha256(fromArrayBufferToBase64(downloaded.buffer)); + await convo.setLokiProfile({ + displayName: groupName || convo.get('name') || 'Unknown', + avatar: upgraded.path, + avatarHash: newHash, + }); } catch (e) { window.log.error(`Could not decrypt profile image: ${e}`); }