Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | 9x 15x 15x 5x 5x 4x 13x 66x 11x 11x 11x 7x 7x 6x 56x 5x 4x 4x 4x 4x 4x 1x 1x 3x 3x 1x 2x 7x 7x 4x 1x 2x | import { MAX_DOMAINS, normaliseDomain } from "@/apps/user/pages/rules/DomainListInput";
import { ADD_TRUSTED_PARAM, REMOVE_BLOCKED_PARAM } from "@/apps/user/pages/rules/links";
import type { RuleAction } from "@/objects/Rule";
import { covers } from "@/util/webSearchNotice";
/**
* The change the note under an answer offers a Tools rule (`editRuleHref`):
* a source to add to a rule that allows only some (`?add_trusted=`), or one
* to stop ruling out (`?remove_blocked=`). The builder makes it once, on the
* rule as it opens, and shows what it did — the person reviews it and saves,
* which is the rule's ordinary edit (a version, its history). Nothing here
* ever saves: a link cannot change a rule.
*/
export type DomainEdit = { kind: "add_trusted" | "remove_blocked"; domain: string };
/** What the change came to on this rule. */
export type DomainEditOutcome =
/** Done: added, or removed — review, then save. */
| "added"
| "removed"
/** Nothing to do: already a source, or not one it rules out. */
| "already"
| "absent"
/** Cannot: the list is full, or the source is the only one it rules out. */
| "full"
| "last"
/** The rule has no such action. */
| "no_action";
export interface DomainEditResult {
actions: RuleAction[];
outcome: DomainEditOutcome;
domain: string;
}
/** The change a link asks for, normalised as svc-core stores a domain —
* null when it asks for none, or names no domain. */
export function domainEditOf(params: URLSearchParams): DomainEdit | null {
for (const [kind, param] of [
["add_trusted", ADD_TRUSTED_PARAM],
["remove_blocked", REMOVE_BLOCKED_PARAM],
] as const) {
const raw = params.get(param);
if (raw === null) continue;
const domain = normaliseDomain(raw);
return domain ? { kind, domain } : null;
}
return null;
}
/** A list parameter as it is held: stored as a list, edited as text. */
const domainsOf = (value: unknown): string[] =>
(Array.isArray(value) ? value.map(String) : String(value ?? "").split(/[\s,;]+/))
.map((entry) => normaliseDomain(entry) || entry.trim())
.filter(Boolean);
/** `actions` with `edit` made on the first action it applies to. */
export function applyDomainEdit(
actions: RuleAction[],
edit: DomainEdit,
): DomainEditResult {
const { kind, domain } = edit;
const unchanged = (outcome: DomainEditOutcome): DomainEditResult => ({
actions,
outcome,
domain,
});
if (kind === "add_trusted") {
const index = actions.findIndex((a) => a.type === "trusted_sources");
if (index < 0) return unchanged("no_action");
const domains = domainsOf(actions[index].domains);
if (domains.some((d) => covers(d, domain))) return unchanged("already");
if (domains.length >= MAX_DOMAINS) return unchanged("full");
return {
actions: actions.map((a, i) =>
i === index ? { ...a, domains: [...domains, domain] } : a,
),
outcome: "added",
domain,
};
}
const index = actions.findIndex(
(a) => a.type === "blocked_sources" && domainsOf(a.domains).includes(domain),
);
if (index < 0) {
return unchanged(
actions.some((a) => a.type === "blocked_sources") ? "absent" : "no_action",
);
}
const domains = domainsOf(actions[index].domains);
if (domains.length === 1) return unchanged("last");
return {
actions: actions.map((a, i) =>
i === index ? { ...a, domains: domains.filter((d) => d !== domain) } : a,
),
outcome: "removed",
domain,
};
}
/** What the builder says about the change, and whether as a warning. */
export function describeDomainEdit(result: DomainEditResult): {
text: string;
warn: boolean;
} {
const { domain } = result;
switch (result.outcome) {
case "added":
return {
text: `Added ${domain} to the sources this rule allows. Review the rule, then save it — nothing changes until you do.`,
warn: false,
};
case "removed":
return {
text: `${domain} is no longer ruled out. Review the rule, then save it — nothing changes until you do.`,
warn: false,
};
case "already":
return { text: `${domain} is already one of this rule's sources.`, warn: false };
case "absent":
return { text: `This rule does not rule out ${domain}.`, warn: false };
case "full":
return {
text: `This rule already names ${MAX_DOMAINS} sources, the most one rule may: remove one before adding ${domain}.`,
warn: true,
};
case "last":
return {
text: `${domain} is the only source this rule rules out. To use it, switch the rule off or delete it instead.`,
warn: true,
};
default:
return { text: `This rule has no web sources to change for ${domain}.`, warn: true };
}
}
|