2014-05-12 00:13:09 +00:00
|
|
|
var Whisper = Whisper || {};
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
2014-05-18 21:26:55 +00:00
|
|
|
var Message = Backbone.Model.extend({
|
|
|
|
toProto: function() {
|
2014-05-21 19:04:05 +00:00
|
|
|
return new textsecure.protos.PushMessageContentProtobuf({body: this.get('body')});
|
2014-05-18 21:26:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-05-12 00:13:09 +00:00
|
|
|
Whisper.Messages = new (Backbone.Collection.extend({
|
|
|
|
localStorage: new Backbone.LocalStorage("Messages"),
|
|
|
|
model: Message,
|
|
|
|
comparator: 'timestamp',
|
|
|
|
|
|
|
|
addIncomingMessage: function(decrypted) {
|
2014-05-19 07:06:28 +00: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-18 21:26:55 +00:00
|
|
|
var m = Whisper.Messages.add({
|
2014-05-18 20:36:56 +00:00
|
|
|
person: decrypted.pushMessage.source,
|
2014-05-12 00:13:09 +00:00
|
|
|
group: decrypted.message.group,
|
|
|
|
body: decrypted.message.body,
|
2014-05-19 07:06:28 +00:00
|
|
|
attachments: attachments,
|
2014-05-12 00:13:09 +00:00
|
|
|
type: 'incoming',
|
|
|
|
timestamp: decrypted.message.timestamp
|
2014-05-18 21:26:55 +00:00
|
|
|
});
|
|
|
|
m.save();
|
|
|
|
return m;
|
2014-05-12 00:13:09 +00:00
|
|
|
},
|
|
|
|
|
2014-05-18 21:26:55 +00:00
|
|
|
addOutgoingMessage: function(message, recipients) {
|
|
|
|
var m = Whisper.Messages.add({
|
2014-05-18 20:36:56 +00:00
|
|
|
person: recipients[0], // TODO: groups
|
2014-05-18 21:26:55 +00:00
|
|
|
body: message,
|
2014-05-12 00:13:09 +00:00
|
|
|
type: 'outgoing',
|
|
|
|
timestamp: new Date().getTime()
|
2014-05-18 21:26:55 +00:00
|
|
|
});
|
|
|
|
m.save();
|
|
|
|
return m;
|
2014-05-12 00:13:09 +00:00
|
|
|
}
|
|
|
|
}))();
|
|
|
|
|
|
|
|
})()
|