1
Fork 0

Rework the Hide Votes logic so it explicitly handles our own and other's votes.

This commit is contained in:
Bauke 2023-06-25 13:28:26 +02:00
parent 30b190dcdf
commit 0542b09e0f
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
1 changed files with 102 additions and 38 deletions

View File

@ -9,16 +9,61 @@ export function runHideVotesFeature(data: HideVotesData) {
function hideVotes(data: HideVotesData): number {
let count = 0;
if (data.otherComments) {
const commentVotes = querySelectorAll(
'.btn-post-action[data-ic-put-to*="/vote"]:not(.trx-votes-hidden)',
'.btn-post-action[data-ic-delete-from*="/vote"]:not(.trx-votes-hidden)',
);
count += commentVotes.length;
// Get the username of the currently logged in user. When not logged in, set
// it to "<logged out>" which isn't a valid username, so matching against it
// will always return false. Meaning all comments are never the current user's.
const currentUser =
document.querySelector(".logged-in-user-username")?.textContent?.trim() ??
"<logged out>";
for (const vote of commentVotes) {
if (data.otherComments || data.ownComments) {
count += hideCommentVotes(data, currentUser);
}
if (data.otherTopics || data.ownTopics) {
count += hideTopicVotes(data, currentUser);
}
return count;
}
/**
* Hide vote displays from comments.
* @param data The {@link HideVotesData}.
* @param currentUser The username of the currently logged in user.
* @returns The amount of vote displays that have been hidden.
*/
function hideCommentVotes(data: HideVotesData, currentUser: string): number {
let count = 0;
for (const comment of querySelectorAll(".comment-itself")) {
const postedBySelf =
comment.querySelector("header .link-user")?.textContent?.trim() ===
currentUser;
if (data.otherComments && !postedBySelf) {
// The vote element can be a "Vote" button, an "Unvote" button or a
// regular text display when you can't vote anymore.
const vote =
comment.querySelector(
'.btn-post-action[data-ic-put-to*="/vote"]:not(.trx-votes-hidden)',
) ??
comment.querySelector(
'.btn-post-action[data-ic-delete-from*="/vote"]:not(.trx-votes-hidden)',
) ??
comment.querySelector(".comment-votes:not(.trx-votes-hidden)");
if (vote === null) {
continue;
}
count++;
vote.classList.add("trx-votes-hidden");
if (vote.tagName === "BUTTON") {
// If the vote element is a button, only remove the number from the text.
if (!vote.textContent!.includes(" ")) {
// If there is no space in the text, it means there are no votes on
// this comment yet.
continue;
}
@ -26,35 +71,54 @@ function hideVotes(data: HideVotesData): number {
0,
vote.textContent!.indexOf(" "),
);
}
}
if (data.ownComments) {
const ownComments = querySelectorAll(".comment-votes");
count += ownComments.length;
for (const vote of ownComments) {
} else {
// Otherwise if it's the regular text display, hide it entirely.
vote.classList.add("trx-hidden");
}
} else if (data.ownComments && postedBySelf) {
// Votes from our own comments are always the regular text display.
const vote = comment.querySelector(
".comment-votes:not(.trx-votes-hidden)",
);
if (vote === null) {
continue;
}
if (data.otherTopics || data.ownTopics) {
const selectors: string[] = [];
// Topics by other people will be encapsulated with a `<button>`.
if (data.otherTopics) {
selectors.push("button > .topic-voting-votes:not(.trx-votes-hidden)");
}
// Topics by yourself will be encapsulated with a `<div>`.
if (data.ownTopics) {
selectors.push("div > .topic-voting-votes:not(.trx-votes-hidden)");
}
const topicVotes = querySelectorAll(...selectors);
count += topicVotes.length;
for (const vote of topicVotes) {
vote.classList.add("trx-votes-hidden");
vote.textContent = "-";
count++;
vote.classList.add("trx-votes-hidden", "trx-hidden");
}
}
return count;
}
/**
* Hide vote displays from topics.
* @param data The {@link HideVotesData}.
* @param currentUser The username of the currently logged in user.
* @returns The amount of vote displays that have been hidden.
*/
function hideTopicVotes(data: HideVotesData, currentUser: string): number {
let count = 0;
for (const topic of querySelectorAll<HTMLElement>(".topic")) {
const postedBySelf = topic.dataset.topicPostedBy === currentUser;
let vote: Element | undefined;
// Select the vote number from the topic if we want to hide other's topics
// and it's not posted by the current user. Or if we want to hide our own
// topics and it *is* posted by the current user.
if (
(data.otherTopics && !postedBySelf) ||
(data.ownTopics && postedBySelf)
) {
vote = topic.querySelector(".topic-voting-votes")!;
}
if (vote !== undefined) {
vote.classList.add("trx-votes-hidden");
vote.textContent = "-";
count++;
}
}