From 475d607fd0fa6a924d72d0a0393342fe195cdacb Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Thu, 15 Jun 2017 16:12:58 -0700 Subject: [PATCH] Prepare for verification sync messages: receiver, ready to send FREEBIE --- js/background.js | 23 ++++++++++++++++++ js/models/conversations.js | 49 ++++++++++++++++++++++++-------------- 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/js/background.js b/js/background.js index 59a25c4a0..1c32e77be 100644 --- a/js/background.js +++ b/js/background.js @@ -119,8 +119,10 @@ messageReceiver.addEventListener('group', onGroupReceived); messageReceiver.addEventListener('sent', onSentMessage); messageReceiver.addEventListener('read', onReadReceipt); + // messageReceiver.addEventListener('verify', onVerify); messageReceiver.addEventListener('error', onError); + window.textsecure.messaging = new textsecure.MessageSender( SERVER_URL, SERVER_PORTS, USERNAME, PASSWORD ); @@ -289,6 +291,27 @@ }); } + var VERIFIED_ENUM = textsecure.storage.protocol.VerifiedStatus; + + function onVerify(ev) { + var number = ev.destination; + var key = ev.identityKey; + var verified = ev.state; + + console.log('verification sync message', number, verified); + + var contact = ConversationController.get(number); + if (!contact) { + return; + } + + if (verified === VERIFIED_ENUM.DEFAULT) { + contact.setVerifiedDefault({viaSyncMessage: true, key: key}); + } else if (verified === VERIFIED_ENUM.VERIFIED) { + contact.setVerified({viaSyncMessage: true, key: key}); + } + } + function onDeliveryReceipt(ev) { var pushMessage = ev.proto; var timestamp = pushMessage.timestamp.toNumber(); diff --git a/js/models/conversations.js b/js/models/conversations.js index 8ec147f04..28dd02360 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -84,10 +84,9 @@ }.bind(this)).then(this.onMemberVerifiedChange.bind(this)); } }, - setVerifiedDefault: function() { - function updateTrustStore() { - return Promise.resolve(); - } + setVerifiedDefault: function(options) { + options = options || {}; + _.defaults(options, {viaSyncMessage: false, key: null}); if (!this.isPrivate()) { throw new Error('You cannot verify a group conversation. ' + @@ -95,18 +94,23 @@ } var DEFAULT = this.verifiedEnum.DEFAULT; - // return textsecure.storage.protocol.setVerified(this.id, DEFAULT).then(function() { - return updateTrustStore(this.id, DEFAULT).then(function() { + // return textsecure.storage.protocol.setVerified(this.id, DEFAULT, options.key).then(function() { + function updateTrustStore() { + return Promise.resolve(); + } + return updateTrustStore(this.id, DEFAULT, options.key).then(function() { return this.save({verified: DEFAULT}); }.bind(this)).then(function() { - // TODO: send sync message? add a parameter to tell us if this is via a sync message? this.addVerifiedChange(this.id, false); + if (!options.viaSyncMessage) { + this.sendVerifySyncMessage(this.id, DEFAULT); + } }.bind(this)); }, - setVerified: function() { - function updateTrustStore() { - return Promise.resolve(); - } + setVerified: function(options) { + options = options || {}; + _.defaults(options, {viaSyncMessage: false, key: null}); + var VERIFIED = this.verifiedEnum.VERIFIED; if (!this.isPrivate()) { @@ -114,14 +118,24 @@ 'You must verify individual contacts.'); } - // return textsecure.storage.protocol.setVerified(this.id, VERIFIED).then(function() { - return updateTrustStore(this.id, VERIFIED).then(function() { + // return textsecure.storage.protocol.setVerified(this.id, VERIFIED, options.key).then(function() { + function updateTrustStore() { + return Promise.resolve(); + } + return updateTrustStore(this.id, VERIFIED, options.key).then(function() { return this.save({verified: VERIFIED}); }.bind(this)).then(function() { - // TODO: send sync message? add a parameter to tell us if this is via a sync message? this.addVerifiedChange(this.id, true); + if (!options.viaSyncMessage) { + this.sendVerifySyncMessage(this.id, VERIFIED); + } }.bind(this)); }, + sendVerifySyncMessage: function(number, state) { + // textsecure.storage.protocol.loadIdentityKey(number).then(function(key) { + // textsecure.storage.protocol.sendVerifySync(number, state, key); + // }); + }, isVerified: function() { if (this.isPrivate()) { return this.get('verified') === this.verifiedEnum.VERIFIED; @@ -171,12 +185,11 @@ } }, isUntrusted: function() { - function getFromTrustStore() { - return Promise.resolve(true); - } - if (this.isPrivate()) { // return textsecure.storage.protocol.isUntrusted(this.id); + function getFromTrustStore() { + return Promise.resolve(true); + } return getFromTrustStore(this.id); } else { if (!this.contactCollection.length) {