Skip to content
This repository has been archived by the owner on Oct 20, 2018. It is now read-only.

Commit

Permalink
Modify thanks method
Browse files Browse the repository at this point in the history
- Use lodash
- Discard non-existing users and self and thank only existing ones (which also fixes thanking @/all as a bonus)
 E.g. if a user doesn't exist in gitter, then there will be no API calls to thank that user.
  • Loading branch information
abhisekp committed Apr 16, 2016
1 parent 565c94f commit 1a1b99e
Showing 1 changed file with 52 additions and 24 deletions.
76 changes: 52 additions & 24 deletions lib/bot/cmds/thanks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

const Utils = require('../../../lib/utils/Utils'),
HttpWrap = require('../../../lib/utils/HttpWrap'),
TextLib = require('../../../lib/utils/TextLib');
TextLib = require('../../../lib/utils/TextLib'),
dedent = require('dedent'),
_ = require('lodash');

const thanksCommands = {

Expand All @@ -21,37 +23,63 @@ const thanksCommands = {

const mentions = input.message.model.mentions;
// just 'thanks' in a message
if (mentions && mentions.length === 0) {
if (_.isEmpty(mentions)) {
Utils.warn('thanks', 'without any mentions', input.message.model);
return null;
}

const fromUser = input.message.model.fromUser.username.toLowerCase();
const fromUser = input.message.model.fromUser.username;

const options = {
method: 'POST',
input: input,
bot: bot
input,
bot
};

const namesList = mentions.reduce((userList, mention) => {
const toUser = mention.screenName.toLowerCase();
if (toUser !== fromUser && userList.indexOf(toUser) === -1) {
const apiPath = '/api/users/give-brownie-points?receiver=' + toUser +
'&giver=' + fromUser;
HttpWrap.callApi(apiPath, options, thanksCommands.showInfoCallback);
userList.push(toUser);
}
return userList;
}, []);

if (namesList.length > 0) {
const toUserMessage = namesList.join(' and @');
return '> ' + fromUser + ' sends brownie points to @' + toUserMessage +
' :sparkles: :thumbsup: :sparkles: ';
} else {
return '> sorry ' + fromUser + ', you can\'t send brownie points to ' +
'yourself! :sparkles: :sparkles: ';
}
const thankList = _.chain(mentions)
.uniq('screenName')
.filter((user) =>
user.screenName.toLowerCase() !== fromUser.toLowerCase()
&& !!user.userId
)
.map((user) => user.screenName)
.value();

thankList.forEach((toUser) => {
const apiPath = `/api/users/give-brownie-points?receiver=${toUser.toLowerCase()}&giver=${fromUser.toLowerCase()}`;
HttpWrap.callApi(apiPath, options, thanksCommands.showInfoCallback);
});

let message = '';

const THANKLIST_SIZE = thankList.length;
if (THANKLIST_SIZE > 0) {
const $symboledThankedList = _.chain(thankList)
.map((user) => `@${user}`);

const $lastUser = $symboledThankedList.last();
const $initialUsers = $symboledThankedList.take(THANKLIST_SIZE - 1);

let thankedUsersMsg = $initialUsers.value().join(', ');
// if more than one user is thanked
// then append an "and"
if (thankedUsersMsg !== '') {
thankedUsersMsg += ' and ';
}

thankedUsersMsg += $lastUser.value();
message += dedent`\n> @${fromUser} sends brownie points to \
${thankedUsersMsg} :sparkles: :thumbsup: :sparkles: `;
}

if (mentions.find(
(user) => user.screenName.toLowerCase() === fromUser.toLowerCase())
) {
message += dedent`\n> sorry @${fromUser}, \
you can't send brownie points to yourself! :sparkles: :sparkles: `;
}

return message;
},

about: function(input, bot) {
Expand Down

0 comments on commit 1a1b99e

Please sign in to comment.