signal-desktop/js/models/messages.js

48 lines
1.4 KiB
JavaScript
Raw Normal View History

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