Skip to content

Commit

Permalink
Fixing invalid members in directPosts
Browse files Browse the repository at this point in the history
  • Loading branch information
mkdbns committed Jul 9, 2017
1 parent a450545 commit b9b00f6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 28 deletions.
29 changes: 16 additions & 13 deletions lib/modules/directChannels.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 25 additions & 13 deletions lib/modules/directPosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/modules/test/fixtures/directPosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ module.exports = [
sent_date: '2017-06-10T03:50:28.515Z',
body_string: '',
body_text: 'message 03'
}, {
to_jid: '[email protected]/g1b9b8hs09',
from_jid: '[email protected]/g1b9b8hs08',
sent_date: '2017-06-10T03:50:28.515Z',
body_string: 'This should not be exported',
body_text: ''
}
]
15 changes: 13 additions & 2 deletions lib/modules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

//
Expand Down

0 comments on commit b9b00f6

Please sign in to comment.