Merge pull request #453 from Mikunj/profile-simplification

Profile simplification
pull/465/head
Beaudan Campbell-Brown 6 years ago committed by GitHub
commit a6abcaf826
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -689,25 +689,21 @@
} }
}); });
Whisper.events.on('onEditProfile', () => { Whisper.events.on('onEditProfile', async () => {
const ourNumber = textsecure.storage.user.getNumber(); const ourNumber = textsecure.storage.user.getNumber();
const profile = storage.getLocalProfile(); const conversation = await ConversationController.getOrCreateAndWait(
ourNumber,
'private'
);
const profile = conversation.getLokiProfile();
const displayName = profile && profile.displayName; const displayName = profile && profile.displayName;
if (appView) { if (appView) {
appView.showNicknameDialog({ appView.showNicknameDialog({
title: window.i18n('editProfileTitle'), title: window.i18n('editProfileTitle'),
message: window.i18n('editProfileDisplayNameWarning'), message: window.i18n('editProfileDisplayNameWarning'),
nickname: displayName, nickname: displayName,
onOk: async newName => { onOk: newName =>
await storage.setProfileName(newName); conversation.setLokiProfile({ displayName: newName }),
// Update the conversation if we have it
const conversation = ConversationController.get(ourNumber);
if (conversation) {
const newProfile = storage.getLocalProfile();
conversation.setProfile(newProfile);
}
},
}); });
} }
}); });

@ -242,7 +242,7 @@
} }
promises.concat([ promises.concat([
conversation.updateProfile(), conversation.updateProfileName(),
conversation.updateProfileAvatar(), conversation.updateProfileAvatar(),
conversation.resetPendingSend(), conversation.resetPendingSend(),
conversation.setFriendRequestExpiryTimeout(), conversation.setFriendRequestExpiryTimeout(),

@ -2020,9 +2020,9 @@
Conversation: Whisper.Conversation, Conversation: Whisper.Conversation,
}); });
await this.updateProfile(); await this.updateProfileName();
}, },
async setProfile(profile) { async setLokiProfile(profile) {
if (!_.isEqual(this.get('profile'), profile)) { if (!_.isEqual(this.get('profile'), profile)) {
this.set({ profile }); this.set({ profile });
await window.Signal.Data.updateConversation(this.id, this.attributes, { await window.Signal.Data.updateConversation(this.id, this.attributes, {
@ -2030,18 +2030,18 @@
}); });
} }
await this.updateProfile(); await this.updateProfileName();
}, },
async updateProfile() { async updateProfileName() {
// Prioritise nickname over the profile display name // Prioritise nickname over the profile display name
const nickname = this.getNickname(); const nickname = this.getNickname();
const profile = this.getLocalProfile(); const profile = this.getLokiProfile();
const displayName = profile && profile.displayName; const displayName = profile && profile.displayName;
const profileName = nickname || displayName || null; const profileName = nickname || displayName || null;
await this.setProfileName(profileName); await this.setProfileName(profileName);
}, },
getLocalProfile() { getLokiProfile() {
return this.get('profile'); return this.get('profile');
}, },
getNickname() { getNickname() {
@ -2140,7 +2140,7 @@
const c = await ConversationController.getOrCreateAndWait(id, 'private'); const c = await ConversationController.getOrCreateAndWait(id, 'private');
// We only need to update the profile as they are all stored inside the conversation // We only need to update the profile as they are all stored inside the conversation
await c.updateProfile(); await c.updateProfileName();
}, },
async setProfileName(name) { async setProfileName(name) {
const profileName = this.get('profileName'); const profileName = this.get('profileName');

@ -2006,7 +2006,7 @@
} else if (dataMessage.profile) { } else if (dataMessage.profile) {
ConversationController.getOrCreateAndWait(source, 'private').then( ConversationController.getOrCreateAndWait(source, 'private').then(
sender => { sender => {
sender.setProfile(dataMessage.profile); sender.setLokiProfile(dataMessage.profile);
} }
); );
} }

@ -1,56 +0,0 @@
/* global storage, _ */
/* global storage: false */
/* eslint-disable more/no-then */
// eslint-disable-next-line func-names
(function() {
'use strict';
window.Whisper = window.Whisper || {};
const PROFILE_ID = 'local-profile';
storage.getLocalProfile = () => {
const profile = storage.get(PROFILE_ID, null);
return profile;
};
storage.setProfileName = async newName => {
if (typeof newName !== 'string' && newName !== null) {
throw Error('Name must be a string!');
}
// Update our profiles accordingly'
const trimmed = newName && newName.trim();
// If we get an empty name then unset the name property
// Otherwise update it
const profile = storage.getLocalProfile();
const newProfile = { ...profile };
if (_.isEmpty(trimmed)) {
delete newProfile.displayName;
} else {
newProfile.displayName = trimmed;
}
await storage.saveLocalProfile(newProfile);
};
storage.saveLocalProfile = async profile => {
const storedProfile = storage.get(PROFILE_ID, null);
// Only store the profile if we have a different object
if (storedProfile && _.isEqual(storedProfile, profile)) {
return;
}
window.log.info('saving local profile ', profile);
await storage.put(PROFILE_ID, profile);
};
storage.removeLocalProfile = async () => {
window.log.info('removing local profile');
await storage.remove(PROFILE_ID);
};
})();

@ -302,7 +302,7 @@
} }
if (conversation) { if (conversation) {
conversation.updateProfile(); conversation.updateProfileName();
} }
this.conversation_stack.open(conversation); this.conversation_stack.open(conversation);

