Skip to content

Commit

Permalink
add lodash and getUserByEmail method
Browse files Browse the repository at this point in the history
  • Loading branch information
mishk0 committed Dec 17, 2016
1 parent 08472b7 commit d4c7ae7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 58 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ npm install slackbots
- `getChannel(name)` (return: promise) - gets channel by name,
- `getGroup(name)` (return: promise) - gets group by name,
- `getUser(name)` (return: promise) - gets user by name,
- `getUserByEmail(name)` (return: promise) - gets user by name,
- `getChannelId(name)` (return: promise) - gets channel ID by name,
- `getGroupId(name)` (return: promise) - gets group ID by name,
- `getUserId(name)` (return: promise) - gets user ID by name,
Expand Down
37 changes: 23 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict';

var _ = require('lodash');
var request = require('request');
var Vow = require('vow');
var extend = require('extend');
var WebSocket = require('ws');
var utils = require('./libs/utils.js');
var find = utils.find;
var assert = utils.assert;
var EventEmitter = require('events').EventEmitter;

class Bot extends EventEmitter {
Expand All @@ -20,7 +18,7 @@ class Bot extends EventEmitter {
this.token = params.token;
this.name = params.name;

assert(params.token, 'token must be defined');
console.assert(params.token, 'token must be defined');
this.login();
}

Expand Down Expand Up @@ -110,7 +108,7 @@ class Bot extends EventEmitter {
*/
getUser(name) {
return this.getUsers().then(function(data) {
return find(data.members, { name: name });
return _.find(data.members, { name: name }) || {};
});
}

Expand All @@ -121,7 +119,7 @@ class Bot extends EventEmitter {
*/
getChannel(name) {
return this.getChannels().then(function(data) {
return find(data.channels, { name: name });
return _.find(data.channels, { name: name }) || {};
});
}

Expand All @@ -132,7 +130,7 @@ class Bot extends EventEmitter {
*/
getGroup(name) {
return this.getGroups().then(function(data) {
return find(data.groups, { name: name });
return _.find(data.groups, { name: name }) || {};
});
}

Expand All @@ -143,7 +141,7 @@ class Bot extends EventEmitter {
*/
getUserById(id) {
return this.getUsers().then(function(data) {
return find(data.members, { id: id });
return _.find(data.members, { id: id }) || {};
});
}

Expand All @@ -154,7 +152,7 @@ class Bot extends EventEmitter {
*/
getChannelById(id) {
return this.getChannels().then(function(data) {
return find(data.channels, { id: id });
return _.find(data.channels, { id: id }) || {};
});
}

Expand All @@ -165,7 +163,7 @@ class Bot extends EventEmitter {
*/
getGroupById(id) {
return this.getGroups().then(function(data) {
return find(data.groups, { id: id });
return _.find(data.groups, { id: id }) || {};
});
}

Expand Down Expand Up @@ -202,6 +200,17 @@ class Bot extends EventEmitter {
});
}

/**
* Get user by email
* @param {string} email
* @returns {object}
*/
getUserByEmail(email) {
return this.getUsers().then(function(data) {
return _.find(data.members, { profile: { email: email } });
});
};

/**
* Get "direct message" channel ID
* @param {string} name
Expand All @@ -210,9 +219,9 @@ class Bot extends EventEmitter {
getChatId(name) {
return this.getUser(name).then(function(data) {

var chatId = find(this.ims, { user: data.id }).id;
var chatId = _.find(this.ims, { user: data.id });

return chatId || this.openIm(data.id);
return (chatId && chatId.id) || this.openIm(data.id);
}.bind(this)).then(function(data) {
return typeof data === 'string' ? data : data.channel.id;
});
Expand Down Expand Up @@ -343,9 +352,9 @@ class Bot extends EventEmitter {
return Vow.all([this.getChannels(), this.getUsers(), this.getGroups()]).then(function(data) {

var all = [].concat(data[0].channels, data[1].members, data[2].groups);
var result = find(all, {name: name});
var result = _.find(all, {name: name});

assert(Object.keys(result).length, 'wrong name');
console.assert(result, 'wrong name');

if (result['is_channel']) {
return this.postMessageToChannel(name, text, params, cb);
Expand Down
22 changes: 0 additions & 22 deletions libs/utils.js

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"extend": "^2.0.1",
"lodash": "^4.17.2",
"request": "^2.56.0",
"vow": "^0.4.9",
"ws": "^1.0.1"
Expand Down
22 changes: 0 additions & 22 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var chai = require('chai');
var Bot = require('../index.js');
var utils = require('../libs/utils.js');
var bot = new Bot({token: 'token'});
var sinon = require('sinon');
var vow = require('vow');
Expand All @@ -11,27 +10,6 @@ var expect = require('chai').expect;
var request = require('request');

describe('slack-bot-api', function() {

describe('#find', function() {

it('1', function() {
var data = [{a: 1, b: 2}, {b: 3, c: 4}];
expect(utils.find(data, {a: 1})).to.equal(data[0]);
});
it('2', function() {
var data = [{a: 1, b: 2, c: 4}, {b: 3, c: 4}];
expect(utils.find(data, {a: 1, c: 4})).to.equal(data[0]);
});
it('3', function() {
var data = [{a: 1, b: 2}, {b: 3, c: 4}];
expect(utils.find(data, {b: 3})).to.equal(data[1]);
});
it('4', function() {
var data = [{a: 1, b: 2}, {b: 3, c: 4}];
expect(utils.find(data, {a: 1, b: 2, c: 3})).to.not.equal(data[0]);
});
});

describe('#_preprocessParams', function() {
it('1', function() {
var input = {
Expand Down

0 comments on commit d4c7ae7

Please sign in to comment.