diff --git a/js/background.js b/js/background.js index 1e2a72553..566ff9cae 100644 --- a/js/background.js +++ b/js/background.js @@ -24,7 +24,7 @@ } else { if (textsecure.registration.isDone()) { textsecure.subscribeToPush(function(message) { - Whisper.Messages.addIncomingMessage(message).then(function() { + Whisper.Threads.addIncomingMessage(message).then(function() { console.log("Got message from " + message.pushMessage.source + "." + message.pushMessage.sourceDevice + ': "' + getString(message.message.body) + '"'); var newUnreadCount = textsecure.storage.getUnencrypted("unreadCount", 0) + 1; diff --git a/js/helpers.js b/js/helpers.js index 18e369712..8f5eed2a8 100644 --- a/js/helpers.js +++ b/js/helpers.js @@ -269,7 +269,7 @@ window.textsecure.subscribeToPush = function(message_callback) { var handleAttachment = function(attachment) { return textsecure.api.getAttachment(attachment.id.toString()).then(function(encryptedBin) { return textsecure.protocol.decryptAttachment(encryptedBin, attachment.key.toArrayBuffer()).then(function(decryptedBin) { - attachment.decrypted = decryptedBin; + attachment.data = decryptedBin; }); }); }; diff --git a/js/models/messages.js b/js/models/messages.js index dbf7e9426..0eb86ee92 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -22,45 +22,4 @@ var Whisper = Whisper || {}; this.localStorage = new Backbone.LocalStorage("Messages-" + options.threadId); } }); - - Whisper.Messages = new (Backbone.Collection.extend({ - localStorage: new Backbone.LocalStorage("Messages"), - model: Message, - comparator: 'timestamp', - - addIncomingMessage: function(decrypted) { - //TODO: The data in decrypted (from subscribeToPush) should already be cleaned up - return Promise.all(decrypted.message.attachments.map(function(a) { - return new Promise(function(resolve, reject) { - var dataView = new DataView(a.decrypted); - var blob = new Blob([dataView], { type: a.contentType }); - var FR = new FileReader(); - FR.onload = function(e) { - resolve(e.target.result); - }; - FR.onerror = reject; - FR.readAsDataURL(blob); - }); - })).then(function(base64_attachments) { - var thread = Whisper.Threads.findOrCreateForIncomingMessage(decrypted); - var timestamp = decrypted.pushMessage.timestamp.toNumber(); - var m = thread.messages().add({ - person: decrypted.pushMessage.source, - threadId: thread.id, - body: decrypted.message.body, - attachments: base64_attachments, - type: 'incoming', - timestamp: timestamp - }); - m.save(); - - if (timestamp > thread.get('timestamp')) { - thread.set('timestamp', timestamp); - } - thread.save({unreadCount: thread.get('unreadCount') + 1, active: true}); - return m; - }); - } - - }))(); })() diff --git a/js/models/threads.js b/js/models/threads.js index 05733215d..a38ae5c3f 100644 --- a/js/models/threads.js +++ b/js/models/threads.js @@ -3,6 +3,20 @@ var Whisper = Whisper || {}; (function () { 'use strict'; + function encodeAttachments (attachments) { + return Promise.all(attachments.map(function(a) { + return new Promise(function(resolve, reject) { + var dataView = new DataView(a.data); + var blob = new Blob([dataView], { type: a.contentType }); + var FR = new FileReader(); + FR.onload = function(e) { + resolve(e.target.result); + }; + FR.onerror = reject; + FR.readAsDataURL(blob); + }); + })); + }; var Thread = Backbone.Model.extend({ defaults: function() { @@ -22,20 +36,8 @@ var Whisper = Whisper || {}; }, sendMessage: function(message, attachments) { - return Promise.all(attachments.map(function(a) { - return new Promise(function(resolve, reject) { - var dataView = new DataView(a.data); - var blob = new Blob([dataView], { type: a.contentType }); - var FR = new FileReader(); - FR.onload = function(e) { - resolve(e.target.result); - }; - FR.onerror = reject; - FR.readAsDataURL(blob); - }); - })).then(function(base64_attachments) { + encodeAttachments(attachments).then(function(base64_attachments) { var timestamp = Date.now(); - this.messages().add({ type: 'outgoing', body: message, threadId: this.id, @@ -59,6 +61,28 @@ var Whisper = Whisper || {}; }); }, + receiveMessage: function(decrypted) { + var thread = this; + encodeAttachments(decrypted.message.attachments).then(function(base64_attachments) { + var timestamp = decrypted.pushMessage.timestamp.toNumber(); + var m = this.messages().add({ + person: decrypted.pushMessage.source, + threadId: this.id, + body: decrypted.message.body, + attachments: base64_attachments, + type: 'incoming', + timestamp: timestamp + }); + m.save(); + + if (timestamp > this.get('timestamp')) { + this.set('timestamp', timestamp); + } + this.save({unreadCount: this.get('unreadCount') + 1, active: true}); + return m; + }.bind(this)); + }, + messages: function() { if (!this.messageCollection) { this.messageCollection = new Whisper.MessageCollection([], {threadId: this.id}); @@ -124,7 +148,11 @@ var Whisper = Whisper || {}; }; } return this.findOrCreate(attributes); - } + }, + addIncomingMessage: function(decrypted) { + var thread = Whisper.Threads.findOrCreateForIncomingMessage(decrypted); + thread.receiveMessage(decrypted); + } }))(); })();