2014-06-08 01:00:51 +00:00
|
|
|
// vim: ts=2:sw=2:expandtab:
|
2014-05-17 04:48:46 +00:00
|
|
|
|
|
|
|
(function () {
|
|
|
|
'use strict';
|
2014-11-13 05:46:57 +00:00
|
|
|
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
|
2014-11-13 00:48:28 +00:00
|
|
|
function encodeAttachments (attachments) {
|
|
|
|
return Promise.all(attachments.map(function(a) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
var dataView = new DataView(a.data);
|
|
|
|
var blob = new Blob([dataView], { type: a.contentType });
|
|
|
|
var FR = new FileReader();
|
|
|
|
FR.onload = function(e) {
|
|
|
|
resolve(e.target.result);
|
|
|
|
};
|
|
|
|
FR.onerror = reject;
|
|
|
|
FR.readAsDataURL(blob);
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
};
|
2014-05-17 04:48:46 +00:00
|
|
|
|
|
|
|
var Thread = Backbone.Model.extend({
|
|
|
|
defaults: function() {
|
|
|
|
return {
|
2014-11-13 05:46:57 +00:00
|
|
|
name: 'New Conversation',
|
2014-05-17 04:48:46 +00:00
|
|
|
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-10-16 21:11:50 +00:00
|
|
|
var required = ['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; }
|
|
|
|
},
|
|
|
|
|
2014-10-25 01:44:30 +00:00
|
|
|
sendMessage: function(message, attachments) {
|
2014-11-13 00:48:28 +00:00
|
|
|
encodeAttachments(attachments).then(function(base64_attachments) {
|
2014-11-02 21:48:35 +00:00
|
|
|
var timestamp = Date.now();
|
|
|
|
this.messages().add({ type: 'outgoing',
|
|
|
|
body: message,
|
|
|
|
threadId: this.id,
|
|
|
|
attachments: base64_attachments,
|
|
|
|
timestamp: timestamp }).save();
|
2014-10-26 07:29:01 +00:00
|
|
|
|
2014-11-02 21:48:35 +00:00
|
|
|
this.save({ timestamp: timestamp,
|
|
|
|
unreadCount: 0,
|
|
|
|
active: true});
|
2014-08-11 06:34:29 +00:00
|
|
|
|
2014-11-02 21:48:35 +00:00
|
|
|
if (this.get('type') == 'private') {
|
|
|
|
return textsecure.messaging.sendMessageToNumber(this.get('id'), message, attachments);
|
2014-06-03 16:39:29 +00:00
|
|
|
}
|
2014-11-02 21:48:35 +00:00
|
|
|
else {
|
|
|
|
return textsecure.messaging.sendMessageToGroup(this.get('groupId'), message, attachments);
|
2014-06-03 16:39:29 +00:00
|
|
|
}
|
2014-11-02 21:48:35 +00:00
|
|
|
}.bind(this)).then(function(result) {
|
|
|
|
console.log(result);
|
|
|
|
}).catch(function(error) {
|
|
|
|
console.log(error);
|
|
|
|
});
|
2014-05-17 04:48:46 +00:00
|
|
|
},
|
|
|
|
|
2014-11-13 00:48:28 +00:00
|
|
|
receiveMessage: function(decrypted) {
|
|
|
|
var thread = this;
|
|
|
|
encodeAttachments(decrypted.message.attachments).then(function(base64_attachments) {
|
|
|
|
var timestamp = decrypted.pushMessage.timestamp.toNumber();
|
|
|
|
var m = this.messages().add({
|
|
|
|
person: decrypted.pushMessage.source,
|
|
|
|
threadId: this.id,
|
|
|
|
body: decrypted.message.body,
|
|
|
|
attachments: base64_attachments,
|
|
|
|
type: 'incoming',
|
|
|
|
timestamp: timestamp
|
|
|
|
});
|
|
|
|
m.save();
|
|
|
|
|
|
|
|
if (timestamp > this.get('timestamp')) {
|
|
|
|
this.set('timestamp', timestamp);
|
|
|
|
}
|
|
|
|
this.save({unreadCount: this.get('unreadCount') + 1, active: true});
|
|
|
|
return m;
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
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,
|
2014-10-18 14:08:57 +00:00
|
|
|
|
|
|
|
comparator: function(m) {
|
|
|
|
return -m.get('timestamp');
|
|
|
|
},
|
|
|
|
|
2014-05-17 04:48:46 +00:00
|
|
|
findOrCreate: function(attributes) {
|
|
|
|
var thread = Whisper.Threads.add(attributes, {merge: true});
|
|
|
|
thread.save();
|
|
|
|
return thread;
|
|
|
|
},
|
|
|
|
|
2014-08-11 06:34:29 +00:00
|
|
|
createGroup: function(recipients, name) {
|
|
|
|
var attributes = {};
|
|
|
|
attributes = {
|
|
|
|
name : name,
|
2014-10-16 21:14:00 +00:00
|
|
|
numbers : recipients,
|
2014-08-11 06:34:29 +00:00
|
|
|
type : 'group',
|
|
|
|
};
|
2014-10-16 21:14:00 +00:00
|
|
|
var thread = this.findOrCreate(attributes);
|
|
|
|
return textsecure.messaging.createGroup(recipients, name).then(function(groupId) {
|
2014-11-13 05:46:57 +00:00
|
|
|
thread.save({
|
|
|
|
id : getString(groupId),
|
|
|
|
groupId : getString(groupId)
|
|
|
|
});
|
2014-10-16 21:14:00 +00:00
|
|
|
return thread;
|
|
|
|
});
|
2014-08-11 06:34:29 +00:00
|
|
|
},
|
|
|
|
|
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 = {
|
2014-10-17 21:32:37 +00:00
|
|
|
id : decrypted.message.group.id,
|
2014-10-17 00:50:36 +00:00
|
|
|
groupId : decrypted.message.group.id,
|
2014-10-17 20:14:09 +00:00
|
|
|
name : decrypted.message.group.name || 'New group',
|
2014-05-17 04:48:46 +00:00
|
|
|
type : 'group',
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
attributes = {
|
|
|
|
id : decrypted.pushMessage.source,
|
|
|
|
name : decrypted.pushMessage.source,
|
|
|
|
type : 'private'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return this.findOrCreate(attributes);
|
2014-11-13 00:48:28 +00:00
|
|
|
},
|
2014-10-18 14:08:57 +00:00
|
|
|
|
2014-11-13 00:48:28 +00:00
|
|
|
addIncomingMessage: function(decrypted) {
|
|
|
|
var thread = Whisper.Threads.findOrCreateForIncomingMessage(decrypted);
|
|
|
|
thread.receiveMessage(decrypted);
|
|
|
|
}
|
2014-05-17 04:48:46 +00:00
|
|
|
}))();
|
|
|
|
})();
|