reduce branching depth, prefer reduce over forEach, add comments, lint, dead code removal

pull/539/head
Ryan Tharp 6 years ago
parent d1526b9997
commit c40fe1dc79

@ -398,8 +398,8 @@ class LokiAppDotNetServerAPI {
log.warn('No pubKeys provided to getUsers!'); log.warn('No pubKeys provided to getUsers!');
return []; return [];
} }
// ok to call without
if (!pubKeys.length) { if (!pubKeys.length) {
log.warn('No pubKeys given to getUsers!');
return []; return [];
} }
if (pubKeys.length > 200) { if (pubKeys.length > 200) {
@ -836,7 +836,6 @@ class LokiPublicChannelAPI {
params, params,
}); });
if (res.err || !res.response) { if (res.err || !res.response) {
return; return;
} }
@ -844,6 +843,8 @@ class LokiPublicChannelAPI {
let receivedAt = new Date().getTime(); let receivedAt = new Date().getTime();
const pubKeys = []; const pubKeys = [];
let pendingMessages = []; let pendingMessages = [];
// the signature forces this to be async
pendingMessages = await Promise.all( pendingMessages = await Promise.all(
res.response.data.reverse().map(async adnMessage => { res.response.data.reverse().map(async adnMessage => {
// still update our last received if deleted, not signed or not valid // still update our last received if deleted, not signed or not valid
@ -901,10 +902,13 @@ class LokiPublicChannelAPI {
].splice(-5); ].splice(-5);
const from = adnMessage.user.name || 'Anonymous'; // profileName const from = adnMessage.user.name || 'Anonymous'; // profileName
// track sources for multidevice support
if (pubKeys.indexOf(`@${adnMessage.user.username}`) === -1) { if (pubKeys.indexOf(`@${adnMessage.user.username}`) === -1) {
pubKeys.push(`@${adnMessage.user.username}`); pubKeys.push(`@${adnMessage.user.username}`);
} }
// generate signal message object
const messageData = { const messageData = {
serverId: adnMessage.id, serverId: adnMessage.id,
clientVerified: true, clientVerified: true,
@ -947,31 +951,28 @@ class LokiPublicChannelAPI {
); );
this.conversation.setLastRetrievedMessage(this.lastGot); this.conversation.setLastRetrievedMessage(this.lastGot);
// do we really need this?
if (!pendingMessages.length) { if (!pendingMessages.length) {
return; return;
} }
if (pubKeys.length) {
// this will set slavePrimaryMap // get list of verified primary PKs
const verifiedPrimaryPKs = await lokiFileServerAPI.verifyPrimaryPubKeys( const verifiedPrimaryPKs = await lokiFileServerAPI.verifyPrimaryPubKeys(
pubKeys pubKeys
); );
// access slavePrimaryMap set by verifyPrimaryPubKeys
const { slavePrimaryMap } = this.serverAPI.chatAPI; const { slavePrimaryMap } = this.serverAPI.chatAPI;
const slaveMessages = {}; // sort pending messages by if slave device or not
// sort pending messages /* eslint-disable no-param-reassign */
pendingMessages.forEach(messageData => { const slaveMessages = pendingMessages.reduce((retval, messageData) => {
// why am I getting these?
if (messageData === undefined) {
log.warn('invalid pendingMessages');
return;
}
// if a known slave, queue // if a known slave, queue
if (slavePrimaryMap[messageData.source]) { if (slavePrimaryMap[messageData.source]) {
// delay sending the message // delay sending the message
if (slaveMessages[messageData.source] === undefined) { if (retval[messageData.source] === undefined) {
slaveMessages[messageData.source] = [messageData]; retval[messageData.source] = [messageData];
} else { } else {
slaveMessages[messageData.source].push(messageData); retval[messageData.source].push(messageData);
} }
} else { } else {
// no user or isPrimary means not multidevice, send event now // no user or isPrimary means not multidevice, send event now
@ -979,35 +980,41 @@ class LokiPublicChannelAPI {
message: messageData, message: messageData,
}); });
} }
}); return retval;
pendingMessages = []; // free memory }, {});
/* eslint-enable no-param-reassign */
// build map of userProfileName to primaryKeys pendingMessages = []; // allow memory to be freed
// if we have verified primaryKeys/the claimed relation
if (verifiedPrimaryPKs.length) { // get actual chat server data (mainly the name rn) of primary device
// get final list of verified chat server profile names
const verifiedDeviceResults = await this.serverAPI.getUsers( const verifiedDeviceResults = await this.serverAPI.getUsers(
verifiedPrimaryPKs verifiedPrimaryPKs
); );
// go through verifiedDeviceResults // build map of userProfileName to primaryKeys
const newPrimaryUserProfileName = {}; /* eslint-disable no-param-reassign */
verifiedDeviceResults.forEach(user => { this.primaryUserProfileName = verifiedDeviceResults.reduce(
newPrimaryUserProfileName[user.username] = user.name; (mapOut, user) => {
}); mapOut[user.username] = user.name;
// replace whole return mapOut;
this.primaryUserProfileName = newPrimaryUserProfileName; },
} {}
);
/* eslint-enable no-param-reassign */
// process remaining messages // process remaining messages
const ourNumber = textsecure.storage.user.getNumber(); const ourNumber = textsecure.storage.user.getNumber();
Object.keys(slaveMessages).forEach(slaveKey => { Object.keys(slaveMessages).forEach(slaveKey => {
// prevent our own sent messages from coming back in // prevent our own device sent messages from coming back in
if (slaveKey === ourNumber) { if (slaveKey === ourNumber) {
// we originally sent these // we originally sent these
return; return;
} }
// look up primary device once
const primaryPubKey = slavePrimaryMap[slaveKey]; const primaryPubKey = slavePrimaryMap[slaveKey];
// send out remaining messages for this merged identity
slaveMessages[slaveKey].forEach(messageDataP => { slaveMessages[slaveKey].forEach(messageDataP => {
const messageData = messageDataP; // for linter const messageData = messageDataP; // for linter
if (slavePrimaryMap[messageData.source]) { if (slavePrimaryMap[messageData.source]) {
@ -1022,39 +1029,6 @@ class LokiPublicChannelAPI {
}); });
}); });
}); });
} // end if there are pending pubkeys to look up
// find messages for original slave key using slavePrimaryMap
if (pendingMessages.length) {
const { slavePrimaryMap } = this.serverAPI.chatAPI;
const ourNumber = textsecure.storage.user.getNumber();
pendingMessages.forEach(messageDataP => {
const messageData = messageDataP; // for linter
// why am I getting these?
if (messageData === undefined) {
log.warn(`invalid pendingMessages ${pendingMessages}`);
return;
}
// prevent our own sent messages from coming back in
if (messageData.source === ourNumber) {
// we originally sent this
return;
}
if (slavePrimaryMap[messageData.source]) {
// rewrite source, profile
const primaryPubKey = slavePrimaryMap[messageData.source];
log.info(`Rewriting ${messageData.source} to ${primaryPubKey}`);
messageData.source = primaryPubKey;
messageData.message.profile.displayName = this.primaryUserProfileName[
primaryPubKey
];
}
this.serverAPI.chatAPI.emit('publicMessage', {
message: messageData,
});
});
}
pendingMessages = [];
} }
static getPreviewFromAnnotation(annotation) { static getPreviewFromAnnotation(annotation) {

Loading…
Cancel
Save