From cf24e42a0e738ad7b6423fe80696822807c171ef Mon Sep 17 00:00:00 2001 From: Mikunj Date: Tue, 27 Nov 2018 09:49:41 +1100 Subject: [PATCH] Added storing nicknames. --- js/models/conversations.js | 6 +++++- js/models/profile.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index 5fdad3772..c83bd0660 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -1739,9 +1739,13 @@ // Update profile variables dynamically async updateProfile() { const profileName = this.get('profileName'); + + // Prioritise nickname over the profile display name + const nickname = await storage.getNickname(this.id); const profile = await storage.getProfile(this.id); + const displayName = profile && profile.name && profile.name.displayName; - const newProfileName = (profile && profile.name && profile.name.displayName) || null + const newProfileName = nickname || displayName || null; if (profileName !== newProfileName) { this.set({ profileName: newProfileName }); await window.Signal.Data.updateConversation(this.id, this.attributes, { diff --git a/js/models/profile.js b/js/models/profile.js index 94edd1352..a7a93d45e 100644 --- a/js/models/profile.js +++ b/js/models/profile.js @@ -43,4 +43,41 @@ window.log.info('removing profile for ', number); await storage.put(PROFILE_ID, profiles); } + + // Names that user can set for users + + const NICKNAME_ID = 'nickname'; + + storage.getNickname = number => { + const nicknames = storage.get(NICKNAME_ID, {}); + return nicknames[number] || null; + } + + storage.saveNickname = async (number, name) => { + const nicknames = storage.get(NICKNAME_ID, {}); + const storedName = nicknames[number]; + + // Only store the name if we have a different name + if (storedName === name) { + return; + } + + window.log.info('adding nickname ', name, 'for ', number); + await storage.put(NICKNAME_ID, { + ...nicknames, + [number]: name, + }); + } + + storage.removeNickname = async number => { + const nicknames = storage.get(NICKNAME_ID, {}); + if (!nicknames[number]) { + return; + } + + delete nicknames[number]; + + window.log.info('removing nickname for ', number); + await storage.put(NICKNAME_ID, nicknames); + } })();