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,
@ -918,7 +922,7 @@ class LokiPublicChannelAPI {
isPublic: true, isPublic: true,
message: { message: {
body: body:
adnMessage.text === timestamp.toString() ? '' : adnMessage.text, adnMessage.text === timestamp.toString() ? '' : adnMessage.text,
attachments, attachments,
group: { group: {
id: this.conversationId, id: this.conversationId,
@ -947,103 +951,74 @@ 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
); );
const { slavePrimaryMap } = this.serverAPI.chatAPI; // access slavePrimaryMap set by verifyPrimaryPubKeys
const { slavePrimaryMap } = this.serverAPI.chatAPI;
const slaveMessages = {};
// sort pending messages // sort pending messages by if slave device or not
pendingMessages.forEach(messageData => { /* eslint-disable no-param-reassign */
// why am I getting these? const slaveMessages = pendingMessages.reduce((retval, messageData) => {
if (messageData === undefined) { // if a known slave, queue
log.warn('invalid pendingMessages'); if (slavePrimaryMap[messageData.source]) {
return; // delay sending the message
} if (retval[messageData.source] === undefined) {
// if a known slave, queue retval[messageData.source] = [messageData];
if (slavePrimaryMap[messageData.source]) {
// delay sending the message
if (slaveMessages[messageData.source] === undefined) {
slaveMessages[messageData.source] = [messageData];
} else {
slaveMessages[messageData.source].push(messageData);
}
} else { } else {
// no user or isPrimary means not multidevice, send event now retval[messageData.source].push(messageData);
this.serverAPI.chatAPI.emit('publicMessage', {
message: messageData,
});
} }
}); } else {
pendingMessages = []; // free memory // no user or isPrimary means not multidevice, send event now
this.serverAPI.chatAPI.emit('publicMessage', {
// build map of userProfileName to primaryKeys message: messageData,
// if we have verified primaryKeys/the claimed relation
if (verifiedPrimaryPKs.length) {
// get final list of verified chat server profile names
const verifiedDeviceResults = await this.serverAPI.getUsers(
verifiedPrimaryPKs
);
// go through verifiedDeviceResults
const newPrimaryUserProfileName = {};
verifiedDeviceResults.forEach(user => {
newPrimaryUserProfileName[user.username] = user.name;
}); });
// replace whole
this.primaryUserProfileName = newPrimaryUserProfileName;
} }
return retval;
}, {});
/* eslint-enable no-param-reassign */
// process remaining messages pendingMessages = []; // allow memory to be freed
const ourNumber = textsecure.storage.user.getNumber();
Object.keys(slaveMessages).forEach(slaveKey => {
// prevent our own sent messages from coming back in
if (slaveKey === ourNumber) {
// we originally sent these
return;
}
const primaryPubKey = slavePrimaryMap[slaveKey];
slaveMessages[slaveKey].forEach(messageDataP => {
const messageData = messageDataP; // for linter
if (slavePrimaryMap[messageData.source]) {
// rewrite source, profile
messageData.source = primaryPubKey;
messageData.message.profile.displayName = this.primaryUserProfileName[
primaryPubKey
];
}
this.serverAPI.chatAPI.emit('publicMessage', {
message: messageData,
});
});
});
} // end if there are pending pubkeys to look up
// find messages for original slave key using slavePrimaryMap // get actual chat server data (mainly the name rn) of primary device
if (pendingMessages.length) { const verifiedDeviceResults = await this.serverAPI.getUsers(
const { slavePrimaryMap } = this.serverAPI.chatAPI; verifiedPrimaryPKs
const ourNumber = textsecure.storage.user.getNumber(); );
pendingMessages.forEach(messageDataP => {
// build map of userProfileName to primaryKeys
/* eslint-disable no-param-reassign */
this.primaryUserProfileName = verifiedDeviceResults.reduce(
(mapOut, user) => {
mapOut[user.username] = user.name;
return mapOut;
},
{}
);
/* eslint-enable no-param-reassign */
// process remaining messages
const ourNumber = textsecure.storage.user.getNumber();
Object.keys(slaveMessages).forEach(slaveKey => {
// prevent our own device sent messages from coming back in
if (slaveKey === ourNumber) {
// we originally sent these
return;
}
// look up primary device once
const primaryPubKey = slavePrimaryMap[slaveKey];
// send out remaining messages for this merged identity
slaveMessages[slaveKey].forEach(messageDataP => {
const messageData = messageDataP; // for linter 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]) { if (slavePrimaryMap[messageData.source]) {
// rewrite source, profile // rewrite source, profile
const primaryPubKey = slavePrimaryMap[messageData.source];
log.info(`Rewriting ${messageData.source} to ${primaryPubKey}`);
messageData.source = primaryPubKey; messageData.source = primaryPubKey;
messageData.message.profile.displayName = this.primaryUserProfileName[ messageData.message.profile.displayName = this.primaryUserProfileName[
primaryPubKey primaryPubKey
@ -1053,8 +1028,7 @@ class LokiPublicChannelAPI {
message: messageData, message: messageData,
}); });
}); });
} });
pendingMessages = [];
} }
static getPreviewFromAnnotation(annotation) { static getPreviewFromAnnotation(annotation) {

Loading…
Cancel
Save