Merge Whisper.Messages into Whisper Threads

Eliminates the global Whisper.Messages object and consolidates shared
send/receive logic in Whisper.Threads.

To the latter end, note that the decrypted array buffer on an attachment
pointer is now named data instead of decrypted, in order to match the
format of outgoing attachments presented by
FileReader.readAsArrayBuffers and let us use the same handler to base64
encode them.
This commit is contained in:
lilia 2014-11-12 16:48:28 -08:00
parent 5a0e199fc5
commit 735737f0bc
4 changed files with 44 additions and 57 deletions

View file

@ -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;
});
}
}))();
})()