fix update of avatar image for opengroupv2

pull/1576/head
Audric Ackermann 4 years ago
parent bdcdca206b
commit 127b7d41fa
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -1082,7 +1082,11 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
await this.updateProfileName(); 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)) { if (!_.isEqual(this.get('profile'), newProfile)) {
this.set({ profile: newProfile }); this.set({ profile: newProfile });
await this.commit(); await this.commit();
@ -1091,7 +1095,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
// a user cannot remove an avatar. Only change it // 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 you change this behavior, double check all setLokiProfile calls (especially the one in EditProfileDialog)
if (newProfile.avatar) { if (newProfile.avatar) {
await this.setProfileAvatar({ path: newProfile.avatar }); await this.setProfileAvatar({ path: newProfile.avatar }, newProfile.avatarHash);
} }
await this.updateProfileName(); await this.updateProfileName();
@ -1216,25 +1220,22 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
} }
// Not sure if we care about updating the database // Not sure if we care about updating the database
} }
public async setGroupNameAndAvatar(name: string, avatarPath: string) {
const currentName = this.get('name'); public async setProfileAvatar(avatar: any, avatarHash?: string) {
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) {
const profileAvatar = this.get('avatar'); const profileAvatar = this.get('avatar');
const existingHash = this.get('avatarHash');
let shouldCommit = false;
if (profileAvatar !== avatar) { if (profileAvatar !== avatar) {
this.set({ avatar }); this.set({ avatar });
shouldCommit = true;
}
if (existingHash !== avatarHash) {
this.set({ avatarHash });
shouldCommit = true;
}
if (shouldCommit) {
await this.commit(); await this.commit();
} }
} }
@ -1453,7 +1454,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
return avatar; return avatar;
} }
if (avatar && avatar.path && typeof avatar.path === 'string') { if (typeof avatar?.path === 'string') {
const { getAbsoluteAttachmentPath } = window.Signal.Migrations; const { getAbsoluteAttachmentPath } = window.Signal.Migrations;
return getAbsoluteAttachmentPath(avatar.path) as string; return getAbsoluteAttachmentPath(avatar.path) as string;

@ -20,6 +20,10 @@ export class OpenGroupManagerV2 {
private static instance: 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<string, OpenGroupServerPoller> = new Map(); private readonly pollers: Map<string, OpenGroupServerPoller> = new Map();
private isPolling = false; private isPolling = false;

@ -71,15 +71,11 @@ export class OpenGroupServerPoller {
this.previewPerRoomPoll, this.previewPerRoomPoll,
pollForRoomAvatarInterval 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. * 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. * 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) { public addRoomToPoll(room: OpenGroupRequestCommonType) {
if (room.serverUrl !== this.serverUrl) { if (room.serverUrl !== this.serverUrl) {
@ -90,6 +86,10 @@ export class OpenGroupServerPoller {
return; return;
} }
this.roomIdsToPoll.add(room.roomId); 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) { public removeRoomFromPoll(room: OpenGroupRequestCommonType) {
@ -374,7 +374,7 @@ const handleBase64AvatarUpdate = async (
if (newHash !== existingHash) { if (newHash !== existingHash) {
// write the file to the disk (automatically encrypted), // write the file to the disk (automatically encrypted),
// ArrayBuffer // ArrayBuffer
const { getAbsoluteAttachmentPath, processNewAttachment } = window.Signal.Migrations; const { processNewAttachment } = window.Signal.Migrations;
const upgradedAttachment = await processNewAttachment({ const upgradedAttachment = await processNewAttachment({
isRaw: true, isRaw: true,
@ -382,11 +382,14 @@ const handleBase64AvatarUpdate = async (
url: `${serverUrl}/${res.roomId}`, url: `${serverUrl}/${res.roomId}`,
}); });
// update the hash on the conversationModel // update the hash on the conversationModel
await convo.setLokiProfile({
displayName: convo.getName() || window.i18n('unknown'),
avatar: upgradedAttachment.path,
avatarHash: newHash,
});
convo.set({ convo.set({
avatar: await getAbsoluteAttachmentPath(upgradedAttachment.path),
avatarHash: newHash, avatarHash: newHash,
}); });
// trigger the write to db and refresh the UI // trigger the write to db and refresh the UI
await convo.commit(); await convo.commit();
} }

@ -2,6 +2,8 @@ import { ApiV2 } from '.';
import { getV2OpenGroupRoom } from '../../data/opengroups'; import { getV2OpenGroupRoom } from '../../data/opengroups';
import { ConversationModel } from '../../models/conversation'; import { ConversationModel } from '../../models/conversation';
import { downloadAttachmentOpenGroupV2 } from '../../receiver/attachments'; import { downloadAttachmentOpenGroupV2 } from '../../receiver/attachments';
import { sha256 } from '../../session/crypto';
import { fromArrayBufferToBase64 } from '../../session/utils/String';
import { arrayBufferFromFile } from '../../types/Attachment'; import { arrayBufferFromFile } from '../../types/Attachment';
import { AttachmentUtil } from '../../util'; import { AttachmentUtil } from '../../util';
@ -68,12 +70,14 @@ export async function updateOpenGroupV2(convo: ConversationModel, groupName: str
isRaw: true, isRaw: true,
url: pathname, url: pathname,
}); });
// TODO on our opengroupv2 we don't have a way to know when the file changed on the server. // FIXME audric update of roomname 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);
window.log.warn('TODO update of roomName'); 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) { } catch (e) {
window.log.error(`Could not decrypt profile image: ${e}`); window.log.error(`Could not decrypt profile image: ${e}`);
} }

Loading…
Cancel
Save