2014-05-11 17:13:09 -07:00
|
|
|
var Whisper = Whisper || {};
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
2014-05-18 14:26:55 -07:00
|
|
|
var Message = Backbone.Model.extend({
|
2014-05-16 21:48:46 -07:00
|
|
|
validate: function(attributes, options) {
|
|
|
|
var required = ['body', 'timestamp', 'threadId'];
|
|
|
|
var missing = _.filter(required, function(attr) { return !attributes[attr]; });
|
2014-06-03 19:37:10 -07:00
|
|
|
if (missing.length) { console.log("Message missing attributes: " + missing); }
|
2014-05-16 21:48:46 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
thread: function() {
|
|
|
|
return Whisper.Threads.get(this.get('threadId'));
|
2014-05-18 14:26:55 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-07-22 04:14:33 -10:00
|
|
|
Whisper.MessageCollection = Backbone.Collection.extend({
|
|
|
|
model: Message,
|
|
|
|
comparator: 'timestamp',
|
|
|
|
initialize: function(models, options) {
|
|
|
|
this.localStorage = new Backbone.LocalStorage("Messages-" + options.threadId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-05-11 17:13:09 -07:00
|
|
|
Whisper.Messages = new (Backbone.Collection.extend({
|
|
|
|
localStorage: new Backbone.LocalStorage("Messages"),
|
|
|
|
model: Message,
|
|
|
|
comparator: 'timestamp',
|
|
|
|
|
|
|
|
addIncomingMessage: function(decrypted) {
|
2014-05-19 03:06:28 -04: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-16 21:48:46 -07:00
|
|
|
var thread = Whisper.Threads.findOrCreateForIncomingMessage(decrypted);
|
2014-07-22 04:14:33 -10:00
|
|
|
var m = thread.messages().add({
|
2014-05-18 13:36:56 -07:00
|
|
|
person: decrypted.pushMessage.source,
|
2014-05-16 21:48:46 -07:00
|
|
|
threadId: thread.id,
|
2014-05-11 17:13:09 -07:00
|
|
|
body: decrypted.message.body,
|
2014-05-19 03:06:28 -04:00
|
|
|
attachments: attachments,
|
2014-05-11 17:13:09 -07:00
|
|
|
type: 'incoming',
|
2014-06-03 19:37:10 -07:00
|
|
|
timestamp: decrypted.pushMessage.timestamp
|
2014-05-18 14:26:55 -07:00
|
|
|
});
|
|
|
|
m.save();
|
2014-05-16 21:48:46 -07:00
|
|
|
|
|
|
|
if (decrypted.message.timestamp > thread.get('timestamp')) {
|
2014-06-07 18:00:51 -07:00
|
|
|
thread.set('timestamp', decrypted.message.timestamp);
|
2014-05-16 21:48:46 -07:00
|
|
|
}
|
2014-08-10 23:34:29 -07:00
|
|
|
thread.save({unreadCount: thread.get('unreadCount') + 1, active: true});
|
2014-05-18 14:26:55 -07:00
|
|
|
return m;
|
2014-05-11 17:13:09 -07:00
|
|
|
}
|
2014-08-10 23:34:29 -07:00
|
|
|
|
2014-05-11 17:13:09 -07:00
|
|
|
}))();
|
|
|
|
})()
|