2014-06-08 01:00:51 +00:00
|
|
|
// vim: ts=2:sw=2:expandtab:
|
2014-05-17 04:48:46 +00:00
|
|
|
var Whisper = Whisper || {};
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Thread = Backbone.Model.extend({
|
|
|
|
defaults: function() {
|
|
|
|
return {
|
|
|
|
image: '/images/default.png',
|
|
|
|
unreadCount: 0,
|
2014-06-08 01:00:51 +00:00
|
|
|
timestamp: new Date().getTime(),
|
|
|
|
active: true
|
2014-05-17 04:48:46 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
validate: function(attributes, options) {
|
2014-06-03 16:39:29 +00:00
|
|
|
var required = ['id', 'type', 'timestamp', 'image', 'name'];
|
2014-05-17 04:48:46 +00:00
|
|
|
var missing = _.filter(required, function(attr) { return !attributes[attr]; });
|
|
|
|
if (missing.length) { return "Thread must have " + missing; }
|
|
|
|
},
|
|
|
|
|
|
|
|
sendMessage: function(message) {
|
2014-06-03 16:39:29 +00:00
|
|
|
var m = Whisper.Messages.addOutgoingMessage(message, this);
|
2014-06-08 01:05:11 +00:00
|
|
|
if (this.get('type') == 'private') {
|
2014-06-03 16:39:29 +00:00
|
|
|
var promise = textsecure.messaging.sendMessageToNumber(this.get('id'), message, []);
|
2014-06-08 01:05:11 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-06-03 16:39:29 +00:00
|
|
|
var promise = textsecure.messaging.sendMessageToGroup(this.get('id'), message, []);
|
2014-06-08 01:05:11 +00:00
|
|
|
}
|
2014-06-03 16:39:29 +00:00
|
|
|
promise.then(
|
|
|
|
function(result) {
|
|
|
|
console.log(result);
|
|
|
|
}
|
|
|
|
).catch(
|
|
|
|
function(error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
);
|
2014-05-17 04:48:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
messages: function() {
|
2014-07-22 18:46:36 +00:00
|
|
|
if (!this.messageCollection) {
|
|
|
|
this.messageCollection = new Whisper.MessageCollection([], {threadId: this.id});
|
|
|
|
}
|
|
|
|
return this.messageCollection;
|
2014-05-17 04:48:46 +00: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 16:39:29 +00:00
|
|
|
findOrCreateForRecipient: function(recipient) {
|
2014-05-17 04:48:46 +00:00
|
|
|
var attributes = {};
|
2014-06-08 01:05:11 +00:00
|
|
|
attributes = {
|
|
|
|
id : recipient,
|
|
|
|
name : recipient,
|
|
|
|
type : 'private',
|
|
|
|
};
|
2014-05-17 04:48:46 +00: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);
|
|
|
|
}
|
|
|
|
}))();
|
|
|
|
})();
|