@ -10,7 +10,6 @@
dcodeIO, dcodeIO,
StringView, StringView,
log, log,
storage,
Event, Event,
ConversationController ConversationController
*/ */
@ -536,7 +535,7 @@
saveMnemonic(mnemonic) { saveMnemonic(mnemonic) {
return textsecure.storage.put('mnemonic', mnemonic); return textsecure.storage.put('mnemonic', mnemonic);
}, },
async registrationDone(number, profileName) { async registrationDone(number, displayName) {
window.log.info('registration done'); window.log.info('registration done');
// Ensure that we always have a conversation for ourself // Ensure that we always have a conversation for ourself
@ -544,12 +543,7 @@
number, number,
'private' 'private'
); );
await conversation.setLokiProfile({ displayName });
await storage.setProfileName(profileName);
// Update the conversation if we have it
const newProfile = storage.getLocalProfile();
await conversation.setProfile(newProfile);
this.dispatchEvent(new Event('registration')); this.dispatchEvent(new Event('registration'));
}, },

@ -1074,7 +1074,7 @@ MessageReceiver.prototype.extend({
} }
// Update the conversation // Update the conversation
await conversation.setProfile(profile); await conversation.setLokiProfile(profile);
} }
if (friendRequest && isMe) { if (friendRequest && isMe) {

@ -819,7 +819,18 @@ MessageSender.prototype = {
return message.toArrayBuffer(); return message.toArrayBuffer();
}, },
sendMessageToNumber( getOurProfile() {
try {
const ourNumber = textsecure.storage.user.getNumber();
const conversation = window.ConversationController.get(ourNumber);
return conversation.getLokiProfile();
} catch (e) {
window.log.error(`Failed to get our profile: ${e}`);
return null;
}
},
async sendMessageToNumber(
number, number,
messageText, messageText,
attachments, attachments,
@ -830,7 +841,7 @@ MessageSender.prototype = {
profileKey, profileKey,
options options
) { ) {
const profile = textsecure.storage.impl.getLocalProfile(); const profile = this.getOurProfile();
return this.sendMessage( return this.sendMessage(
{ {
recipients: [number], recipients: [number],
@ -946,7 +957,7 @@ MessageSender.prototype = {
if (options.isPublic) { if (options.isPublic) {
numbers = [groupId]; numbers = [groupId];
} }
const profile = textsecure.storage.impl.getLocalProfile(); const profile = this.getOurProfile();
const attrs = { const attrs = {
recipients: numbers, recipients: numbers,
body: messageText, body: messageText,

@ -3264,6 +3264,7 @@
.module-left-pane__list { .module-left-pane__list {
flex-grow: 1; flex-grow: 1;
flex-shrink: 1; flex-shrink: 1;
overflow-y: auto;
} }
.module-left-pane__virtual-list { .module-left-pane__virtual-list {

@ -1,108 +0,0 @@
/* global storage */
/* eslint no-await-in-loop: 0 */
'use strict';
const PROFILE_ID = 'local-profile';
describe('Profile', () => {
beforeEach(async () => {
await clearDatabase();
await storage.remove(PROFILE_ID);
});
describe('getLocalProfile', () => {
it('returns the local profile', async () => {
const values = [null, 'hello', { a: 'b' }];
for (let i = 0; i < values.length; i += 1) {
await storage.put(PROFILE_ID, values[i]);
assert.strictEqual(values[i], storage.getLocalProfile());
}
});
});
describe('saveLocalProfile', () => {
it('saves a profile', async () => {
const values = [null, 'hello', { a: 'b' }];
for (let i = 0; i < values.length; i += 1) {
await storage.saveLocalProfile(values[i]);
assert.strictEqual(values[i], storage.get(PROFILE_ID));
}
});
});
describe('removeLocalProfile', () => {
it('removes a profile', async () => {
await storage.saveLocalProfile('a');
assert.strictEqual('a', storage.getLocalProfile());
await storage.removeLocalProfile();
assert.strictEqual(null, storage.getLocalProfile());
});
});
describe('setProfileName', () => {
it('throws if a name is not a string', async () => {
const values = [0, { a: 'b' }, [1, 2]];
for (let i = 0; i < values.length; i += 1) {
try {
await storage.setProfileName(values[i]);
assert.fail(
`setProfileName did not throw an error for ${typeof values[i]}`
);
} catch (e) {
assert.throws(() => {
throw e;
}, 'Name must be a string!');
}
}
});
it('does not throw if we pass a string or null', async () => {
const values = [null, '1'];
for (let i = 0; i < values.length; i += 1) {
try {
await storage.setProfileName(values[i]);
} catch (e) {
assert.fail('setProfileName threw an error');
}
}
});
it('saves the display name', async () => {
await storage.setProfileName('hi there!');
const profile = storage.getLocalProfile();
assert.deepEqual(profile.displayName, 'hi there!');
});
it('saves the display name without overwriting the other profile properties', async () => {
const profile = { title: 'hello' };
await storage.put(PROFILE_ID, profile);
await storage.setProfileName('hi there!');
const expected = {
...profile,
displayName: 'hi there!',
};
assert.deepEqual(expected, storage.getLocalProfile());
});
it('trims the display name', async () => {
await storage.setProfileName(' in middle ');
const profile = storage.getLocalProfile();
assert.deepEqual('in middle', profile.displayName);
});
it('unsets the display name property if it is empty', async () => {
const profile = {
displayName: 'HI THERE!',
};
await storage.put(PROFILE_ID, profile);
assert.exists(storage.getLocalProfile().displayName);
await storage.setProfileName('');
assert.notExists(storage.getLocalProfile().displayName);
});
});
});
Loading…
Cancel
Save