1
1
'use strict' ;
2
2
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' ) ;
8
8
9
9
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 ) {
22
11
Utils . hasProperty ( input , 'message' , 'thanks expects an object' ) ;
23
12
24
13
const mentions = input . message . model . mentions ;
@@ -30,56 +19,67 @@ const thanksCommands = {
30
19
31
20
const fromUser = input . message . model . fromUser . username ;
32
21
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
+ */
39
27
const thankList = _ . chain ( mentions )
40
28
. uniq ( 'screenName' )
41
- . filter ( ( user ) =>
29
+ . filter ( user =>
42
30
user . screenName . toLowerCase ( ) !== fromUser . toLowerCase ( )
43
31
&& ! ! user . userId
44
32
)
45
- . map ( ( user ) => user . screenName )
33
+ . map ( ' screenName' )
46
34
. value ( ) ;
47
35
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 ;
83
83
} ,
84
84
85
85
about : function ( input , bot ) {
0 commit comments