![lilia](/assets/img/avatar_default.png)
When a thread is 'destroyed' from the UI we delete its messages and mark the thread as inactive, (in other words, keep it around as contact info). Additionally, we only load active threads when initializing the UI, and reactivate threads when new messages are added to them. Conflicts: js/models/messages.js js/models/threads.js js/views/conversations/show.js
73 lines
2.3 KiB
JavaScript
73 lines
2.3 KiB
JavaScript
var Whisper = Whisper || {};
|
|
|
|
(function () {
|
|
'use strict';
|
|
|
|
var Message = Backbone.Model.extend({
|
|
validate: function(attributes, options) {
|
|
var required = ['body', 'timestamp', 'threadId'];
|
|
var missing = _.filter(required, function(attr) { return !attributes[attr]; });
|
|
if (missing.length) { console.log("Message missing attributes: " + missing); }
|
|
},
|
|
|
|
thread: function() {
|
|
return Whisper.Threads.get(this.get('threadId'));
|
|
}
|
|
});
|
|
|
|
Whisper.MessageCollection = Backbone.Collection.extend({
|
|
model: Message,
|
|
comparator: 'timestamp',
|
|
initialize: function(models, options) {
|
|
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
|
|
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));
|
|
|
|
var thread = Whisper.Threads.findOrCreateForIncomingMessage(decrypted);
|
|
var m = thread.messages().add({
|
|
person: decrypted.pushMessage.source,
|
|
threadId: thread.id,
|
|
body: decrypted.message.body,
|
|
attachments: attachments,
|
|
type: 'incoming',
|
|
timestamp: decrypted.pushMessage.timestamp
|
|
});
|
|
m.save();
|
|
|
|
if (decrypted.message.timestamp > thread.get('timestamp')) {
|
|
thread.set('timestamp', decrypted.message.timestamp);
|
|
}
|
|
thread.set('unreadCount', thread.get('unreadCount') + 1);
|
|
thread.set('active', true);
|
|
thread.save();
|
|
return m;
|
|
},
|
|
|
|
addOutgoingMessage: function(message, thread) {
|
|
var m = thread.messages().add({
|
|
threadId: thread.id,
|
|
body: message,
|
|
type: 'outgoing',
|
|
timestamp: new Date().getTime()
|
|
});
|
|
m.save();
|
|
|
|
thread.set('timestamp', new Date().getTime());
|
|
thread.set('unreadCount', 0);
|
|
thread.set('active', true);
|
|
thread.save();
|
|
return m;
|
|
}
|
|
}))();
|
|
})()
|