1
Fork 0

Add the different replacement types for Anonymize Usernames.

This commit is contained in:
Bauke 2023-08-13 11:40:40 +02:00
parent be5ccaf0c1
commit 6e62ea0036
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 42 additions and 9 deletions

View File

@ -1,15 +1,23 @@
import {log, querySelectorAll} from "../../utilities/exports.js"; import {hashSha256, log, querySelectorAll} from "../../utilities/exports.js";
import {
type AnonymizeUsernamesData,
ReplacementType,
} from "../../storage/exports.js";
export function runAnonymizeUsernamesFeature() { export async function runAnonymizeUsernamesFeature(
const count = anonymizeUsernames(); data: AnonymizeUsernamesData,
) {
const count = await anonymizeUsernames(data);
log(`Anonymize Usernames: Initialized for ${count} user links.`); log(`Anonymize Usernames: Initialized for ${count} user links.`);
} }
function anonymizeUsernames(): number { async function anonymizeUsernames(
data: AnonymizeUsernamesData,
): Promise<number> {
const usernameElements = querySelectorAll<HTMLElement>( const usernameElements = querySelectorAll<HTMLElement>(
".link-user:not(.trx-anonymized)", ".link-user:not(.trx-anonymized)",
); );
const replacements = generateReplacements(usernameElements); const replacements = await generateReplacements(usernameElements, data);
for (const element of usernameElements) { for (const element of usernameElements) {
let username = usernameFromElement(element); let username = usernameFromElement(element);
@ -28,14 +36,36 @@ function anonymizeUsernames(): number {
return usernameElements.length; return usernameElements.length;
} }
function generateReplacements(elements: HTMLElement[]): Record<string, string> { async function generateReplacements(
elements: HTMLElement[],
data: AnonymizeUsernamesData,
): Promise<Record<string, string>> {
const usernames = new Set( const usernames = new Set(
elements.map((element) => usernameFromElement(element).replace(/@/g, "")), elements.map((element) => usernameFromElement(element).replace(/@/g, "")),
); );
const replacements: Record<string, string> = {}; const replacements: Record<string, string> = {};
for (const [index, username] of Array.from(usernames).entries()) { for (const [index, username] of Array.from(usernames).entries()) {
switch (data.replacementType) {
case ReplacementType.Hashed: {
const hash = await hashSha256(username);
replacements[username] = hash.slice(0, 10).toUpperCase();
break;
}
case ReplacementType.Numerical: {
replacements[username] = `Anonymous ${index + 1}`; replacements[username] = `Anonymous ${index + 1}`;
break;
}
default: {
throw new Error(
`Unknown ReplacementType in AnonymizeUsernamesData: ${JSON.stringify(
data,
)}`,
);
}
}
} }
return replacements; return replacements;

View File

@ -82,8 +82,11 @@ async function initialize() {
} }
if (anonymizeUsernamesEnabled) { if (anonymizeUsernamesEnabled) {
observerFeatures.push(() => { const anonymizeUsernamesData = await fromStorage(
runAnonymizeUsernamesFeature(); Feature.AnonymizeUsernames,
);
observerFeatures.push(async () => {
await runAnonymizeUsernamesFeature(anonymizeUsernamesData.value);
}); });
} }