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

Commit 1a1b99e

Browse files
committed
Modify thanks method
- 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.
1 parent 565c94f commit 1a1b99e

File tree

1 file changed

+52
-24
lines changed

1 file changed

+52
-24
lines changed

lib/bot/cmds/thanks.js

+52-24
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
const Utils = require('../../../lib/utils/Utils'),
44
HttpWrap = require('../../../lib/utils/HttpWrap'),
5-
TextLib = require('../../../lib/utils/TextLib');
5+
TextLib = require('../../../lib/utils/TextLib'),
6+
dedent = require('dedent'),
7+
_ = require('lodash');
68

79
const thanksCommands = {
810

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

2224
const mentions = input.message.model.mentions;
2325
// just 'thanks' in a message
24-
if (mentions && mentions.length === 0) {
26+
if (_.isEmpty(mentions)) {
2527
Utils.warn('thanks', 'without any mentions', input.message.model);
2628
return null;
2729
}
2830

29-
const fromUser = input.message.model.fromUser.username.toLowerCase();
31+
const fromUser = input.message.model.fromUser.username;
32+
3033
const options = {
3134
method: 'POST',
32-
input: input,
33-
bot: bot
35+
input,
36+
bot
3437
};
3538

36-
const namesList = mentions.reduce((userList, mention) => {
37-
const toUser = mention.screenName.toLowerCase();
38-
if (toUser !== fromUser && userList.indexOf(toUser) === -1) {
39-
const apiPath = '/api/users/give-brownie-points?receiver=' + toUser +
40-
'&giver=' + fromUser;
41-
HttpWrap.callApi(apiPath, options, thanksCommands.showInfoCallback);
42-
userList.push(toUser);
43-
}
44-
return userList;
45-
}, []);
46-
47-
if (namesList.length > 0) {
48-
const toUserMessage = namesList.join(' and @');
49-
return '> ' + fromUser + ' sends brownie points to @' + toUserMessage +
50-
' :sparkles: :thumbsup: :sparkles: ';
51-
} else {
52-
return '> sorry ' + fromUser + ', you can\'t send brownie points to ' +
53-
'yourself! :sparkles: :sparkles: ';
54-
}
39+
const thankList = _.chain(mentions)
40+
.uniq('screenName')
41+
.filter((user) =>
42+
user.screenName.toLowerCase() !== fromUser.toLowerCase()
43+
&& !!user.userId
44+
)
45+
.map((user) => user.screenName)
46+
.value();
47+
48+
thankList.forEach((toUser) => {
49+
const apiPath = `/api/users/give-brownie-points?receiver=${toUser.toLowerCase()}&giver=${fromUser.toLowerCase()}`;
50+
HttpWrap.callApi(apiPath, options, thanksCommands.showInfoCallback);
51+
});
52+
53+
let message = '';
54+
55+
const THANKLIST_SIZE = thankList.length;
56+
if (THANKLIST_SIZE > 0) {
57+
const $symboledThankedList = _.chain(thankList)
58+
.map((user) => `@${user}`);
59+
60+
const $lastUser = $symboledThankedList.last();
61+
const $initialUsers = $symboledThankedList.take(THANKLIST_SIZE - 1);
62+
63+
let thankedUsersMsg = $initialUsers.value().join(', ');
64+
// if more than one user is thanked
65+
// then append an "and"
66+
if (thankedUsersMsg !== '') {
67+
thankedUsersMsg += ' and ';
68+
}
69+
70+
thankedUsersMsg += $lastUser.value();
71+
message += dedent`\n> @${fromUser} sends brownie points to \
72+
${thankedUsersMsg} :sparkles: :thumbsup: :sparkles: `;
73+
}
74+
75+
if (mentions.find(
76+
(user) => user.screenName.toLowerCase() === fromUser.toLowerCase())
77+
) {
78+
message += dedent`\n> sorry @${fromUser}, \
79+
you can't send brownie points to yourself! :sparkles: :sparkles: `;
80+
}
81+
82+
return message;
5583
},
5684

5785
about: function(input, bot) {

0 commit comments

Comments
 (0)