Skip to content

Commit

Permalink
Checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mkdbns committed Jul 6, 2017
1 parent 1c90a16 commit 560c375
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 125 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
channels,
users,
posts,
directChannels,
end
} = modules

Expand All @@ -17,6 +18,7 @@ start(context)
.then(channels)
.then(users)
.then(posts)
.then(directChannels)
.then(end)
.catch(function(err) {
console.error(err)
Expand Down
20 changes: 20 additions & 0 deletions lib/factory/directChannel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const Joi = require('joi')
const validate = require('./validate')

//
// Define the schema
//
const schema = {
header: Joi.string().allow('').optional(),
members: Joi.array().items(Joi.string())
}

//
// Generate a valid object
//
module.exports = function (props) {
return {
type: 'direct_channel',
direct_channel: validate(schema, props)
}
}
22 changes: 22 additions & 0 deletions lib/factory/directPost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const Joi = require('joi')
const validate = require('./validate')

//
// Define the schema
//
const schema = {
channel_members: Joi.array().items(Joi.string()),
user: Joi.string(),
message: Joi.string(),
create_at: Joi.number()
}

//
// Generate a valid object
//
module.exports = function (props) {
return {
type: 'direct_post',
direct_post: validate(schema, props)
}
}
6 changes: 5 additions & 1 deletion lib/factory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ const team = require('./team')
const channel = require('./channel')
const user = require('./user')
const post = require('./post')
const directChannel = require('./directChannel')
const directPost = require('./directPost')

module.exports = {
version,
team,
channel,
user,
post
post,
directChannel,
directPost
}
2 changes: 1 addition & 1 deletion lib/modules/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = function(context) {
// Select all of the rooms
//
return context.jabber.fetch(
'select * from dbo.tc_rooms'
'SELECT * FROM dbo.tc_rooms'
)
//
// The convert them to channels and
Expand Down
42 changes: 18 additions & 24 deletions lib/modules/directChannels.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ const Utils = require('./utils')
module.exports = function(context) {
return new Promise(function(resolve /*, reject */) {
//
// Array to accumulate the unique
// pair of users
// Array to accumulate the channel
// member pairs
//
var members = []
var channels = {}
//
// Set up the transform stream
//
const toDirectChannel = new Transform({
readableObjectMode: true,
writableObjectMode: true,
transform: function(pairs, encoding, callback) {
transform: function(result, encoding, callback) {
//
// Convert the jabber jid to usernames and
// sort them so it's easier to dedup. We
Expand All @@ -25,17 +25,24 @@ module.exports = function(context) {
// it should never happen. We want to terminate
// the export
//
let sorted = _.sortBy(
_.map(pairs, function(value) {
return context.values.users[Utils.realJID(value)].username
let members = _.sortBy(
_.map(result, function(jid) {
return context.values.users[Utils.realJID(jid)].username
})
)
//
// Ensure that we don't have a pair with the same
// usernames
// If we haven't processed this pair already
// AND the username pair are not the same, write
// the object
//
if(sorted[0] !== sorted[1]) {
members.push(sorted)
var key = `${members[0]}|${members}[1]`
if(!channels[key] && members[0] !== members[1]) {
channels[key] = members
context.output.write(
Factory.directChannel({
members
})
)
}
//
// Invoke the call to mark that we are
Expand All @@ -44,20 +51,7 @@ module.exports = function(context) {
return callback()
}
}).on('finish', function() {
_.map(
_.uniqBy(members, function(value) {
return `${value[0]}|${value}[1]`
}), function(value) {
context.output.write(
Factory.directChannel({
members: value
})
)
}
)

console.log('direct channels: ... finished')

resolve(context)
})

Expand Down
2 changes: 2 additions & 0 deletions lib/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const team = require('./team')
const channels = require('./channels')
const users = require('./users')
const posts = require('./posts')
const directChannels = require('./directChannels')
const end = require('./end')

module.exports = {
Expand All @@ -13,5 +14,6 @@ module.exports = {
channels,
users,
posts,
directChannels,
end
}
11 changes: 6 additions & 5 deletions lib/modules/posts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { Transform } = require('stream')
const Factory = require('../factory')
const Utils = require('./utils')

module.exports = function(context) {
return new Promise(function(resolve /*, reject */) {
Expand All @@ -20,7 +21,7 @@ module.exports = function(context) {
// Lookup the user
//
var user = lookup(
'user', context.values.users, message.from_jid
'user', context.values.users, Utils.realJID(message.from_jid)
)
//
// Get the body
Expand All @@ -31,7 +32,7 @@ module.exports = function(context) {
// post to the output
//
if(channel && user && body) {
this.push(
context.output.write(
Factory.post({
team: context.values.team.name,
channel: channel.name,
Expand Down Expand Up @@ -62,16 +63,16 @@ module.exports = function(context) {
//
context.jabber
.pipe(
`select
`SELECT
msg_id,
to_jid,
from_jid,
sent_date,
body_string,
body_text
from dbo.tc_msgarchive where msg_type = 'g'`,
FROM dbo.tc_msgarchive WHERE msg_type = 'g' AND (body_string != '' OR datalength(body_text) > 0) `,
toPost
).pipe(context.output)
)
})
}

Expand Down
105 changes: 15 additions & 90 deletions lib/modules/test/fixtures/users.js
Original file line number Diff line number Diff line change
@@ -1,123 +1,48 @@
module.exports = [
{
room_jid: '[email protected]',
real_jid: '[email protected]',
role: 'none',
affiliation: 'member',
nick_jid: '[email protected]/Micahel Cross',
reason: '',
initiator_jid: '[email protected]'
real_jid: '[email protected]'
}, {
room_jid: '[email protected]',
real_jid: '[email protected]',
role: 'none',
affiliation: 'owner',
nick_jid: '[email protected]/Scott Barclay',
reason: '',
initiator_jid: '[email protected]'
real_jid: '[email protected]'
}, {
room_jid: '[email protected]',
real_jid: '[email protected]',
role: 'none',
affiliation: 'admin',
nick_jid: '[email protected]/Tonyy Brown',
reason: '',
initiator_jid: '[email protected]'
real_jid: '[email protected]'
}, {
room_jid: '[email protected]',
real_jid: '[email protected]',
role: 'none',
affiliation: 'admin',
nick_jid: '[email protected]/James Seegar',
reason: '',
initiator_jid: '[email protected]'
real_jid: '[email protected]'
}, {
room_jid: '[email protected]',
real_jid: '[email protected]',
role: 'none',
affiliation: 'admin',
nick_jid: '[email protected]/jarrett.jennings2',
reason: '',
initiator_jid: '[email protected]'
real_jid: '[email protected]'
}, {
room_jid: '[email protected]',
real_jid: '[email protected]',
role: 'none',
affiliation: 'owner',
nick_jid: '[email protected]/Micahel.Cross',
reason: '',
initiator_jid: '[email protected]'
real_jid: '[email protected]'
}, {
room_jid: '[email protected]',
real_jid: '[email protected]',
role: 'none',
affiliation: 'owner',
nick_jid: '[email protected]/Michael Rhoades',
reason: '',
initiator_jid: '[email protected]'
real_jid: '[email protected]'
}, {
room_jid: '[email protected]',
real_jid: '[email protected]',
role: 'none',
affiliation: 'admin',
nick_jid: '[email protected]/Michael Rock',
reason: '',
initiator_jid: '[email protected]'
real_jid: '[email protected]'
}, {
room_jid: '[email protected]',
real_jid: '[email protected]',
role: 'none',
affiliation: 'admin',
nick_jid: '[email protected]/Terrence Flynn',
reason: '',
initiator_jid: '[email protected]'
real_jid: '[email protected]'
}, {
room_jid: '[email protected]',
real_jid: '[email protected]',
role: 'none',
affiliation: 'admin',
nick_jid: '[email protected]/Tony DiLisio',
reason: '',
initiator_jid: '[email protected]'
real_jid: '[email protected]'
}, {
room_jid: '[email protected]',
real_jid: '[email protected]',
role: 'none',
affiliation: 'owner',
nick_jid: '[email protected]/william.fleming2',
reason: '',
initiator_jid: '[email protected]'
real_jid: '[email protected]'
}, {
room_jid: 'test\\[email protected]',
real_jid: '[email protected]',
role: 'none',
affiliation: 'owner',
nick_jid: 'test\\[email protected]/LT U755',
reason: '',
initiator_jid: 'test\\[email protected]'
real_jid: '[email protected]'
}, {
room_jid: 'test\\[email protected]',
real_jid: '[email protected]/9fyg1zayoi',
role: 'none',
affiliation: 'owner',
nick_jid: 'test\\[email protected]/LT U755',
reason: 'creation',
initiator_jid: 'test\\[email protected]'
real_jid: '[email protected]/9fyg1zayoi'
}, {
room_jid: 'the\\20cool\\[email protected]',
real_jid: '[email protected]',
role: 'none',
affiliation: 'owner',
nick_jid: 'the\\20cool\\[email protected]/Cody Webb',
reason: '',
initiator_jid: 'the\\20cool\\[email protected]'
real_jid: '[email protected]'
}, {
room_jid: '[email protected]',
real_jid: '[email protected]',
role: 'none',
affiliation: 'owner',
nick_jid: '[email protected]/Tony DiLisio',
reason: '',
initiator_jid: '[email protected]'
real_jid: '[email protected]'
}
]
13 changes: 10 additions & 3 deletions lib/modules/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ module.exports = function(context) {
//
// Select all of the users
//
return context.jabber.fetch(
'select * from dbo.tc_users'
)
const where = 'msg_type = \'c\''

return context.jabber.fetch(`
SELECT real_jid, room_jid FROM dbo.tc_users
UNION ALL (
SELECT from_jid AS real_jid, NULL AS room_jid FROM dbo.jm WHERE ${where}
UNION
SELECT to_jid AS real_jid, NULL AS room_jid FROM dbo.jm WHERE ${where}
)
`)
//
// Build up the user objects and then
// write them to the output
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ module.exports = {
// once at the same time
//
realJID: function (jid='') {
return jid.split('/')[0]
return jid.split('/')[0].replace(/\\20/g, '.')
}
}

0 comments on commit 560c375

Please sign in to comment.