DRY up PushMessageContentProtobuf construction
Messages now know how to protobuf-ify themselves.
This commit is contained in:
parent
2601c3cc3a
commit
3bd559bbaa
3 changed files with 31 additions and 20 deletions
|
@ -3,29 +3,38 @@ var Whisper = Whisper || {};
|
||||||
(function () {
|
(function () {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var Message = Backbone.Model.extend();
|
var Message = Backbone.Model.extend({
|
||||||
|
toProto: function() {
|
||||||
|
return new PushMessageContentProtobuf({body: this.get('body')});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Whisper.Messages = new (Backbone.Collection.extend({
|
Whisper.Messages = new (Backbone.Collection.extend({
|
||||||
localStorage: new Backbone.LocalStorage("Messages"),
|
localStorage: new Backbone.LocalStorage("Messages"),
|
||||||
model: Message,
|
model: Message,
|
||||||
comparator: 'timestamp',
|
comparator: 'timestamp',
|
||||||
|
|
||||||
addIncomingMessage: function(decrypted) {
|
addIncomingMessage: function(decrypted) {
|
||||||
Whisper.Messages.add({
|
var m = Whisper.Messages.add({
|
||||||
person: decrypted.pushMessage.source,
|
person: decrypted.pushMessage.source,
|
||||||
group: decrypted.message.group,
|
group: decrypted.message.group,
|
||||||
body: decrypted.message.body,
|
body: decrypted.message.body,
|
||||||
type: 'incoming',
|
type: 'incoming',
|
||||||
timestamp: decrypted.message.timestamp
|
timestamp: decrypted.message.timestamp
|
||||||
}).save();
|
});
|
||||||
|
m.save();
|
||||||
|
return m;
|
||||||
},
|
},
|
||||||
|
|
||||||
addOutgoingMessage: function(messageProto, recipients) {
|
addOutgoingMessage: function(message, recipients) {
|
||||||
Whisper.Messages.add({
|
var m = Whisper.Messages.add({
|
||||||
person: recipients[0], // TODO: groups
|
person: recipients[0], // TODO: groups
|
||||||
body: messageProto.body,
|
body: message,
|
||||||
type: 'outgoing',
|
type: 'outgoing',
|
||||||
timestamp: new Date().getTime()
|
timestamp: new Date().getTime()
|
||||||
}).save();
|
});
|
||||||
|
m.save();
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
}))();
|
}))();
|
||||||
|
|
||||||
|
|
|
@ -51,10 +51,10 @@ textsecure.registerOnLoadFunction(function() {
|
||||||
alert(numberError);
|
alert(numberError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var messageProto = new PushMessageContentProtobuf();
|
var message = Whisper.Messages.addOutgoingMessage(
|
||||||
messageProto.body = $("#popup_send_text").val();
|
$("#popup_send_text").val(), numbers
|
||||||
Whisper.Messages.addOutgoingMessage(messageProto, numbers);
|
);
|
||||||
textsecure.sendMessage(numbers, messageProto,
|
textsecure.sendMessage(numbers, message.toProto(),
|
||||||
//TODO: Handle result
|
//TODO: Handle result
|
||||||
function(thing) {console.log(thing);});
|
function(thing) {console.log(thing);});
|
||||||
});
|
});
|
||||||
|
|
|
@ -81,17 +81,19 @@ var Whisper = Whisper || {};
|
||||||
$button.attr("disabled", "disabled");
|
$button.attr("disabled", "disabled");
|
||||||
$button.find('span').text("Sending");
|
$button.find('span').text("Sending");
|
||||||
|
|
||||||
var messageProto = new PushMessageContentProtobuf();
|
|
||||||
messageProto.body = $input.val();
|
|
||||||
|
|
||||||
Whisper.Messages.addOutgoingMessage(messageProto, options.recipients);
|
var message = Whisper.Messages.addOutgoingMessage(
|
||||||
|
$input.val(), options.recipients
|
||||||
|
);
|
||||||
|
|
||||||
textsecure.sendMessage(options.recipients, messageProto, function(result) {
|
textsecure.sendMessage(options.recipients, message.toProto(),
|
||||||
console.log(result);
|
function(result) {
|
||||||
$button.removeAttr("disabled");
|
console.log(result);
|
||||||
$button.find('span').text("Send");
|
$button.removeAttr("disabled");
|
||||||
$input.val("");
|
$button.find('span').text("Send");
|
||||||
});
|
$input.val("");
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue