Skip to content

Commit e4402d1

Browse files
paulmolluzzosindresorhus
authored andcommitted
Close refined-github#39 PR: Improve upvote / downvote comment handling. Fixes refined-github#32
1 parent b92fc31 commit e4402d1

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

extension/content.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const isReleases = () => isRepo && /^\/[^/]+\/[^/]+\/(releases|tags)/.test(locat
1111
const isBlame = () => isRepo && /^\/[^/]+\/[^/]+\/blame\//.test(location.pathname);
1212
const getUsername = () => $('meta[name="user-login"]').attr('content');
1313
const uselessContent = {
14-
upvote: {text: ['+1\n'], emoji: [':+1:']},
14+
upvote: {text: ['+1\n'], emoji: [':+1:', ':100:', ':ok_hand:']},
1515
downvote: {text: ['-1\n'], emoji: [':-1:']}
1616
};
1717

@@ -49,6 +49,8 @@ function commentIsUseless(type, el) {
4949
}
5050
}
5151
}
52+
53+
return false;
5254
}
5355

5456
function renderVoteCount(type, count) {
@@ -68,24 +70,41 @@ function renderVoteCount(type, count) {
6870
}
6971

7072
function moveVotes() {
71-
let upCount = 0;
72-
let downCount = 0;
73+
const upVoters = new Set();
74+
const downVoters = new Set();
7375
$('.js-comment-body').each((i, el) => {
76+
// this is a comment not in the usual container - found on inline comments
77+
if (!$(el).closest('.js-comment-container')) {
78+
return;
79+
}
80+
7481
const isUp = commentIsUseless('upvote', el);
7582
const isDown = commentIsUseless('downvote', el);
83+
const commenter = $(el).closest('.js-comment-container').find('.author').get(0).innerHTML;
7684

7785
if (isUp || isDown) {
78-
el.closest('.js-comment-container').remove();
86+
// remove from both arrays
87+
upVoters.delete(commenter);
88+
downVoters.delete(commenter);
7989

80-
upCount += isUp ? 1 : 0;
81-
downCount += isDown ? 1 : 0;
90+
// add to upvoters if it's an upvote
91+
if (isUp) {
92+
upVoters.add(commenter);
93+
}
94+
95+
// add to upvoters if it's an upvote
96+
if (isDown) {
97+
downVoters.add(commenter);
98+
}
99+
100+
el.closest('.js-comment-container').remove();
82101
}
83102
});
84-
if (upCount > 0) {
85-
renderVoteCount('upvote', upCount);
103+
if (upVoters.size > 0) {
104+
renderVoteCount('upvote', upVoters.size);
86105
}
87-
if (downCount > 0) {
88-
renderVoteCount('downvote', downCount);
106+
if (downVoters.size > 0) {
107+
renderVoteCount('downvote', downVoters.size);
89108
}
90109
}
91110

0 commit comments

Comments
 (0)