/
usedAllReactionsInComment.achievement.js
65 lines (53 loc) · 2.07 KB
/
usedAllReactionsInComment.achievement.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var _ = require('lodash');
// Gives achievement to comment authors who got all reactions
// without reacting themselves
var usedAllReactionsInComment = {
name: 'Gladiator',
check: function(pullRequest, shall) {
var topAuthorsUsernames = getCommentAuthorsWithAllReactions(pullRequest);
if (!_.isEmpty(topAuthorsUsernames)) {
var achievement = {
avatar: 'images/achievements/gladiator.achievement.gif',
name: 'Gladiator',
short: 'Are you not ENTERTAINED?!',
description: [
'Your message got all the possible reactions. ',
'Enjoy your 15 minutes of fame'
].join(''),
relatedPullRequest: pullRequest._id
};
_.forEach(topAuthorsUsernames, function(author) {
shall.grant(author, achievement);
});
}
}
};
function getCommentAuthorsWithAllReactions(pullRequest) {
var allComments = _.concat(pullRequest.comments, pullRequest.inlineComments);
var authors = _.map(allComments, 'author.username');
var AllCommentsReactions = _.map(allComments, 'reactions');
// also add pull request description reactions
authors.push(pullRequest.creator.username);
AllCommentsReactions.push(pullRequest.reactions);
getOnlyUniqueReactionsWithoutAuthors(AllCommentsReactions, authors);
return onlyUsersWithAllReactions(authors, AllCommentsReactions);
}
function reactionsWithoutAuthor(reactions, author) {
return _.map(_.reject(reactions, ['user.username', author]), 'reaction');
}
function getOnlyUniqueReactionsWithoutAuthors(AllCommentsReactions, authors) {
_.forEach(AllCommentsReactions, function(reactions, index) {
AllCommentsReactions[index] =
_.uniq(reactionsWithoutAuthor(reactions, authors[index]));
});
}
function onlyUsersWithAllReactions(authors, AllCommentsReactions) {
var commentAuthorsWithAllReactions = [];
_.forEach(authors, function(author, index) {
if (AllCommentsReactions[index].length === 6) {
commentAuthorsWithAllReactions.push(author);
}
});
return commentAuthorsWithAllReactions;
}
module.exports = usedAllReactionsInComment;