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) { function makeAttachmentPointer(attachment) {
if (typeof attachment !== 'object' || attachment == null) {
return Promise.resolve(undefined);
}
var proto = new textsecure.protobuf.AttachmentPointer(); var proto = new textsecure.protobuf.AttachmentPointer();
proto.key = textsecure.crypto.getRandomBytes(64); proto.key = textsecure.crypto.getRandomBytes(64);
@ -39717,9 +39720,9 @@ window.textsecure.MessageSender = function(url, username, password) {
if (updateDevices === undefined) { if (updateDevices === undefined) {
return TextSecureServer.getKeysForNumber(number).then(handleResult); return TextSecureServer.getKeysForNumber(number).then(handleResult);
} else { } else {
var promises = []; var promises = updateDevices.map(function(device) {
for (var i in updateDevices) return TextSecureServer.getKeysForNumber(number, device).then(handleResult);
promises[promises.length] = TextSecureServer.getKeysForNumber(number, updateDevices[i]).then(handleResult); });
return Promise.all(promises); return Promise.all(promises);
} }
@ -39860,10 +39863,7 @@ window.textsecure.MessageSender = function(url, username, password) {
var proto = new textsecure.protobuf.DataMessage(); var proto = new textsecure.protobuf.DataMessage();
proto.body = messageText; proto.body = messageText;
var promises = []; return Promise.all(attachments.map(makeAttachmentPointer)).then(function(attachmentsArray) {
for (var i in attachments)
promises.push(makeAttachmentPointer(attachments[i]));
return Promise.all(promises).then(function(attachmentsArray) {
proto.attachments = attachmentsArray; proto.attachments = attachmentsArray;
return sendIndividualProto(number, proto, timestamp).then(function() { return sendIndividualProto(number, proto, timestamp).then(function() {
return sendSyncMessage(proto, timestamp, number); return sendSyncMessage(proto, timestamp, number);
@ -39897,10 +39897,7 @@ window.textsecure.MessageSender = function(url, username, password) {
if (numbers === undefined) if (numbers === undefined)
return Promise.reject(new Error("Unknown Group")); return Promise.reject(new Error("Unknown Group"));
var promises = []; return Promise.all(attachments.map(makeAttachmentPointer)).then(function(attachmentsArray) {
for (var i in attachments)
promises.push(makeAttachmentPointer(attachments[i]));
return Promise.all(promises).then(function(attachmentsArray) {
proto.attachments = attachmentsArray; proto.attachments = attachmentsArray;
return sendGroupProto(numbers, proto, timestamp); return sendGroupProto(numbers, proto, timestamp);
}); });
@ -39919,18 +39916,12 @@ window.textsecure.MessageSender = function(url, username, password) {
proto.group.members = numbers; proto.group.members = numbers;
proto.group.name = name; proto.group.name = name;
if (avatar !== undefined) { return makeAttachmentPointer(avatar).then(function(attachment) {
return makeAttachmentPointer(avatar).then(function(attachment) { proto.group.avatar = attachment;
proto.group.avatar = attachment;
return sendGroupProto(numbers, proto).then(function() {
return proto.group.id;
});
});
} else {
return sendGroupProto(numbers, proto).then(function() { return sendGroupProto(numbers, proto).then(function() {
return proto.group.id; return proto.group.id;
}); });
} });
}); });
} }
@ -39948,18 +39939,12 @@ window.textsecure.MessageSender = function(url, username, password) {
} }
proto.group.members = numbers; proto.group.members = numbers;
if (avatar !== undefined && avatar !== null) { return makeAttachmentPointer(avatar).then(function(attachment) {
return makeAttachmentPointer(avatar).then(function(attachment) { proto.group.avatar = attachment;
proto.group.avatar = attachment;
return sendGroupProto(numbers, proto).then(function() {
return proto.group.id;
});
});
} else {
return sendGroupProto(numbers, proto).then(function() { return sendGroupProto(numbers, proto).then(function() {
return proto.group.id; return proto.group.id;
}); });
} });
}); });
} }

View file

@ -45,6 +45,9 @@ window.textsecure.MessageSender = function(url, username, password) {
} }
function makeAttachmentPointer(attachment) { function makeAttachmentPointer(attachment) {
if (typeof attachment !== 'object' || attachment == null) {
return Promise.resolve(undefined);
}
var proto = new textsecure.protobuf.AttachmentPointer(); var proto = new textsecure.protobuf.AttachmentPointer();
proto.key = textsecure.crypto.getRandomBytes(64); proto.key = textsecure.crypto.getRandomBytes(64);
@ -128,9 +131,9 @@ window.textsecure.MessageSender = function(url, username, password) {
if (updateDevices === undefined) { if (updateDevices === undefined) {
return TextSecureServer.getKeysForNumber(number).then(handleResult); return TextSecureServer.getKeysForNumber(number).then(handleResult);
} else { } else {
var promises = []; var promises = updateDevices.map(function(device) {
for (var i in updateDevices) return TextSecureServer.getKeysForNumber(number, device).then(handleResult);
promises[promises.length] = TextSecureServer.getKeysForNumber(number, updateDevices[i]).then(handleResult); });
return Promise.all(promises); return Promise.all(promises);
} }
@ -271,10 +274,7 @@ window.textsecure.MessageSender = function(url, username, password) {
var proto = new textsecure.protobuf.DataMessage(); var proto = new textsecure.protobuf.DataMessage();
proto.body = messageText; proto.body = messageText;
var promises = []; return Promise.all(attachments.map(makeAttachmentPointer)).then(function(attachmentsArray) {
for (var i in attachments)
promises.push(makeAttachmentPointer(attachments[i]));
return Promise.all(promises).then(function(attachmentsArray) {
proto.attachments = attachmentsArray; proto.attachments = attachmentsArray;
return sendIndividualProto(number, proto, timestamp).then(function() { return sendIndividualProto(number, proto, timestamp).then(function() {
return sendSyncMessage(proto, timestamp, number); return sendSyncMessage(proto, timestamp, number);
@ -308,10 +308,7 @@ window.textsecure.MessageSender = function(url, username, password) {
if (numbers === undefined) if (numbers === undefined)
return Promise.reject(new Error("Unknown Group")); return Promise.reject(new Error("Unknown Group"));
var promises = []; return Promise.all(attachments.map(makeAttachmentPointer)).then(function(attachmentsArray) {
for (var i in attachments)
promises.push(makeAttachmentPointer(attachments[i]));
return Promise.all(promises).then(function(attachmentsArray) {
proto.attachments = attachmentsArray; proto.attachments = attachmentsArray;
return sendGroupProto(numbers, proto, timestamp); return sendGroupProto(numbers, proto, timestamp);
}); });
@ -330,18 +327,12 @@ window.textsecure.MessageSender = function(url, username, password) {
proto.group.members = numbers; proto.group.members = numbers;
proto.group.name = name; proto.group.name = name;
if (avatar !== undefined) { return makeAttachmentPointer(avatar).then(function(attachment) {
return makeAttachmentPointer(avatar).then(function(attachment) { proto.group.avatar = attachment;
proto.group.avatar = attachment;
return sendGroupProto(numbers, proto).then(function() {
return proto.group.id;
});
});
} else {
return sendGroupProto(numbers, proto).then(function() { return sendGroupProto(numbers, proto).then(function() {
return proto.group.id; return proto.group.id;
}); });
} });
}); });
} }
@ -359,18 +350,12 @@ window.textsecure.MessageSender = function(url, username, password) {
} }
proto.group.members = numbers; proto.group.members = numbers;
if (avatar !== undefined && avatar !== null) { return makeAttachmentPointer(avatar).then(function(attachment) {
return makeAttachmentPointer(avatar).then(function(attachment) { proto.group.avatar = attachment;
proto.group.avatar = attachment;
return sendGroupProto(numbers, proto).then(function() {
return proto.group.id;
});
});
} else {
return sendGroupProto(numbers, proto).then(function() { return sendGroupProto(numbers, proto).then(function() {
return proto.group.id; return proto.group.id;
}); });
} });
}); });
} }