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,52 +9,116 @@ 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) {
vote.classList.add("trx-votes-hidden"); count += hideCommentVotes(data, currentUser);
if (!vote.textContent!.includes(" ")) {
continue;
}
vote.textContent = vote.textContent!.slice(
0,
vote.textContent!.indexOf(" "),
);
}
}
if (data.ownComments) {
const ownComments = querySelectorAll(".comment-votes");
count += ownComments.length;
for (const vote of ownComments) {
vote.classList.add("trx-hidden");
}
} }
if (data.otherTopics || data.ownTopics) { if (data.otherTopics || data.ownTopics) {
const selectors: string[] = []; count += hideTopicVotes(data, currentUser);
}
// Topics by other people will be encapsulated with a `<button>`. return count;
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) { * Hide vote displays from comments.
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.
*/
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;
const topicVotes = querySelectorAll(...selectors); if (data.otherComments && !postedBySelf) {
count += topicVotes.length; // The vote element can be a "Vote" button, an "Unvote" button or a
for (const vote of topicVotes) { // 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");
vote.textContent = "-";
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;
}
vote.textContent = vote.textContent!.slice(
0,
vote.textContent!.indexOf(" "),
);
} 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;
}
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++;
} }
} }