From a8f4bac2f728e4f50aa6a9d49cce9fce6af005c6 Mon Sep 17 00:00:00 2001 From: lilia Date: Tue, 22 Sep 2015 15:55:47 -0700 Subject: [PATCH] Let makeAttachmentPointer resolve falsey arguments Internalizing this pattern to the makeAttachmentPointer function lets us DRY it up throughout the file. // FREEBIE --- js/libtextsecure.js | 43 ++++++++++++------------------------ libtextsecure/sendmessage.js | 43 ++++++++++++------------------------ 2 files changed, 28 insertions(+), 58 deletions(-) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index 6aaeb6d8c54c..cbae0710831d 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -39634,6 +39634,9 @@ window.textsecure.MessageSender = function(url, username, password) { } function makeAttachmentPointer(attachment) { + if (typeof attachment !== 'object' || attachment == null) { + return Promise.resolve(undefined); + } var proto = new textsecure.protobuf.AttachmentPointer(); proto.key = textsecure.crypto.getRandomBytes(64); @@ -39717,9 +39720,9 @@ window.textsecure.MessageSender = function(url, username, password) { if (updateDevices === undefined) { return TextSecureServer.getKeysForNumber(number).then(handleResult); } else { - var promises = []; - for (var i in updateDevices) - promises[promises.length] = TextSecureServer.getKeysForNumber(number, updateDevices[i]).then(handleResult); + var promises = updateDevices.map(function(device) { + return TextSecureServer.getKeysForNumber(number, device).then(handleResult); + }); return Promise.all(promises); } @@ -39860,10 +39863,7 @@ window.textsecure.MessageSender = function(url, username, password) { var proto = new textsecure.protobuf.DataMessage(); proto.body = messageText; - var promises = []; - for (var i in attachments) - promises.push(makeAttachmentPointer(attachments[i])); - return Promise.all(promises).then(function(attachmentsArray) { + return Promise.all(attachments.map(makeAttachmentPointer)).then(function(attachmentsArray) { proto.attachments = attachmentsArray; return sendIndividualProto(number, proto, timestamp).then(function() { return sendSyncMessage(proto, timestamp, number); @@ -39897,10 +39897,7 @@ window.textsecure.MessageSender = function(url, username, password) { if (numbers === undefined) return Promise.reject(new Error("Unknown Group")); - var promises = []; - for (var i in attachments) - promises.push(makeAttachmentPointer(attachments[i])); - return Promise.all(promises).then(function(attachmentsArray) { + return Promise.all(attachments.map(makeAttachmentPointer)).then(function(attachmentsArray) { proto.attachments = attachmentsArray; return sendGroupProto(numbers, proto, timestamp); }); @@ -39919,18 +39916,12 @@ window.textsecure.MessageSender = function(url, username, password) { proto.group.members = numbers; proto.group.name = name; - if (avatar !== undefined) { - return makeAttachmentPointer(avatar).then(function(attachment) { - proto.group.avatar = attachment; - return sendGroupProto(numbers, proto).then(function() { - return proto.group.id; - }); - }); - } else { + return makeAttachmentPointer(avatar).then(function(attachment) { + proto.group.avatar = attachment; return sendGroupProto(numbers, proto).then(function() { return proto.group.id; }); - } + }); }); } @@ -39948,18 +39939,12 @@ window.textsecure.MessageSender = function(url, username, password) { } proto.group.members = numbers; - if (avatar !== undefined && avatar !== null) { - return makeAttachmentPointer(avatar).then(function(attachment) { - proto.group.avatar = attachment; - return sendGroupProto(numbers, proto).then(function() { - return proto.group.id; - }); - }); - } else { + return makeAttachmentPointer(avatar).then(function(attachment) { + proto.group.avatar = attachment; return sendGroupProto(numbers, proto).then(function() { return proto.group.id; }); - } + }); }); } diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index 7e3c5056c674..ce4fe8e41701 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -45,6 +45,9 @@ window.textsecure.MessageSender = function(url, username, password) { } function makeAttachmentPointer(attachment) { + if (typeof attachment !== 'object' || attachment == null) { + return Promise.resolve(undefined); + } var proto = new textsecure.protobuf.AttachmentPointer(); proto.key = textsecure.crypto.getRandomBytes(64); @@ -128,9 +131,9 @@ window.textsecure.MessageSender = function(url, username, password) { if (updateDevices === undefined) { return TextSecureServer.getKeysForNumber(number).then(handleResult); } else { - var promises = []; - for (var i in updateDevices) - promises[promises.length] = TextSecureServer.getKeysForNumber(number, updateDevices[i]).then(handleResult); + var promises = updateDevices.map(function(device) { + return TextSecureServer.getKeysForNumber(number, device).then(handleResult); + }); return Promise.all(promises); } @@ -271,10 +274,7 @@ window.textsecure.MessageSender = function(url, username, password) { var proto = new textsecure.protobuf.DataMessage(); proto.body = messageText; - var promises = []; - for (var i in attachments) - promises.push(makeAttachmentPointer(attachments[i])); - return Promise.all(promises).then(function(attachmentsArray) { + return Promise.all(attachments.map(makeAttachmentPointer)).then(function(attachmentsArray) { proto.attachments = attachmentsArray; return sendIndividualProto(number, proto, timestamp).then(function() { return sendSyncMessage(proto, timestamp, number); @@ -308,10 +308,7 @@ window.textsecure.MessageSender = function(url, username, password) { if (numbers === undefined) return Promise.reject(new Error("Unknown Group")); - var promises = []; - for (var i in attachments) - promises.push(makeAttachmentPointer(attachments[i])); - return Promise.all(promises).then(function(attachmentsArray) { + return Promise.all(attachments.map(makeAttachmentPointer)).then(function(attachmentsArray) { proto.attachments = attachmentsArray; return sendGroupProto(numbers, proto, timestamp); }); @@ -330,18 +327,12 @@ window.textsecure.MessageSender = function(url, username, password) { proto.group.members = numbers; proto.group.name = name; - if (avatar !== undefined) { - return makeAttachmentPointer(avatar).then(function(attachment) { - proto.group.avatar = attachment; - return sendGroupProto(numbers, proto).then(function() { - return proto.group.id; - }); - }); - } else { + return makeAttachmentPointer(avatar).then(function(attachment) { + proto.group.avatar = attachment; return sendGroupProto(numbers, proto).then(function() { return proto.group.id; }); - } + }); }); } @@ -359,18 +350,12 @@ window.textsecure.MessageSender = function(url, username, password) { } proto.group.members = numbers; - if (avatar !== undefined && avatar !== null) { - return makeAttachmentPointer(avatar).then(function(attachment) { - proto.group.avatar = attachment; - return sendGroupProto(numbers, proto).then(function() { - return proto.group.id; - }); - }); - } else { + return makeAttachmentPointer(avatar).then(function(attachment) { + proto.group.avatar = attachment; return sendGroupProto(numbers, proto).then(function() { return proto.group.id; }); - } + }); }); }