2014-05-12 02:13:09 +02:00
|
|
|
var Whisper = Whisper || {};
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
2014-05-18 23:26:55 +02:00
|
|
|
var Message = Backbone.Model.extend({
|
2014-05-17 06:48:46 +02:00
|
|
|
validate: function(attributes, options) {
|
|
|
|
var required = ['body', 'timestamp', 'threadId'];
|
|
|
|
var missing = _.filter(required, function(attr) { return !attributes[attr]; });
|
2014-06-04 04:37:10 +02:00
|
|
|
if (missing.length) { console.log("Message missing attributes: " + missing); }
|
2014-05-17 06:48:46 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
thread: function() {
|
|
|
|
return Whisper.Threads.get(this.get('threadId'));
|
2014-05-18 23:26:55 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-05-12 02:13:09 +02:00
|
|
|
Whisper.Messages = new (Backbone.Collection.extend({
|
|
|
|
localStorage: new Backbone.LocalStorage("Messages"),
|
|
|
|
model: Message,
|
|
|
|
comparator: 'timestamp',
|
|
|
|
|
|
|
|
addIncomingMessage: function(decrypted) {
|
2014-05-19 09:06:28 +02:00
|
|
|
//TODO: The data in decrypted (from subscribeToPush) should already be cleaned up
|
|
|
|
var attachments = [];
|
|
|
|
for (var i = 0; i < decrypted.message.attachments.length; i++)
|
|
|
|
attachments[i] = "data:" + decrypted.message.attachments[i].contentType + ";base64," + btoa(getString(decrypted.message.attachments[i].decrypted));
|
|
|
|
|
2014-05-17 06:48:46 +02:00
|
|
|
var thread = Whisper.Threads.findOrCreateForIncomingMessage(decrypted);
|
2014-05-18 23:26:55 +02:00
|
|
|
var m = Whisper.Messages.add({
|
2014-05-18 22:36:56 +02:00
|
|
|
person: decrypted.pushMessage.source,
|
2014-05-17 06:48:46 +02:00
|
|
|
threadId: thread.id,
|
2014-05-12 02:13:09 +02:00
|
|
|
body: decrypted.message.body,
|
2014-05-19 09:06:28 +02:00
|
|
|
attachments: attachments,
|
2014-05-12 02:13:09 +02:00
|
|
|
type: 'incoming',
|
2014-06-04 04:37:10 +02:00
|
|
|
timestamp: decrypted.pushMessage.timestamp
|
2014-05-18 23:26:55 +02:00
|
|
|
});
|
|
|
|
m.save();
|
2014-05-17 06:48:46 +02:00
|
|
|
|
|
|
|
if (decrypted.message.timestamp > thread.get('timestamp')) {
|
|
|
|
thread.set('timestamp', decrypted.message.timestamp);
|
|
|
|
thread.set('unreadCount', thread.get('unreadCount') + 1);
|
|
|
|
thread.save();
|
|
|
|
}
|
2014-05-18 23:26:55 +02:00
|
|
|
return m;
|
2014-05-12 02:13:09 +02:00
|
|
|
},
|
|
|
|
|
2014-05-17 06:48:46 +02:00
|
|
|
addOutgoingMessage: function(message, thread) {
|
2014-05-18 23:26:55 +02:00
|
|
|
var m = Whisper.Messages.add({
|
2014-05-17 06:48:46 +02:00
|
|
|
threadId: thread.id,
|
2014-05-18 23:26:55 +02:00
|
|
|
body: message,
|
2014-05-12 02:13:09 +02:00
|
|
|
type: 'outgoing',
|
|
|
|
timestamp: new Date().getTime()
|
2014-05-18 23:26:55 +02:00
|
|
|
});
|
|
|
|
m.save();
|
|
|
|
return m;
|
2014-05-12 02:13:09 +02:00
|
|
|
}
|
|
|
|
}))();
|
|
|
|
})()
|