Skip to content

Commit

Permalink
Further cleanup of callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
philliphenslee committed Oct 15, 2015
1 parent 95e187a commit 76e9c85
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 61 deletions.
136 changes: 88 additions & 48 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ SmartSlack.prototype.connect = function () {
this.ws.on('close', function (data) {
_this.emit('close', data);

//TODO Add auto reonnect option
// Attempt reconnection
_this._reconnect();
});
Expand All @@ -147,7 +148,8 @@ SmartSlack.prototype.connect = function () {
});

this.ws.on('message', function (data) {
_this.onRtmEvent(JSON.parse(data));
//TODO try catch JSON call..
_this._onRtmEvent(JSON.parse(data));
});
};

Expand Down Expand Up @@ -263,36 +265,51 @@ SmartSlack.prototype.getLastChannelMessage = function (channel, callback) {
* Gets a group by id
* @return {object} group object
*/
SmartSlack.prototype.getGroupById = function (id) {
SmartSlack.prototype.getGroupById = function (id, callback) {

var group;
callback = (typeof callback === 'function') ? callback : function () { };

if (id && this.groups) {
return _.find(this.groups, { id: id });
group = _.find(this.groups, { id: id });
if (group) {
return callback(null, group);
}
} else {
throw new Error(errors.missing_required_arg);
}
}
};

/**
* Gets a group by name
* @return {object} group object
*/
SmartSlack.prototype.getGroupByName = function (name) {
SmartSlack.prototype.getGroupByName = function (name, callback) {

var group;
callback = (typeof callback === 'function') ? callback : function () { };

if (name && this.groups) {
return _.find(this.groups, { name: name });
group = _.find(this.groups, { name: name });
if (group) {
return callback(null, group);
}
} else {
throw new Error(errors.missing_required_arg);
}
}
};

/**
* Gets the users presence
* @param {string} auto || away
*/
SmartSlack.prototype.getPresence = function (user,callback) {
SmartSlack.prototype.getPresence = function (user, callback) {

callback = (typeof callback === 'function') ? callback : function () { };

if (user && typeof user === 'string') {
this._apiCall('users.getPresence', { user: user },function(data) {
if (callback) {
callback(data);
}
this._apiCall('users.getPresence', { user: user }, function (data) {
return callback(null, data);
})
} else {
throw new Error(errors.missing_required_arg);
Expand All @@ -303,22 +320,36 @@ SmartSlack.prototype.getPresence = function (user,callback) {
* Gets a user by name
* @return {object} user object
*/
SmartSlack.prototype.getUserByName = function (name) {
SmartSlack.prototype.getUserByName = function (name, callback) {

var user;
callback = (typeof callback === 'function') ? callback : function () { };

if (name && this.users) {
return _.find(this.users, { name: name });
user = _.find(this.users, { name: name });
if (user) {
return callback(null, user);
}
} else {
throw new Error(errors.missing_required_arg);
}

}
};

/**
* Gets a user by id
* @return {object} user object
*/
SmartSlack.prototype.getUserById = function (id) {
SmartSlack.prototype.getUserById = function (id, callback) {

var user;
callback = (typeof callback === 'function') ? callback : function () { };

if (id && this.users) {
return _.find(this.users, { id: id });
user = _.find(this.users, { id: id });
if (user) {
return callback(null, user);
}

} else {
throw new Error(errors.missing_required_arg);
}
Expand All @@ -330,22 +361,22 @@ SmartSlack.prototype.getUserById = function (id) {
* @return {array} the user list
*/
SmartSlack.prototype.getUsersList = function (callback) {

var _this = this;
callback = (typeof callback === 'function') ? callback : function () { };

this._apiCall('users.list', function (data) {

if (data.ok) {

// Save the users list
_this.users = data.users;
if (callback) {
callback(data.users);
}
return callback(null, data.users);
} else {
if (callback) {
callback(data);
}
return callback(null, data);
}
});
}
};

/**
* Login to Slack
Expand All @@ -356,10 +387,8 @@ SmartSlack.prototype.login = function () {
this._apiCall('rtm.start', function (data) {

if (data) {

if (!data.ok) {
_this.emit('error', data.error);

} else {

//console.log(data);
Expand All @@ -380,13 +409,14 @@ SmartSlack.prototype.login = function () {
}
}
});
}
};

/**
* Handle slack RTM event message
* @param The slack RTM message
*/
SmartSlack.prototype.onRtmEvent = function (message) {
SmartSlack.prototype._onRtmEvent = function (message) {

var _this = this;

this.emit('rawmessage', message);
Expand Down Expand Up @@ -415,19 +445,21 @@ SmartSlack.prototype.onRtmEvent = function (message) {
this.log.debug('Latency = ' + (this._lastPong - message.time) + 'ms');
break;
}

}
};

/**
* Open a direct message channel
* @param user
* @returns {object} JSON response
*/
SmartSlack.prototype.openIm = function (user, callback) {

callback = (typeof callback === 'function') ? callback : function () { };

if (user && typeof user === 'string') {
this._apiCall('im.open', { user: user }, function (data) {
if (data.ok) {
callback(data);
return callback(null, data);
}
});
} else {
Expand All @@ -443,6 +475,13 @@ SmartSlack.prototype.openIm = function (user, callback) {
*/
SmartSlack.prototype.postMessage = function (channelId, text, params, callback) {

callback = (typeof callback === 'function') ? callback : function () { };

if (typeof params === 'function') {
callback = params;
params = null;
}

if (channelId && text) {
params = _.extend({
channel: channelId,
Expand All @@ -451,10 +490,7 @@ SmartSlack.prototype.postMessage = function (channelId, text, params, callback)
}, params || {});

this._apiCall('chat.postMessage', params, function (data) {
if (callback) {
callback(data);
}

return callback(null, data);
});
} else {
throw new Error(errors.missing_required_arg);
Expand All @@ -465,6 +501,7 @@ SmartSlack.prototype.postMessage = function (channelId, text, params, callback)
* Sends a message via the RTM socket
*/
SmartSlack.prototype.send = function (channel, text) {

var msg;
var data;
if (channel && text) {
Expand All @@ -485,12 +522,12 @@ SmartSlack.prototype.send = function (channel, text) {
* @param {string} auto || away
*/
SmartSlack.prototype.setPresence = function (presence,callback) {

callback = (typeof callback === 'function') ? callback : function () { };

if (presence && presence === 'away' || presence === 'auto') {
this._apiCall('users.setPresence', { "presence": presence },function(data) {
if (callback) {
callback(data);
}

return callback(null, data);
})
} else {
throw new Error(errors.missing_required_arg);
Expand All @@ -503,27 +540,30 @@ SmartSlack.prototype.setPresence = function (presence,callback) {
* @returns {boolean}
*/
SmartSlack.prototype._canResolve = function (callback) {

var _this = this;
callback = (typeof callback === 'function') ? callback : function () { };

this.log.debug('Attempting to resolve ' + this.hostname);

dns.resolve4(_this.hostname, function (err, addresses) {

if (!err) {
_this.log.debug('Resolved ' + _this.hostname + ' (' + addresses[0] + ')');

if (_this.reconnecting) {
clearInterval(_this._reconnecting);
_this.login();
}
if (callback) {
callback(true);
}
return callback(true);

} else {

_this.log.debug('DNS resolution failed. Error Code: ' + err.code);
if (callback) {
callback(false);
}
return callback(false);
}
});
}
};

/**
* Sends ping message over the RTM
Expand All @@ -539,7 +579,7 @@ SmartSlack.prototype._ping = function () {
}

/**
* Reconnects to Slack RTM API
* Reconnects to Slack RTM
*/
SmartSlack.prototype._reconnect = function () {
var _this = this;
Expand Down
32 changes: 19 additions & 13 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,13 @@ describe('SmartSlack', function () {
})

it('should return a group object', function (done) {
var group = slackClient.getGroupById('G0BC7NYJ0');
expect(group).to.be.an('object');
expect(group.name).to.equal('groupname');
slackClient.getGroupById('G0BC7NYJ0', function (err, group) {
expect(group).to.be.an('object');
expect(group.name).to.equal('groupname');
});

done();
})
});
});

describe('#getLastChannelMessage', function () {
Expand Down Expand Up @@ -404,9 +406,11 @@ describe('SmartSlack', function () {
})

it('should return a user object', function (done) {
var user = slackClient.getUserByName('john');
expect(user).to.be.an('object');
expect(user.id).to.equal('U0BZD3JFH7');
slackClient.getUserByName('john', function (err, user) {
expect(user).to.be.an('object');
expect(user.id).to.equal('U0BZD3JFH7');
});

done();
})
});
Expand Down Expand Up @@ -434,11 +438,13 @@ describe('SmartSlack', function () {
})

it('should return a user object', function (done) {
var user = slackClient.getUserById('U0BZD3JFH7');
expect(user).to.be.an('object');
expect(user.name).to.equal('john');
slackClient.getUserById('U0BZD3JFH7', function (err, user) {
expect(user).to.be.an('object');
expect(user.name).to.equal('john');
});

done();
})
});
});

describe('#getUserList', function () {
Expand Down Expand Up @@ -518,8 +524,8 @@ describe('SmartSlack', function () {

var slackClient = new SmartSlack(mockopts);

it('exists as a public method on SmartSlack', function (done) {
expect(typeof slackClient.onRtmEvent).to.equal('function');
it('exists as a method on SmartSlack', function (done) {
expect(typeof slackClient._onRtmEvent).to.equal('function');
done();
})

Expand Down

0 comments on commit 76e9c85

Please sign in to comment.