From 3bd559bbaa6bf298f389da289fa7e7f0f6705518 Mon Sep 17 00:00:00 2001 From: lilia Date: Sun, 18 May 2014 14:26:55 -0700 Subject: [PATCH] DRY up PushMessageContentProtobuf construction Messages now know how to protobuf-ify themselves. --- js/models/messages.js | 23 ++++++++++++++++------- js/popup.js | 8 ++++---- js/views/messages.js | 20 +++++++++++--------- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/js/models/messages.js b/js/models/messages.js index 31dfcf77..32876a96 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -3,29 +3,38 @@ var Whisper = Whisper || {}; (function () { '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({ localStorage: new Backbone.LocalStorage("Messages"), model: Message, comparator: 'timestamp', addIncomingMessage: function(decrypted) { - Whisper.Messages.add({ + var m = Whisper.Messages.add({ person: decrypted.pushMessage.source, group: decrypted.message.group, body: decrypted.message.body, type: 'incoming', timestamp: decrypted.message.timestamp - }).save(); + }); + m.save(); + return m; }, - addOutgoingMessage: function(messageProto, recipients) { - Whisper.Messages.add({ + addOutgoingMessage: function(message, recipients) { + var m = Whisper.Messages.add({ person: recipients[0], // TODO: groups - body: messageProto.body, + body: message, type: 'outgoing', timestamp: new Date().getTime() - }).save(); + }); + m.save(); + return m; } }))(); diff --git a/js/popup.js b/js/popup.js index 3e8fafe7..45f1ecf9 100644 --- a/js/popup.js +++ b/js/popup.js @@ -51,10 +51,10 @@ textsecure.registerOnLoadFunction(function() { alert(numberError); } } - var messageProto = new PushMessageContentProtobuf(); - messageProto.body = $("#popup_send_text").val(); - Whisper.Messages.addOutgoingMessage(messageProto, numbers); - textsecure.sendMessage(numbers, messageProto, + var message = Whisper.Messages.addOutgoingMessage( + $("#popup_send_text").val(), numbers + ); + textsecure.sendMessage(numbers, message.toProto(), //TODO: Handle result function(thing) {console.log(thing);}); }); diff --git a/js/views/messages.js b/js/views/messages.js index 4c71a8cd..0af6bbe5 100644 --- a/js/views/messages.js +++ b/js/views/messages.js @@ -81,17 +81,19 @@ var Whisper = Whisper || {}; $button.attr("disabled", "disabled"); $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) { - console.log(result); - $button.removeAttr("disabled"); - $button.find('span').text("Send"); - $input.val(""); - }); + textsecure.sendMessage(options.recipients, message.toProto(), + function(result) { + console.log(result); + $button.removeAttr("disabled"); + $button.find('span').text("Send"); + $input.val(""); + } + ); }); },