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 { function hideVotes(data: HideVotesData): number {
let count = 0; let count = 0;
if (data.otherComments) { // Get the username of the currently logged in user. When not logged in, set
const commentVotes = querySelectorAll( // it to "<logged out>" which isn't a valid username, so matching against it
'.btn-post-action[data-ic-put-to*="/vote"]:not(.trx-votes-hidden)', // will always return false. Meaning all comments are never the current user's.
'.btn-post-action[data-ic-delete-from*="/vote"]:not(.trx-votes-hidden)', const currentUser =
); document.querySelector(".logged-in-user-username")?.textContent?.trim() ??
count += commentVotes.length; "<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"); 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 (!vote.textContent!.includes(" ")) {
// If there is no space in the text, it means there are no votes on
// this comment yet.
continue; continue;
} }
@ -26,35 +71,54 @@ function hideVotes(data: HideVotesData): number {
0, 0,
vote.textContent!.indexOf(" "), vote.textContent!.indexOf(" "),
); );
} } else {
} // Otherwise if it's the regular text display, hide it entirely.
if (data.ownComments) {
const ownComments = querySelectorAll(".comment-votes");
count += ownComments.length;
for (const vote of ownComments) {
vote.classList.add("trx-hidden"); 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) { count++;
const selectors: string[] = []; vote.classList.add("trx-votes-hidden", "trx-hidden");
}
// Topics by other people will be encapsulated with a `<button>`. }
if (data.otherTopics) {
selectors.push("button > .topic-voting-votes:not(.trx-votes-hidden)"); return count;
} }
// Topics by yourself will be encapsulated with a `<div>`. /**
if (data.ownTopics) { * Hide vote displays from topics.
selectors.push("div > .topic-voting-votes:not(.trx-votes-hidden)"); * @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.
const topicVotes = querySelectorAll(...selectors); */
count += topicVotes.length; function hideTopicVotes(data: HideVotesData, currentUser: string): number {
for (const vote of topicVotes) { let count = 0;
vote.classList.add("trx-votes-hidden"); for (const topic of querySelectorAll<HTMLElement>(".topic")) {
vote.textContent = "-"; 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++;
} }
} }