Skip to content

Commit

Permalink
first pass at admin-notes
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanguill committed May 7, 2016
1 parent 1c75898 commit 59e5839
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/watney.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
"path": "plugins/admin-notify.js",
"adminChannel": "admins"
},
{
"id": "admin-notes",
"path": "plugins/admin-notes.js",
"adminChannel": "admins"
},
{
"id": "message-cop",
"path": "plugins/message-cop.js",
Expand Down
156 changes: 156 additions & 0 deletions plugins/admin-notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
const _ = require('lodash');
const moment = require('moment');

module.exports = (function(){

let bot,
redis,
config,
adminChannel,
redisNotesKey;

function persistNote (targetUser, authorUser, note) {
getNotesByUser(targetUser, function(err, data) {
if (err) {
return console.error(err);
}
data = JSON.parse(data || '[]');
data.unshift({authorUser: authorUser, targetUser: targetUser, dte: Date.now(), note: note});
redis.hset(redisNotesKey, targetUser, JSON.stringify(data));
});

}

function getNotesByUser (targetUser, callback) {
redis.hget(redisNotesKey, targetUser, callback);
}

function getUsersWithNotes (callback) {
redis.hkeys(redisNotesKey, callback);
}

function takeNote (message, channel, user) {
//only consider this in the admin channel. Do not even respond if not in that channel.
if (channel.name !== adminChannel) {
console.log(`cant take a !note from ${channel.name}`);
return;
}

let [command, flag, receiverUsername, ...rest] = message.parts;

if (_.isUndefined(receiverUsername)) {
return channel.send("who is the note about? use the format: `!note -add @user {note}`");
}

let slackUser = bot.getUserForMention(receiverUsername);
if (_.isUndefined(slackUser)) {
slackUser = bot.getUserByName(receiverUsername);
}

if (_.isUndefined(slackUser)) {
return channel.send('I`m not sure who ' + receiverUsername + ' is...');
}

let note = rest.join(' ').trim();

if (note.length === 0) {
return channel.send(`please provide a note for ${receiverUsername}`);
}

persistNote(slackUser.name, user.name, rest.join(' '));

return channel.send(`note added for ${slackUser.name}`);
}

function listNotes (message, channel, user) {
//only consider this in the admin channel. Do not even respond if not in that channel.
if (channel.name !== adminChannel) {
console.log(`cant take a !note from ${channel.name}`);
return;
}

let [command, flag, receiverUsername, ...rest] = message.parts;

if (_.isUndefined(receiverUsername)) {
return getUsersWithNotes(function(err, data) {
if (err) {
console.error(err);
return channel.send(`error getting users with notes: ${err}`);
}
data = data || [];
return channel.send(`I have notes about the following users: ${data.join(', ')}`);
});
}

let slackUser = bot.getUserForMention(receiverUsername);
if (_.isUndefined(slackUser)) {
slackUser = bot.getUserByName(receiverUsername);
}

if (_.isUndefined(slackUser)) {
return channel.send('I`m not sure who ' + receiverUsername + ' is...');
}

return getNotesByUser(slackUser.name, function (err, data) {
if (err) {
console.error(err);
return channel.send(`error getting notes for ${slackuser}: ${err}`);
}

data = JSON.parse(data || '[]');

let response = '';

if (data.length === 0) {
response = `there are not yet any notes for ${slackUser.name}`;
} else {
response = `${data.length} note${(data.length == 1 ? '' : 's')} for ${slackUser.name}\n\n`;

response = _.reduce(data, function (response, note) {
return response + moment(note.dte).format('YYYY-MM-DDTHH:mm:ss[Z]') + ` by ${note.authorUser}\n>${note.note}\n\n`;
}, response);
}

return channel.send(response);
});
}


return function init (_bot, _config) {
bot = _bot;
config = _config;
redis = bot.redis;

adminChannel = config.adminChannel;

if (_.isUndefined(adminChannel)) {
console.log('adminChannel not defined!');
} else if (!_.contains(bot.channelList, adminChannel) && !_.contains(bot.groupList, adminChannel)) {
console.log('Invalid adminChannel:', adminChannel, 'I am not part of that channel. ' +
'I must be invited to that channel.');
}

redisNotesKey = bot.botName + '-' + adminChannel + '.notes';

bot.register({
pattern: {regex: /^!note -add/g},
f: takeNote,
type: 'OUT',
eventType: 'message',
priority: 1000,
flags: {}
});

bot.register({
pattern: {regex: /^!note[s]? -list/g},
f: listNotes,
type: 'OUT',
eventType: 'message',
priority: 1000,
flags: {}
});

};


})();

0 comments on commit 59e5839

Please sign in to comment.