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

Commit 04b9b6e

Browse files
committed
Improved thanks more
- fixed some messages - formatted source a bit
1 parent 9476bd7 commit 04b9b6e

File tree

1 file changed

+60
-60
lines changed

1 file changed

+60
-60
lines changed

lib/bot/cmds/thanks.js

+60-60
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
11
'use strict';
22

3-
const Utils = require('../../../lib/utils/Utils'),
4-
HttpWrap = require('../../../lib/utils/HttpWrap'),
5-
TextLib = require('../../../lib/utils/TextLib'),
6-
dedent = require('dedent'),
7-
_ = require('lodash');
3+
const Utils = require('../../../lib/utils/Utils');
4+
const HttpWrap = require('../../../lib/utils/HttpWrap');
5+
const TextLib = require('../../../lib/utils/TextLib');
6+
const dedent = require('dedent');
7+
const _ = require('lodash');
88

99
const thanksCommands = {
10-
11-
// messages: {
12-
// wikiHint: function(fromUser) {
13-
// const wikiUrl = '(https://github.com/freecodecamp/' +
14-
// 'freecodecamp/wiki/wiki-style-guide)';
15-
//
16-
// return '\n> hey @' + fromUser + ' if you found this info helpful ' +
17-
// ':point_right: *[consider adding a wiki article!]' + wikiUrl + '*';
18-
// }
19-
// },
20-
21-
thanks: function(input, bot) {
10+
thanks: function (input, bot) {
2211
Utils.hasProperty(input, 'message', 'thanks expects an object');
2312

2413
const mentions = input.message.model.mentions;
@@ -30,56 +19,67 @@ const thanksCommands = {
3019

3120
const fromUser = input.message.model.fromUser.username;
3221

33-
const options = {
34-
method: 'POST',
35-
input,
36-
bot
37-
};
38-
22+
/*
23+
* - filter the mentions by unique mentions
24+
* - filter by removing the thanking user
25+
* - grab the `screenName`
26+
*/
3927
const thankList = _.chain(mentions)
4028
.uniq('screenName')
41-
.filter((user) =>
29+
.filter(user =>
4230
user.screenName.toLowerCase() !== fromUser.toLowerCase()
4331
&& !!user.userId
4432
)
45-
.map((user) => user.screenName)
33+
.map('screenName')
4634
.value();
4735

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

8585
about: function(input, bot) {

0 commit comments

Comments
 (0)