Skip to content

Commit

Permalink
Fixed bug in chat.PostDirectMessage. Refactored ._send function.
Browse files Browse the repository at this point in the history
  • Loading branch information
philliphenslee committed Nov 4, 2015
1 parent 84c7525 commit 33961c4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
14 changes: 7 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var _ = require('lodash');
var util = require('util');
var WebSocket = require('ws');

var attachment = require('./slack/attachment');
var Attachment = require('./slack/attachment');
var Cache = require('./cache');
var errors = require('./errors');
var logging = require('./logging');
Expand Down Expand Up @@ -73,7 +73,7 @@ util.inherits(SmartSlack, EventEmitter);
* @returns {object} Attachment The attachment instance
*/
SmartSlack.prototype.createAttachment = function (text) {
return new attachment(text);
return new Attachment(text);
};

/**
Expand Down Expand Up @@ -168,7 +168,7 @@ SmartSlack.prototype.sendToChannel = function (channel, text, callback) {
return callback(new Error(errors.missing_required_arg));
}
if (channel.match(/^([C]0)/)) {
return this._send(channel, text);
return this.send(channel, text);
}
return this._sendToType(slack.types.CHANNEL, channel, text, callback);
};
Expand All @@ -186,7 +186,7 @@ SmartSlack.prototype.sendToGroup = function (group, text, callback) {
return callback(new Error(errors.missing_required_arg));
}
if (group.match(/^([G]0)/)) {
return this._send(group, text);
return this.send(group, text);
}
return this._sendToType(slack.types.GROUP, group, text, callback);
};
Expand All @@ -204,7 +204,7 @@ SmartSlack.prototype.sendToUser = function (user, text, callback) {
return callback(new Error(errors.missing_required_arg));
}
if (user.match(/^([D]0)/)) {
return this._send(user, text);
return this.send(user, text);
}
return this._sendToType(slack.types.USER, user, text, callback);
};
Expand Down Expand Up @@ -381,7 +381,7 @@ SmartSlack.prototype._reconnect = function () {
* @param {string} text The message text
* @access private
*/
SmartSlack.prototype._send = function (channel, text) {
SmartSlack.prototype.send = function (channel, text) {
var _this = this;
var message;
var data;
Expand Down Expand Up @@ -447,7 +447,7 @@ SmartSlack.prototype._sendToType = function (slackType, typeName, text, callback
}

if (entityId) {
return this._send(entityId, text);
return this.send(entityId, text);
}
callback(new TypeError(errors.invalid_entity_type));
};
Expand Down
28 changes: 17 additions & 11 deletions lib/slack/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ function deleteMessage(timestamp, channel, callback) {
});
}
/**
* Post API message to a user by username
* @param user {string} user The user id
* Post API message to a entity by email, username, or channel
* @param entity {string} entity The entity
* @param text {string} message The message text
* @param {object} options Optional message options
* @param {function} callback(err,result)
*/
function postDirectMessage(user, text, options, callback) {
var channel = '';
function postDirectMessage(entity, text, options, callback) {
var channel;

callback = (_.isFunction(callback)) ? callback : _.noop;

Expand All @@ -45,16 +45,22 @@ function postDirectMessage(user, text, options, callback) {
options = null;
}

if (!_.isString(user) && !_.isString(text)) {
if (!_.isString(entity) && !_.isString(text)) {
return callback(new Error(errors.missing_required_arg), null);
}

users.getImChannel(user, function (err, result) {
if (err) {
return callback(err);
}
channel = result;
});
if (entity.match(/^D0[0-9A-Z]{7}/)) {
channel = entity;
}

if (!channel) {
users.getImChannel(entity, function (err, result) {
if (err) {
return callback(err);
}
channel = result;
});
}

text = common.escape(text);

Expand Down
4 changes: 2 additions & 2 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,11 @@ describe('SmartSlack', function () {
});
});

describe('#_send', function () {
describe('#send', function () {

it('exists as method on SmartSlack', function (done) {
var slackClient = new SmartSlack(mockopts);
expect(typeof slackClient._send).to.equal('function');
expect(typeof slackClient.send).to.equal('function');
done();
});
});
Expand Down

0 comments on commit 33961c4

Please sign in to comment.