From dba6a36e830306ae80aca522e79fc90d03cbbd0c Mon Sep 17 00:00:00 2001 From: Mikunj Date: Wed, 21 Nov 2018 11:48:10 +1100 Subject: [PATCH] removed old unused code. --- js/background.js | 8 +-- js/models/conversations.js | 99 +++++-------------------------- js/models/messages.js | 11 +++- js/views/app_view.js | 10 ---- libloki/proof-of-work.js | 2 +- libtextsecure/message_receiver.js | 33 +++-------- 6 files changed, 33 insertions(+), 130 deletions(-) diff --git a/js/background.js b/js/background.js index 179affa15..b29be0b31 100644 --- a/js/background.js +++ b/js/background.js @@ -570,12 +570,6 @@ } }); - Whisper.events.on('showFriendRequest', friendRequest => { - if (appView) { - appView.showFriendRequest(friendRequest); - } - }); - Whisper.events.on('calculatingPoW', ({ pubKey, timestamp }) => { try { const conversation = ConversationController.get(pubKey); @@ -1268,6 +1262,7 @@ unidentifiedDeliveryReceived: data.unidentifiedDeliveryReceived, type: 'incoming', unread: 1, + preKeyBundle: data.preKeyBundle || null, }; if (data.type === 'friend-request') { @@ -1275,7 +1270,6 @@ ...messageData, type: 'friend-request', friendStatus: 'pending', - preKeyBundle: data.preKeyBundle || null, direction: 'incoming', } } diff --git a/js/models/conversations.js b/js/models/conversations.js index b4e56afac..005905f78 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -668,91 +668,6 @@ existing.trigger('destroy'); } }, - // This will add a message which will allow the user to reply to a friend request - async addFriendRequest(body, options = {}) { - const _options = { - friendStatus: 'pending', - direction: 'incoming', - preKeyBundle: null, - timestamp: null, - source: null, - sourceDevice: null, - received_at: null, - ...options, - }; - - if (this.isMe()) { - window.log.info( - 'refusing to send friend request to ourselves' - ); - return; - } - - const timestamp = _options.timestamp || this.get('timestamp') || Date.now(); - - window.log.info( - 'adding friend request for', - this.ourNumber, - this.idForLogging(), - timestamp - ); - - this.lastMessageStatus = 'sending'; - - this.set({ - active_at: Date.now(), - timestamp: Date.now(), - unreadCount: this.get('unreadCount') + 1, - }); - - await window.Signal.Data.updateConversation(this.id, this.attributes, { - Conversation: Whisper.Conversation, - }); - - // If we need to add new incoming friend requests - // Then we need to make sure we remove any pending requests that we may have - // This is to ensure that one user cannot spam us with multiple friend requests - if (_options.direction === 'incoming') { - const requests = await this.getPendingFriendRequests('incoming'); - - // Delete the old message if it's pending - await Promise.all(requests.map(request => this._removeMessage(request.id))); - // Trigger an update if we removed messages - if (requests.length > 0) - this.trigger('change'); - } - - // Add the new message - // eslint-disable-next-line camelcase - const received_at = _options.received_at || Date.now(); - const message = { - conversationId: this.id, - type: 'friend-request', - sent_at: timestamp, - received_at, - unread: 1, - from: this.id, - to: this.ourNumber, - friendStatus: _options.friendStatus, - direction: _options.direction, - body, - preKeyBundle: _options.preKeyBundle, - source: _options.source, - sourceDevice: _options.sourceDevice, - }; - - const id = await window.Signal.Data.saveMessage(message, { - Message: Whisper.Message, - }); - - const whisperMessage = new Whisper.Message({ - ...message, - id, - }); - - this.trigger('newmessage', whisperMessage); - this.notify(whisperMessage); - }, async addVerifiedChange(verifiedChangeId, verified, providedOptions) { const options = providedOptions || {}; _.defaults(options, { local: true }); @@ -1333,6 +1248,20 @@ this.changed = {}; this.set(lastMessageUpdate); + // If we need to add new incoming friend requests + // Then we need to make sure we remove any pending requests that we may have + // This is to ensure that one user cannot spam us with multiple friend requests + if (lastMessage.isFriendRequest() && lastMessage.direction === 'incoming') { + const requests = await this.getPendingFriendRequests('incoming'); + + // Delete the old message if it's pending + await Promise.all(requests.map(request => this._removeMessage(request.id))); + + // Trigger an update if we removed messages + hasChanged = hasChanged || (requests.length > 0); + } + + if (this.hasChanged()) { await window.Signal.Data.updateConversation(this.id, this.attributes, { Conversation: Whisper.Conversation, diff --git a/js/models/messages.js b/js/models/messages.js index d6d1fd71e..1d5ee7386 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -1227,7 +1227,16 @@ hasVisualMediaAttachments: dataMessage.hasVisualMediaAttachments, quote: dataMessage.quote, schemaVersion: dataMessage.schemaVersion, + preKeyBundle: dataMessage.preKeyBundle || null, }); + + if (type === 'friend-request') { + message.set({ + friendStatus: dataMessage.friendStatus, + direction: dataMessage.direction, + }); + } + if (type === 'outgoing') { const receipts = Whisper.DeliveryReceipts.forMessage( conversation, @@ -1291,7 +1300,7 @@ ); } } - if (type === 'incoming') { + if (type === 'incoming' || type === 'friend-request') { const readSync = Whisper.ReadSyncs.forMessage(message); if (readSync) { if ( diff --git a/js/views/app_view.js b/js/views/app_view.js index bcf325768..1db6314e3 100644 --- a/js/views/app_view.js +++ b/js/views/app_view.js @@ -178,15 +178,5 @@ }); } }, - async showFriendRequest({ pubKey, message, preKeyBundle, options }) { - const controller = window.ConversationController; - const conversation = await controller.getOrCreateAndWait(pubKey, 'private'); - if (conversation) { - conversation.addFriendRequest(message, { - preKeyBundle: preKeyBundle || null, - ...options, - }); - } - }, }); })(); diff --git a/libloki/proof-of-work.js b/libloki/proof-of-work.js index 2b4579d32..3adab32b2 100644 --- a/libloki/proof-of-work.js +++ b/libloki/proof-of-work.js @@ -4,7 +4,7 @@ const { BigInteger } = require('jsbn'); const NONCE_LEN = 8; // Modify this value for difficulty scaling -const NONCE_TRIALS = 10; +const NONCE_TRIALS = 1000; // Increment Uint8Array nonce by 1 with carrying function incrementNonce(nonce) { diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index f7d14f071..bec5faed1 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -541,7 +541,7 @@ MessageReceiver.prototype.extend({ return textsecure.storage.unprocessed.add(data); }, async updateCache(envelope, plaintext) { - const { id, preKeyBundleMessage } = envelope; + const { id } = envelope; const item = await textsecure.storage.unprocessed.get(id); if (!item) { window.log.error( @@ -556,7 +556,6 @@ MessageReceiver.prototype.extend({ sourceDevice: envelope.sourceDevice, serverTimestamp: envelope.serverTimestamp, decrypted: await MessageReceiver.arrayBufferToStringBase64(plaintext), - preKeyBundleMessage: await MessageReceiver.arrayBufferToStringBase64(preKeyBundleMessage), }); } else { item.set({ @@ -564,7 +563,6 @@ MessageReceiver.prototype.extend({ sourceDevice: envelope.sourceDevice, serverTimestamp: envelope.serverTimestamp, decrypted: await MessageReceiver.arrayBufferToString(plaintext), - preKeyBundleMessage: await MessageReceiver.arrayBufferToStringBase64(preKeyBundleMessage), }); } @@ -717,9 +715,11 @@ MessageReceiver.prototype.extend({ if (envelope.preKeyBundleMessage) { const decryptedText = await fallBackSessionCipher.decrypt(envelope.preKeyBundleMessage.toArrayBuffer()); const unpadded = await this.unpad(decryptedText); + const decodedProto = textsecure.protobuf.PreKeyBundleMessage.decode(unpadded); + const decodedBundle = this.decodePreKeyBundleMessage(decodedProto); // eslint-disable-next-line no-param-reassign - envelope.preKeyBundleMessage = unpadded; + envelope.preKeyBundleMessage = decodedBundle; // Save the preKey bundle if this is not a friend request. // We don't automatically save on a friend request because @@ -960,8 +960,6 @@ MessageReceiver.prototype.extend({ return this.removeFromCache(envelope); } - const preKeyBundle = envelope.preKeyBundleMessage && this.decodePreKeyBundleMessage(envelope.preKeyBundleMessage); - const ev = new Event('message'); ev.confirm = this.removeFromCache.bind(this, envelope); ev.data = { @@ -972,7 +970,7 @@ MessageReceiver.prototype.extend({ receivedAt: envelope.receivedAt, unidentifiedDeliveryReceived: envelope.unidentifiedDeliveryReceived, message, - preKeyBundle: preKeyBundle || null, + preKeyBundle: envelope.preKeyBundleMessage || null, }; return this.dispatchAndWait(ev); }) @@ -1000,19 +998,6 @@ MessageReceiver.prototype.extend({ return this.innerHandleContentMessage(envelope, plaintext); }); }, - promptUserToAcceptFriendRequest(envelope, message, preKeyBundleMessage) { - window.Whisper.events.trigger('showFriendRequest', { - pubKey: envelope.source, - message, - preKeyBundle: this.decodePreKeyBundleMessage(preKeyBundleMessage), - options: { - source: envelope.source, - sourceDevice: envelope.sourceDevice, - timestamp: envelope.timestamp.toNumber(), - receivedAt: envelope.receivedAt, - }, - }); - }, // A handler function for when a friend request is accepted or declined async onFriendRequestUpdate(pubKey, message) { if (!message || !message.direction || !message.friendStatus) return; @@ -1046,12 +1031,6 @@ MessageReceiver.prototype.extend({ }, async innerHandleContentMessage(envelope, plaintext) { const content = textsecure.protobuf.Content.decode(plaintext); - const preKeyBundleMessage = envelope.preKeyBundleMessage && textsecure.protobuf.PreKeyBundleMessage.decode(envelope.preKeyBundleMessage); - - // Set the decoded preKeyMessage - if (preKeyBundleMessage) { - envelope.preKeyBundleMessage = preKeyBundleMessage; - } let conversation; try { @@ -1300,6 +1279,8 @@ MessageReceiver.prototype.extend({ }; }, async handlePreKeyBundleMessage(pubKey, preKeyBundleMessage) { + if (!preKeyBundleMessage) return null; + const { preKeyId, signedKeyId,