From ece266fffdafde9dfa023cf8e8b7314f11e2475b Mon Sep 17 00:00:00 2001 From: Mikunj Date: Thu, 15 Nov 2018 13:46:53 +1100 Subject: [PATCH] Added showing pow icon. --- js/background.js | 7 +++++++ js/models/conversations.js | 9 +++++++++ js/models/messages.js | 13 +++++++++++++ js/modules/loki_message_api.js | 6 +++++- libtextsecure/outgoing_message.js | 2 +- 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/js/background.js b/js/background.js index 99920f195..af12a9f45 100644 --- a/js/background.js +++ b/js/background.js @@ -562,6 +562,13 @@ appView.showFriendRequest(friendRequest); } }); + + Whisper.events.on('calculatingPoW', ({ pubKey, timestamp}) => { + try { + const conversation = ConversationController.get(pubKey); + conversation.onCalculatingPoW(pubKey, timestamp); + } catch (e) {} + }); } window.getSyncRequest = () => diff --git a/js/models/conversations.js b/js/models/conversations.js index 167057077..1d675e4fb 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -198,6 +198,15 @@ await this.inProgressFetch; removeMessage(); }, + async onCalculatingPoW(pubKey, timestamp) { + if (this.id !== pubKey) return; + + // Go through our messages and find the one that we need to update + const messages = this.messageCollection.models.filter(m => m.get('sent_at') === timestamp); + for (const message of messages) { + await message.setCalculatingPoW(); + } + }, addSingleMessage(message, setToExpire = true) { const model = this.messageCollection.add(message, { merge: true }); diff --git a/js/models/messages.js b/js/models/messages.js index e82b02d75..3b58ca934 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -93,6 +93,7 @@ return { timestamp: new Date().getTime(), attachments: [], + pow: false, }; }, validate(attributes) { @@ -439,6 +440,8 @@ if (sent || sentTo.length > 0) { return 'sent'; } + const calculatingPoW = this.get('calculatingPoW'); + if (calculatingPoW) return 'pow'; return 'sending'; }, @@ -930,7 +933,17 @@ return null; }, + async setCalculatingPoW() { + if (this.calculatingPoW) return; + this.set({ + calculatingPoW: true, + }); + + await window.Signal.Data.saveMessage(this.attributes, { + Message: Whisper.Message, + }); + }, send(promise) { this.trigger('pending'); return promise diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index af835e3cd..2224b3f20 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -46,7 +46,7 @@ class LokiServer { }); } - async sendMessage(pubKey, data, ttl) { + async sendMessage(pubKey, data, messageTimeStamp, ttl) { const data64 = dcodeIO.ByteBuffer.wrap(data).toString('base64'); // Hardcoded to use a single node/server for now const currentNode = this.nodes[0]; @@ -55,6 +55,10 @@ class LokiServer { // Nonce is returned as a base64 string to include in header let nonce; try { + window.Whisper.events.trigger('calculatingPoW', { + pubKey, + timestamp: messageTimeStamp, + }); nonce = await getPoWNonce(timestamp, ttl, pubKey, data64); } catch (err) { // Something went horribly wrong diff --git a/libtextsecure/outgoing_message.js b/libtextsecure/outgoing_message.js index 52b5129c8..fb8c949f2 100644 --- a/libtextsecure/outgoing_message.js +++ b/libtextsecure/outgoing_message.js @@ -177,7 +177,7 @@ OutgoingMessage.prototype = { async transmitMessage(number, data, timestamp, ttl = 24 * 60 * 60) { const pubKey = number; try { - const result = await this.lokiserver.sendMessage(pubKey, data, ttl); + const result = await this.lokiserver.sendMessage(pubKey, data, timestamp, ttl); return result; } catch (e) { if (e.name === 'HTTPError' && (e.code !== 409 && e.code !== 410)) {