Let makeAttachmentPointer resolve falsey arguments

Internalizing this pattern to the makeAttachmentPointer function lets us
DRY it up throughout the file.

// FREEBIE
This commit is contained in:
lilia 2015-09-22 15:55:47 -07:00
parent e626355e0c
commit a8f4bac2f7
2 changed files with 28 additions and 58 deletions

View file

@ -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;
});
}
});
});
}

View file

@ -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;
});
}
});
});
}