2
2
3
3
const Utils = require ( '../../../lib/utils/Utils' ) ,
4
4
HttpWrap = require ( '../../../lib/utils/HttpWrap' ) ,
5
- TextLib = require ( '../../../lib/utils/TextLib' ) ;
5
+ TextLib = require ( '../../../lib/utils/TextLib' ) ,
6
+ dedent = require ( 'dedent' ) ,
7
+ _ = require ( 'lodash' ) ;
6
8
7
9
const thanksCommands = {
8
10
@@ -21,37 +23,63 @@ const thanksCommands = {
21
23
22
24
const mentions = input . message . model . mentions ;
23
25
// just 'thanks' in a message
24
- if ( mentions && mentions . length === 0 ) {
26
+ if ( _ . isEmpty ( mentions ) ) {
25
27
Utils . warn ( 'thanks' , 'without any mentions' , input . message . model ) ;
26
28
return null ;
27
29
}
28
30
29
- const fromUser = input . message . model . fromUser . username . toLowerCase ( ) ;
31
+ const fromUser = input . message . model . fromUser . username ;
32
+
30
33
const options = {
31
34
method : 'POST' ,
32
- input : input ,
33
- bot : bot
35
+ input,
36
+ bot
34
37
} ;
35
38
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 ;
55
83
} ,
56
84
57
85
about : function ( input , bot ) {
0 commit comments