From 26ba553e6ae9c593c011ecc36e278445b8af6dd3 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Fri, 7 Dec 2018 11:06:32 +1100 Subject: [PATCH] Added menu options to set, change and remove password. --- _locales/en/messages.json | 12 ++++++++++++ js/background.js | 6 ++++++ js/views/inbox_view.js | 20 ++++++++++++++++++++ js/views/main_header_view.js | 17 ++++++++++------- preload.js | 1 + 5 files changed, 49 insertions(+), 7 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index a29bb9195..e7420151b 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -1692,5 +1692,17 @@ }, "unlock": { "message": "Unlock" + }, + "setPassword": { + "message": "Set Password", + "description": "Button action that the user can click to set a password" + }, + "changePassword": { + "message": "Set Password", + "description": "Button action that the user can click to change a password" + }, + "removePassword": { + "message": "Set Password", + "description": "Button action that the user can click to remove a password" } } diff --git a/js/background.js b/js/background.js index dc94d55ba..faef150b6 100644 --- a/js/background.js +++ b/js/background.js @@ -610,6 +610,12 @@ window.log.error('Error showing PoW cog'); } }); + + Whisper.events.on('password-updated', () => { + if (appView && appView.inboxView) { + appView.inboxView.trigger('password-updated'); + } + }); } window.getSyncRequest = () => diff --git a/js/views/inbox_view.js b/js/views/inbox_view.js index 643cd135e..dc52dbbbe 100644 --- a/js/views/inbox_view.js +++ b/js/views/inbox_view.js @@ -6,6 +6,7 @@ /* global Whisper: false */ /* global textsecure: false */ /* global clipboard: false */ +/* global Signal: false */ // eslint-disable-next-line func-names (function() { @@ -106,6 +107,8 @@ el: this.$('.main-header-placeholder'), items: this.getMainHeaderItems(), }); + this.onPasswordUpdated(); + this.on('password-updated', () => this.onPasswordUpdated()); this.conversation_stack = new Whisper.ConversationStack({ el: this.$('.conversation-stack'), @@ -340,8 +343,25 @@ this._mainHeaderItem('editDisplayName', () => { window.Whisper.events.trigger('onEditProfile'); }), + ...this.passwordHeaderItems || [], ]; }, + async onPasswordUpdated() { + const hasPassword = await Signal.Data.getPasswordHash(); + const items = this.getMainHeaderItems(); + if (hasPassword) { + items.push( + this._mainHeaderItem('changePassword'), + this._mainHeaderItem('removePassword') + ); + } else { + items.push( + this._mainHeaderItem('setPassword') + ); + } + + this.mainHeaderView.updateItems(items); + }, _mainHeaderItem(textKey, onClick) { return { id: textKey, diff --git a/js/views/main_header_view.js b/js/views/main_header_view.js index 6366f914d..77da0632e 100644 --- a/js/views/main_header_view.js +++ b/js/views/main_header_view.js @@ -1,4 +1,4 @@ -/* global Whisper, textsecure, ConversationController, Signal, i18n */ +/* global Whisper, textsecure, ConversationController, Signal */ // eslint-disable-next-line func-names (function() { @@ -14,8 +14,6 @@ 'click .copy-key': 'onCopyKey', }, initialize(options) { - this.items = options.items || []; - this.ourNumber = textsecure.storage.user.getNumber(); const me = ConversationController.getOrCreate(this.ourNumber, 'private'); @@ -34,14 +32,19 @@ this.$content = this.$('.main-header-content-wrapper'); this.$content.hide(); - this.registerCallbacks(); + this.updateItems(options.items); }, - registerCallbacks() { - this.items.forEach(item => { + updateItems(items) { + this.$content.html(''); + (items || []).forEach(item => { + // Add the item + this.$content.append(`
${item.text}
`); + + // Register its callback if (item.onClick) { this.$(`#${item.id}`).click(item.onClick); } - }) + }); }, render_attributes() { return { diff --git a/preload.js b/preload.js index e55fedc0a..830079b3c 100644 --- a/preload.js +++ b/preload.js @@ -55,6 +55,7 @@ window.setPassword = (passPhrase, oldPhrase) => new Promise((resolve, reject) => if (error) { return reject(error); } + Whisper.events.trigger('password-updated'); return resolve(); }); ipc.send('set-password', passPhrase, oldPhrase);