From b9b00f68e246535d826883984994aa7fd2a55621 Mon Sep 17 00:00:00 2001 From: Michael DeBonis Date: Sat, 8 Jul 2017 21:37:38 -0500 Subject: [PATCH] Fixing invalid members in directPosts --- lib/modules/directChannels.js | 29 ++++++++++-------- lib/modules/directPosts.js | 38 ++++++++++++++++-------- lib/modules/test/fixtures/directPosts.js | 6 ++++ lib/modules/utils.js | 15 ++++++++-- 4 files changed, 60 insertions(+), 28 deletions(-) diff --git a/lib/modules/directChannels.js b/lib/modules/directChannels.js index 7d1fb7a..4898049 100644 --- a/lib/modules/directChannels.js +++ b/lib/modules/directChannels.js @@ -47,20 +47,23 @@ module.exports = function(context) { result.from_jid ) // - // If we haven't processed this pair already - // AND the username pair are not the same, write - // the object + // If there at least two members // - var key = `${members[0]}|${members}[1]` - if(!channels[key] && members[0] !== members[1]) { - channels[key] = members - - log.info(`... writing ${members}`) - context.output.write( - Factory.directChannel({ - members - }) - ) + if(Utils.membersAreValid(members)) { + var key = `${members[0]}|${members[1]}` + // + // And we haven't processed this pair + // already + // + if(!channels[key]) { + channels[key] = members + log.info(`... writing ${members}`) + context.output.write( + Factory.directChannel({ + members + }) + ) + } } // // Invoke the call to mark that we are diff --git a/lib/modules/directPosts.js b/lib/modules/directPosts.js index 71fa54a..ec899c0 100644 --- a/lib/modules/directPosts.js +++ b/lib/modules/directPosts.js @@ -38,22 +38,34 @@ module.exports = function(context) { // transform(function(message, encoding, callback) { // - // Write the direct post to the - // output + // Generate the members array for the + // direct channel // - context.output.write( - Factory.directPost({ - channel_members: Utils.members( - context.values.users, - message.to_jid, - message.from_jid - ), - user: Utils.username(context.values.users, message.from_jid), - message: Utils.body(message), - create_at: Utils.millis(message.sent_date) - }) + let members = Utils.members( + context.values.users, + message.to_jid, + message.from_jid ) // + // Ensure we have at least two channel + // members before we can write the message + // + if (Utils.membersAreValid(members)) { + context.output.write( + Factory.directPost({ + channel_members: members, + user: Utils.username(context.values.users, message.from_jid), + message: Utils.body(message), + create_at: Utils.millis(message.sent_date) + }) + ) + } else { + log.warn({ + to_jid: message.to_jid, + from_jid: message.from_jid + }, '... skipping message with invalid channel members') + } + // // Log progress periodically // written += 1 diff --git a/lib/modules/test/fixtures/directPosts.js b/lib/modules/test/fixtures/directPosts.js index eb026ff..437a744 100644 --- a/lib/modules/test/fixtures/directPosts.js +++ b/lib/modules/test/fixtures/directPosts.js @@ -17,5 +17,11 @@ module.exports = [ sent_date: '2017-06-10T03:50:28.515Z', body_string: '', body_text: 'message 03' + }, { + to_jid: 'person.two@example.com/g1b9b8hs09', + from_jid: 'person.two@example.com/g1b9b8hs08', + sent_date: '2017-06-10T03:50:28.515Z', + body_string: 'This should not be exported', + body_text: '' } ] diff --git a/lib/modules/utils.js b/lib/modules/utils.js index 6e7b2bf..2b92f0c 100644 --- a/lib/modules/utils.js +++ b/lib/modules/utils.js @@ -60,10 +60,21 @@ utils.body = function (message) { // array // utils.members = function(users, to, from) { - return _.sortBy([ + // + // We use uniq to remove duplicate + // members in the same channel + // + return _.uniq(_.sortBy([ utils.username(users, to), utils.username(users, from), - ]) + ])) +} + +// +// Checks if the members list is valid +// +utils.membersAreValid = function(members) { + return _.isArray(members) && members.length > 1 } //