2014-05-17 06:48:46 +02:00
|
|
|
var Whisper = Whisper || {};
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Thread = Backbone.Model.extend({
|
|
|
|
defaults: function() {
|
|
|
|
return {
|
|
|
|
image: '/images/default.png',
|
|
|
|
unreadCount: 0,
|
|
|
|
timestamp: new Date().getTime()
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
validate: function(attributes, options) {
|
2014-06-03 18:39:29 +02:00
|
|
|
var required = ['id', 'type', 'timestamp', 'image', 'name'];
|
2014-05-17 06:48:46 +02:00
|
|
|
var missing = _.filter(required, function(attr) { return !attributes[attr]; });
|
|
|
|
if (missing.length) { return "Thread must have " + missing; }
|
|
|
|
},
|
|
|
|
|
|
|
|
sendMessage: function(message) {
|
2014-06-03 18:39:29 +02:00
|
|
|
var m = Whisper.Messages.addOutgoingMessage(message, this);
|
|
|
|
if (this.get('type') == 'private')
|
|
|
|
var promise = textsecure.messaging.sendMessageToNumber(this.get('id'), message, []);
|
|
|
|
else
|
|
|
|
var promise = textsecure.messaging.sendMessageToGroup(this.get('id'), message, []);
|
|
|
|
promise.then(
|
|
|
|
function(result) {
|
|
|
|
console.log(result);
|
|
|
|
}
|
|
|
|
).catch(
|
|
|
|
function(error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
);
|
2014-05-17 06:48:46 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
messages: function() {
|
2014-07-22 16:14:33 +02:00
|
|
|
var messages = new Whisper.MessageCollection([], {threadId: this.id});
|
|
|
|
messages.fetch();
|
|
|
|
return messages;
|
2014-05-17 06:48:46 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Whisper.Threads = new (Backbone.Collection.extend({
|
|
|
|
localStorage: new Backbone.LocalStorage("Threads"),
|
|
|
|
model: Thread,
|
|
|
|
comparator: 'timestamp',
|
|
|
|
findOrCreate: function(attributes) {
|
|
|
|
var thread = Whisper.Threads.add(attributes, {merge: true});
|
|
|
|
thread.save();
|
|
|
|
return thread;
|
|
|
|
},
|
|
|
|
|
2014-06-03 18:39:29 +02:00
|
|
|
findOrCreateForRecipient: function(recipient) {
|
2014-05-17 06:48:46 +02:00
|
|
|
var attributes = {};
|
2014-06-03 18:39:29 +02:00
|
|
|
attributes = {
|
|
|
|
id : recipient,
|
|
|
|
name : recipient,
|
|
|
|
type : 'private',
|
|
|
|
};
|
2014-05-17 06:48:46 +02:00
|
|
|
return this.findOrCreate(attributes);
|
|
|
|
},
|
|
|
|
|
|
|
|
findOrCreateForIncomingMessage: function(decrypted) {
|
|
|
|
var attributes = {};
|
|
|
|
if (decrypted.message.group) {
|
|
|
|
attributes = {
|
|
|
|
id : decrypted.message.group.id,
|
|
|
|
name : decrypted.message.group.name,
|
|
|
|
type : 'group',
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
attributes = {
|
|
|
|
id : decrypted.pushMessage.source,
|
|
|
|
name : decrypted.pushMessage.source,
|
|
|
|
type : 'private'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return this.findOrCreate(attributes);
|
|
|
|
}
|
|
|
|
}))();
|
|
|
|
})();
|