@cloudhal/comments 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +86 -2
- package/dist/src/client/comments.browser.js +245 -0
- package/dist/src/client/index.d.ts +19 -0
- package/dist/src/client/index.js +39 -0
- package/dist/src/client/solver.browser.js +118 -0
- package/dist/src/crypto.d.ts +64 -0
- package/dist/src/crypto.js +170 -0
- package/dist/src/errors.d.ts +38 -0
- package/dist/src/errors.js +61 -0
- package/dist/src/fixtures/comment-text.json +335 -0
- package/dist/src/hashes.d.ts +70 -0
- package/dist/src/hashes.js +191 -0
- package/dist/src/index.d.ts +24 -0
- package/dist/src/index.js +24 -0
- package/dist/src/limits.d.ts +73 -0
- package/dist/src/limits.js +227 -0
- package/dist/src/linkify.d.ts +53 -0
- package/dist/src/linkify.js +142 -0
- package/dist/src/model.d.ts +287 -0
- package/dist/src/model.js +341 -0
- package/dist/src/normalize.d.ts +17 -0
- package/dist/src/normalize.js +125 -0
- package/dist/src/pipeline.d.ts +176 -0
- package/dist/src/pipeline.js +898 -0
- package/dist/src/pow.d.ts +86 -0
- package/dist/src/pow.js +239 -0
- package/dist/src/purge.d.ts +87 -0
- package/dist/src/purge.js +186 -0
- package/dist/src/retention.d.ts +32 -0
- package/dist/src/retention.js +106 -0
- package/dist/src/rules.d.ts +68 -0
- package/dist/src/rules.js +218 -0
- package/dist/src/settings.d.ts +108 -0
- package/dist/src/settings.js +347 -0
- package/dist/src/sql/index.d.ts +7 -0
- package/dist/src/sql/index.js +29 -0
- package/dist/src/sql/portability.d.ts +113 -0
- package/dist/src/sql/portability.js +514 -0
- package/dist/src/sql/rows.d.ts +54 -0
- package/dist/src/sql/rows.js +145 -0
- package/dist/src/sql/store.d.ts +312 -0
- package/dist/src/sql/store.js +2111 -0
- package/dist/src/thread.d.ts +35 -0
- package/dist/src/thread.js +64 -0
- package/package.json +61 -3
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The retention plan (RFC-0007 §9.3, invariant C11): what the hourly job deletes or clears, with
|
|
3
|
+
* the cutoffs computed from a site's settings and the clock. Pure; `SqlCommentStore.retention`
|
|
4
|
+
* runs the steps in order as bounded statements — at most 500 rows a run in total, so the job
|
|
5
|
+
* stays inside Workers Free's limits and a backlog catches up over hours.
|
|
6
|
+
*
|
|
7
|
+
* | Data | Default |
|
|
8
|
+
* |---|---|
|
|
9
|
+
* | `pending` never decided | deleted after 30 days |
|
|
10
|
+
* | `spam` | deleted after 30 days |
|
|
11
|
+
* | `deleted` | deleted after 30 days; a tombstone stays while approved replies need it |
|
|
12
|
+
* | `hidden` | kept, or deleted after N days when set |
|
|
13
|
+
* | `ip_hmac`, `challenge_id` | cleared after 30 days (at most; never longer) on comments and reports |
|
|
14
|
+
* | Commenter emails | deleted when nothing refers to them |
|
|
15
|
+
* | Resolved reports, statements of reasons of removed comments | 6 months |
|
|
16
|
+
* | Bans | removed when they expire |
|
|
17
|
+
* | Monthly salts | deleted when two months old |
|
|
18
|
+
* | Comments of a deleted entry | deleted 30 days after the entry |
|
|
19
|
+
*
|
|
20
|
+
* A comment due for deletion that still has approved replies under it becomes a tombstone instead
|
|
21
|
+
* (no text, erased author, status `deleted`), so the replies keep their place.
|
|
22
|
+
*/
|
|
23
|
+
import { DAY_MS, utcMonth } from "./model.js";
|
|
24
|
+
import { clientDataDays, DEFAULT_RETENTION, } from "./settings.js";
|
|
25
|
+
|
|
26
|
+
export const RETENTION_BUDGET = 500;
|
|
27
|
+
/** Comments of a deleted entry are deleted this long after the entry. */
|
|
28
|
+
export const DELETED_ENTRY_DAYS = 30;
|
|
29
|
+
/** `cache_purge_log` rows older than this are deleted (the budget window is 60 s). */
|
|
30
|
+
export const PURGE_LOG_KEEP_MS = 3_600_000;
|
|
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
|
+
/** A site's retention steps, in the order they must run (comments before the emails they free). */
|
|
57
|
+
export function retentionPlan(
|
|
58
|
+
retention = DEFAULT_RETENTION,
|
|
59
|
+
now ,
|
|
60
|
+
budget = RETENTION_BUDGET,
|
|
61
|
+
) {
|
|
62
|
+
const ago = (days ) => new Date(now.getTime() - days * DAY_MS).toISOString();
|
|
63
|
+
const steps = [
|
|
64
|
+
{ kind: "pending", cutoff: ago(retention.pendingDays) },
|
|
65
|
+
{ kind: "spam", cutoff: ago(retention.spamDays) },
|
|
66
|
+
{ kind: "deleted", cutoff: ago(retention.deletedDays) },
|
|
67
|
+
];
|
|
68
|
+
if (retention.hiddenDays !== null)
|
|
69
|
+
steps.push({ kind: "hidden", cutoff: ago(retention.hiddenDays) });
|
|
70
|
+
// tombstones: rows of the steps above kept because approved replies hang under them
|
|
71
|
+
steps.push({
|
|
72
|
+
kind: "tombstones",
|
|
73
|
+
cutoff: ago(Math.min(retention.pendingDays, retention.spamDays, retention.deletedDays)),
|
|
74
|
+
});
|
|
75
|
+
steps.push(
|
|
76
|
+
// clamped: a value stored before the 30-day ceiling never keeps hashes longer (§9.1)
|
|
77
|
+
{ kind: "client-data", cutoff: ago(clientDataDays(retention)) },
|
|
78
|
+
{ kind: "reports", cutoff: ago(retention.resolvedReportDays) },
|
|
79
|
+
{ kind: "events", cutoff: ago(retention.resolvedReportDays) },
|
|
80
|
+
{ kind: "emails", cutoff: now.toISOString() },
|
|
81
|
+
{ kind: "bans", cutoff: now.toISOString() },
|
|
82
|
+
);
|
|
83
|
+
return { steps, budget };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Salts of months before the previous one are deleted (§9.2): the oldest month to keep. */
|
|
87
|
+
export function oldestSaltMonthToKeep(now ) {
|
|
88
|
+
const d = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth() - 1, 1));
|
|
89
|
+
return utcMonth(d);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Entries deleted at least 30 days ago whose comments the job may delete. */
|
|
93
|
+
export function deletedEntriesDue(
|
|
94
|
+
deleted ,
|
|
95
|
+
now ,
|
|
96
|
+
) {
|
|
97
|
+
const cutoff = now.getTime() - DELETED_ENTRY_DAYS * DAY_MS;
|
|
98
|
+
return deleted.filter((d) => Date.parse(d.deletedAt) <= cutoff).map((d) => d.nodeId);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content rules (RFC-0007 §6.4) on normalised text:
|
|
3
|
+
*
|
|
4
|
+
* - **Links:** `https?://` or `www.` occurrences. Above the site's `linkLimit` → label `links`;
|
|
5
|
+
* above 10 → refused (`400 invalid`, "too many links").
|
|
6
|
+
* - **Word lists:** `watchWords` → label `watch-word`; `blockedWords` → status `spam`. Matched on
|
|
7
|
+
* **whole words** (a list entry of several words matches them in a row) after NFKC and case
|
|
8
|
+
* folding with the Turkish dotted and dotless i folded together (U+0130 `İ`, `I`, U+0131 `ı`, and `i` + U+0307 → `i`), and
|
|
9
|
+
* with invisible format characters removed, so "press" does not match "WordPress", "İstanbul"
|
|
10
|
+
* matches "istanbul", and "fr" + U+200B + "ee" matches "free".
|
|
11
|
+
* - **Instruction-like text:** a small, versioned rule set for text that addresses an AI, in
|
|
12
|
+
* English and Turkish (label `instruction-like`). It never blocks; it warns people and agents.
|
|
13
|
+
*/
|
|
14
|
+
import type { CommentLabel } from "@cloudhal/core-contracts";
|
|
15
|
+
import { type LabelName } from "./model.js";
|
|
16
|
+
import type { FieldProblem } from "./normalize.js";
|
|
17
|
+
/** NFKC, Turkish-aware lower-casing, invisible format characters removed. */
|
|
18
|
+
export declare function foldForMatch(text: string): string;
|
|
19
|
+
/** `foldForMatch` with diacritics removed too (`ö` → `o`): the instruction rules only. */
|
|
20
|
+
export declare function foldLoose(text: string): string;
|
|
21
|
+
/** Whole words of a text, folded. */
|
|
22
|
+
export declare function wordTokens(text: string): string[];
|
|
23
|
+
/** A word list compiled once per settings value. */
|
|
24
|
+
export type CompiledWordList = readonly (readonly string[])[];
|
|
25
|
+
export declare function compileWordList(words: readonly string[]): CompiledWordList;
|
|
26
|
+
/** True when any entry's words occur in a row in `tokens`. */
|
|
27
|
+
export declare function matchesWordList(tokens: readonly string[], list: CompiledWordList): boolean;
|
|
28
|
+
/** `https?://` and `www.` occurrences (§6.4). */
|
|
29
|
+
export declare function countLinks(text: string): number;
|
|
30
|
+
/** Version of the instruction-like rule set; bump when it changes (labels are not recomputed). */
|
|
31
|
+
export declare const INSTRUCTION_RULES_VERSION = 1;
|
|
32
|
+
/** Cloudhal's MCP tool names: text that names them is talking to an agent. */
|
|
33
|
+
export declare const MCP_TOOL_NAMES: readonly ["list_comments", "get_comment_thread", "comments_summary", "label_comments", "moderate_comments", "draft_comment_reply", "set_comment_thread", "list_sites", "get_site", "list_pages", "get_page", "create_page", "update_page", "delete_page", "list_entries", "get_entry", "create_entry", "update_entry", "delete_entry", "create_snapshot", "list_snapshots", "diff_snapshots", "preview_page"];
|
|
34
|
+
/**
|
|
35
|
+
* The rules, matched against `foldLoose(text)` (lower case, Turkish i folded, no diacritics), so
|
|
36
|
+
* the Turkish patterns are written without `ı`, `ş`, `ğ`, `ü`, `ö`, `ç`.
|
|
37
|
+
*/
|
|
38
|
+
export declare const INSTRUCTION_RULES: readonly {
|
|
39
|
+
readonly id: string;
|
|
40
|
+
readonly re: RegExp;
|
|
41
|
+
}[];
|
|
42
|
+
/** Ids of the instruction rules a text matches (empty when none). */
|
|
43
|
+
export declare function instructionLikeRules(...texts: readonly string[]): string[];
|
|
44
|
+
export declare const isInstructionLike: (...texts: readonly string[]) => boolean;
|
|
45
|
+
export interface ContentRuleSettings {
|
|
46
|
+
readonly linkLimit: number;
|
|
47
|
+
readonly watchWords: CompiledWordList;
|
|
48
|
+
readonly blockedWords: CompiledWordList;
|
|
49
|
+
}
|
|
50
|
+
export type ContentDecision = {
|
|
51
|
+
readonly ok: true;
|
|
52
|
+
readonly status: "pending" | "spam";
|
|
53
|
+
readonly labels: readonly LabelName[];
|
|
54
|
+
} | {
|
|
55
|
+
readonly ok: false;
|
|
56
|
+
readonly field: "body";
|
|
57
|
+
readonly problem: FieldProblem;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Runs the rules on a normalised name and text: refusals a person can fix come back as
|
|
61
|
+
* `ok: false`; otherwise the status (`spam` on a blocked word) and the rule labels.
|
|
62
|
+
*/
|
|
63
|
+
export declare function evaluateContent(input: {
|
|
64
|
+
readonly name: string;
|
|
65
|
+
readonly text: string;
|
|
66
|
+
}, rules: ContentRuleSettings): ContentDecision;
|
|
67
|
+
/** Rule labels as contract labels, stamped `source: "rule"`. */
|
|
68
|
+
export declare function ruleLabels(labels: readonly LabelName[], at: string): CommentLabel[];
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content rules (RFC-0007 §6.4) on normalised text:
|
|
3
|
+
*
|
|
4
|
+
* - **Links:** `https?://` or `www.` occurrences. Above the site's `linkLimit` → label `links`;
|
|
5
|
+
* above 10 → refused (`400 invalid`, "too many links").
|
|
6
|
+
* - **Word lists:** `watchWords` → label `watch-word`; `blockedWords` → status `spam`. Matched on
|
|
7
|
+
* **whole words** (a list entry of several words matches them in a row) after NFKC and case
|
|
8
|
+
* folding with the Turkish dotted and dotless i folded together (U+0130 `İ`, `I`, U+0131 `ı`, and `i` + U+0307 → `i`), and
|
|
9
|
+
* with invisible format characters removed, so "press" does not match "WordPress", "İstanbul"
|
|
10
|
+
* matches "istanbul", and "fr" + U+200B + "ee" matches "free".
|
|
11
|
+
* - **Instruction-like text:** a small, versioned rule set for text that addresses an AI, in
|
|
12
|
+
* English and Turkish (label `instruction-like`). It never blocks; it warns people and agents.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { LINK_HARD_LIMIT } from "./model.js";
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
/** NFKC, Turkish-aware lower-casing, invisible format characters removed. */
|
|
19
|
+
export function foldForMatch(text ) {
|
|
20
|
+
return text
|
|
21
|
+
.normalize("NFKC")
|
|
22
|
+
.replace(/[\u0130I\u0131]/g, "i")
|
|
23
|
+
.toLowerCase()
|
|
24
|
+
.replace(/i\u0307/g, "i")
|
|
25
|
+
.replace(/\p{Default_Ignorable_Code_Point}/gu, "");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** `foldForMatch` with diacritics removed too (`ö` → `o`): the instruction rules only. */
|
|
29
|
+
export function foldLoose(text ) {
|
|
30
|
+
return foldForMatch(text).normalize("NFD").replace(/\p{M}/gu, "").normalize("NFC");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const WORD = /[\p{L}\p{N}\p{M}]+/gu;
|
|
34
|
+
|
|
35
|
+
/** Whole words of a text, folded. */
|
|
36
|
+
export function wordTokens(text ) {
|
|
37
|
+
return foldForMatch(text).match(WORD) ?? [];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A word list compiled once per settings value. */
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
export function compileWordList(words ) {
|
|
44
|
+
return words.map((w) => wordTokens(w)).filter((t) => t.length > 0);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** True when any entry's words occur in a row in `tokens`. */
|
|
48
|
+
export function matchesWordList(tokens , list ) {
|
|
49
|
+
for (const entry of list) {
|
|
50
|
+
const first = entry[0];
|
|
51
|
+
for (let i = 0; i + entry.length <= tokens.length; i++) {
|
|
52
|
+
if (tokens[i] !== first) continue;
|
|
53
|
+
let ok = true;
|
|
54
|
+
for (let j = 1; j < entry.length; j++)
|
|
55
|
+
if (tokens[i + j] !== entry[j]) {
|
|
56
|
+
ok = false;
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
if (ok) return true;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** `https?://` and `www.` occurrences (§6.4). */
|
|
66
|
+
export function countLinks(text ) {
|
|
67
|
+
return text.match(/https?:\/\/|www\./gi)?.length ?? 0;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Version of the instruction-like rule set; bump when it changes (labels are not recomputed). */
|
|
71
|
+
export const INSTRUCTION_RULES_VERSION = 1;
|
|
72
|
+
|
|
73
|
+
/** Cloudhal's MCP tool names: text that names them is talking to an agent. */
|
|
74
|
+
export const MCP_TOOL_NAMES = [
|
|
75
|
+
"list_comments",
|
|
76
|
+
"get_comment_thread",
|
|
77
|
+
"comments_summary",
|
|
78
|
+
"label_comments",
|
|
79
|
+
"moderate_comments",
|
|
80
|
+
"draft_comment_reply",
|
|
81
|
+
"set_comment_thread",
|
|
82
|
+
"list_sites",
|
|
83
|
+
"get_site",
|
|
84
|
+
"list_pages",
|
|
85
|
+
"get_page",
|
|
86
|
+
"create_page",
|
|
87
|
+
"update_page",
|
|
88
|
+
"delete_page",
|
|
89
|
+
"list_entries",
|
|
90
|
+
"get_entry",
|
|
91
|
+
"create_entry",
|
|
92
|
+
"update_entry",
|
|
93
|
+
"delete_entry",
|
|
94
|
+
"create_snapshot",
|
|
95
|
+
"list_snapshots",
|
|
96
|
+
"diff_snapshots",
|
|
97
|
+
"preview_page",
|
|
98
|
+
] ;
|
|
99
|
+
|
|
100
|
+
const B = "(?<![\\p{L}\\p{N}_])";
|
|
101
|
+
const E = "(?![\\p{L}\\p{N}_])";
|
|
102
|
+
const rule = (source ) => new RegExp(source, "u");
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The rules, matched against `foldLoose(text)` (lower case, Turkish i folded, no diacritics), so
|
|
106
|
+
* the Turkish patterns are written without `ı`, `ş`, `ğ`, `ü`, `ö`, `ç`.
|
|
107
|
+
*/
|
|
108
|
+
export const INSTRUCTION_RULES = [
|
|
109
|
+
// English
|
|
110
|
+
{
|
|
111
|
+
id: "en-ignore-instructions",
|
|
112
|
+
re: rule(
|
|
113
|
+
`${B}(?:ignore|disregard|forget|override|bypass)${E}[^.\\n]{0,40}${B}(?:instructions?|prompts?|rules|guidelines|directives|guardrails)${E}`,
|
|
114
|
+
),
|
|
115
|
+
},
|
|
116
|
+
{ id: "en-system-prompt", re: rule(`${B}system\\s+(?:prompt|message|instructions?)${E}`) },
|
|
117
|
+
{ id: "en-you-are-now", re: rule(`${B}you\\s+are\\s+now${E}`) },
|
|
118
|
+
{ id: "en-new-instructions", re: rule(`${B}new\\s+(?:instructions?|task|rules)\\s*:`) },
|
|
119
|
+
{
|
|
120
|
+
id: "en-address-ai",
|
|
121
|
+
re: rule(
|
|
122
|
+
`${B}(?:ai|llm|language\\s+model|ai\\s+assistant|assistant|agent|chatgpt|gpt|claude|gemini|copilot)\\s*[:,]\\s*(?:please\\s+)?(?:ignore|approve|publish|delete|execute|run|call|reply|forget)${E}`,
|
|
123
|
+
),
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
id: "en-as-an-ai",
|
|
127
|
+
re: rule(`${B}(?:as|dear)\\s+an?\\s+(?:ai|llm|language\\s+model|assistant)${E}`),
|
|
128
|
+
},
|
|
129
|
+
{ id: "en-jailbreak", re: rule(`${B}(?:jailbreak|developer\\s+mode|do\\s+anything\\s+now)${E}`) },
|
|
130
|
+
{
|
|
131
|
+
id: "en-approve-all",
|
|
132
|
+
re: rule(
|
|
133
|
+
`${B}(?:approve|publish|accept)\\s+(?:all|every|each|any)\\s+(?:the\\s+)?(?:pending\\s+)?comments?${E}`,
|
|
134
|
+
),
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
id: "markup-roles",
|
|
138
|
+
re: rule(
|
|
139
|
+
"</?\\s*(?:system|instructions?|reader-text|assistant|user|tool_call|tool_use|function_calls?|im_start|im_end)\\b",
|
|
140
|
+
),
|
|
141
|
+
},
|
|
142
|
+
{ id: "mcp-tool-name", re: rule(`${B}(?:${MCP_TOOL_NAMES.join("|")})${E}`) },
|
|
143
|
+
// Turkish (folded: "önceki talimatları yok say" → "onceki talimatlari yok say")
|
|
144
|
+
{
|
|
145
|
+
id: "tr-ignore-instructions",
|
|
146
|
+
re: rule(
|
|
147
|
+
`${B}(?:talimat|komut|kural|yonerge|yonlendirme)\\w*${E}[^.\\n]{0,30}${B}(?:yok\\s+say|unut|gormezden\\s+gel|dikkate\\s+alma|gecersiz\\s+say)`,
|
|
148
|
+
),
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
id: "tr-system-prompt",
|
|
152
|
+
re: rule(`${B}sistem\\s+(?:istem|komut|prompt|mesaj|talimat)\\w*`),
|
|
153
|
+
},
|
|
154
|
+
{ id: "tr-you-are-now", re: rule(`${B}(?:artik|bundan\\s+sonra)\\s+sen\\s+bir${E}`) },
|
|
155
|
+
{
|
|
156
|
+
id: "tr-address-ai",
|
|
157
|
+
re: rule(
|
|
158
|
+
`${B}(?:yapay\\s+zeka|asistan|ajan|bot)\\w*\\s*[:,]\\s*(?:lutfen\\s+)?(?:onayla|yayinla|sil|yok\\s+say|unut|calistir)`,
|
|
159
|
+
),
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
id: "tr-approve-all",
|
|
163
|
+
re: rule(`${B}(?:tum|butun|her)\\s+(?:bekleyen\\s+)?yorum\\w*\\s+(?:onayla|yayinla)`),
|
|
164
|
+
},
|
|
165
|
+
{ id: "tr-new-instructions", re: rule(`${B}yeni\\s+(?:talimat|gorev|kural)\\w*\\s*:`) },
|
|
166
|
+
];
|
|
167
|
+
|
|
168
|
+
/** Ids of the instruction rules a text matches (empty when none). */
|
|
169
|
+
export function instructionLikeRules(...texts ) {
|
|
170
|
+
const hits = [];
|
|
171
|
+
for (const t of texts) {
|
|
172
|
+
const folded = foldLoose(t);
|
|
173
|
+
for (const r of INSTRUCTION_RULES)
|
|
174
|
+
if (!hits.includes(r.id) && r.re.test(folded)) hits.push(r.id);
|
|
175
|
+
}
|
|
176
|
+
return hits;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export const isInstructionLike = (...texts ) =>
|
|
180
|
+
instructionLikeRules(...texts).length > 0;
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Runs the rules on a normalised name and text: refusals a person can fix come back as
|
|
198
|
+
* `ok: false`; otherwise the status (`spam` on a blocked word) and the rule labels.
|
|
199
|
+
*/
|
|
200
|
+
export function evaluateContent(
|
|
201
|
+
input ,
|
|
202
|
+
rules ,
|
|
203
|
+
) {
|
|
204
|
+
const links = countLinks(input.text);
|
|
205
|
+
if (links > LINK_HARD_LIMIT) return { ok: false, field: "body", problem: "too_many_links" };
|
|
206
|
+
const labels = [];
|
|
207
|
+
if (links > rules.linkLimit) labels.push("links");
|
|
208
|
+
const tokens = [...wordTokens(input.name), "\u0000", ...wordTokens(input.text)];
|
|
209
|
+
if (matchesWordList(tokens, rules.watchWords)) labels.push("watch-word");
|
|
210
|
+
if (isInstructionLike(input.name, input.text)) labels.push("instruction-like");
|
|
211
|
+
const spam = matchesWordList(tokens, rules.blockedWords);
|
|
212
|
+
return { ok: true, status: spam ? "spam" : "pending", labels };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Rule labels as contract labels, stamped `source: "rule"`. */
|
|
216
|
+
export function ruleLabels(labels , at ) {
|
|
217
|
+
return labels.map((label) => ({ label, source: "rule", at }));
|
|
218
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
export type Protection = "pow" | "turnstile";
|
|
2
|
+
export type AiLabels = "off" | "workers-ai";
|
|
3
|
+
export type PlanHint = "free" | "paid";
|
|
4
|
+
export interface CollectionCommentSettings {
|
|
5
|
+
readonly enabled: boolean;
|
|
6
|
+
/** Overrides the site's `closeAfterDays`; `null` = never close; absent = the site's. */
|
|
7
|
+
readonly closeAfterDays?: number | null;
|
|
8
|
+
}
|
|
9
|
+
/** Retention in days (§9.3). `null` = keep. */
|
|
10
|
+
export interface RetentionSettings {
|
|
11
|
+
/** `pending` never decided (default 30). */
|
|
12
|
+
readonly pendingDays: number;
|
|
13
|
+
/** `spam` (default 30). */
|
|
14
|
+
readonly spamDays: number;
|
|
15
|
+
/** `deleted`, unless approved replies keep a tombstone (default 30). */
|
|
16
|
+
readonly deletedDays: number;
|
|
17
|
+
/** `hidden` (default `null`: kept until a person deletes it). */
|
|
18
|
+
readonly hiddenDays: number | null;
|
|
19
|
+
/**
|
|
20
|
+
* `ip_hmac` and `challenge_id` on comments and reports: 1 to 30 days (default 30). The privacy
|
|
21
|
+
* notice promises "up to 30 days" (RFC-0007 §9.1, §9.6), so it is a ceiling, not just a default.
|
|
22
|
+
*/
|
|
23
|
+
readonly clientDataDays: number;
|
|
24
|
+
/** Resolved reports and statements of reasons of removed comments (default 183, six months). */
|
|
25
|
+
readonly resolvedReportDays: number;
|
|
26
|
+
}
|
|
27
|
+
export interface CommentSettings {
|
|
28
|
+
readonly siteId: string;
|
|
29
|
+
/** New comments accepted anywhere on the site. */
|
|
30
|
+
readonly enabled: boolean;
|
|
31
|
+
/** Keep approved comments visible (closed) while comments are off. */
|
|
32
|
+
readonly showWhenOff: boolean;
|
|
33
|
+
/** Collection slug → settings. A collection absent here is off. */
|
|
34
|
+
readonly collections: Readonly<Record<string, CollectionCommentSettings>>;
|
|
35
|
+
/** Site default; `null` = never. */
|
|
36
|
+
readonly closeAfterDays: number | null;
|
|
37
|
+
readonly protection: Protection;
|
|
38
|
+
/** Public Turnstile widget key (the secret is in `comment_secrets`). */
|
|
39
|
+
readonly turnstileSiteKey: string | null;
|
|
40
|
+
/**
|
|
41
|
+
* Accept JavaScript-less posts under the stricter limits (§6.5). They carry no proof of work and
|
|
42
|
+
* no Turnstile token, so with Turnstile on they are accepted only when
|
|
43
|
+
* `allowWithoutScriptWithTurnstile` is also true (see `withoutScriptAllowed`).
|
|
44
|
+
*/
|
|
45
|
+
readonly allowWithoutScript: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Keep accepting JavaScript-less posts while Turnstile protects the form (default false). An
|
|
48
|
+
* owner who turns on Turnstile expects every post to be verified; these posts are not, so the
|
|
49
|
+
* owner must opt in explicitly (threat-model review of 2026-10-12, item 5).
|
|
50
|
+
*/
|
|
51
|
+
readonly allowWithoutScriptWithTurnstile: boolean;
|
|
52
|
+
/** Links above this add the `links` label; above 10 the comment is refused. */
|
|
53
|
+
readonly linkLimit: number;
|
|
54
|
+
/** Add the `watch-word` label. */
|
|
55
|
+
readonly watchWords: readonly string[];
|
|
56
|
+
/** Stored as `spam`, never shown. */
|
|
57
|
+
readonly blockedWords: readonly string[];
|
|
58
|
+
/** Stored submissions per UTC day (§6.5). */
|
|
59
|
+
readonly dailyCap: number;
|
|
60
|
+
readonly aiLabels: AiLabels;
|
|
61
|
+
readonly retention: RetentionSettings;
|
|
62
|
+
readonly planHint: PlanHint | null;
|
|
63
|
+
/** `0` and `updatedAt: null` when no row is stored (the defaults apply). */
|
|
64
|
+
readonly rev: number;
|
|
65
|
+
readonly updatedAt: string | null;
|
|
66
|
+
readonly updatedBy: string | null;
|
|
67
|
+
}
|
|
68
|
+
export type CommentSettingsInput = Partial<Omit<CommentSettings, "siteId" | "rev" | "updatedAt" | "updatedBy" | "retention">> & {
|
|
69
|
+
readonly retention?: Partial<RetentionSettings>;
|
|
70
|
+
};
|
|
71
|
+
export declare const DEFAULT_RETENTION: RetentionSettings;
|
|
72
|
+
/**
|
|
73
|
+
* The defaults: comments off (§0.2 D4), 90-day close, proof of work, JavaScript-less posts on
|
|
74
|
+
* (under proof of work only: off while Turnstile is on unless the owner allows them there too).
|
|
75
|
+
*/
|
|
76
|
+
export declare function defaultCommentSettings(siteId: string): CommentSettings;
|
|
77
|
+
/** Suggested `dailyCap` on Workers Paid (§6.5). */
|
|
78
|
+
export declare const PAID_DAILY_CAP = 5000;
|
|
79
|
+
export declare const MAX_DAILY_CAP = 100000;
|
|
80
|
+
export declare const MAX_WORDS = 200;
|
|
81
|
+
export declare const MAX_WORD_LENGTH = 64;
|
|
82
|
+
export declare const MAX_COLLECTIONS = 100;
|
|
83
|
+
export declare const MAX_CLOSE_DAYS = 3650;
|
|
84
|
+
/**
|
|
85
|
+
* The longest IP hashes and challenge ids are kept (§9.1, §9.3). The disclosure snippet says "up
|
|
86
|
+
* to 30 days", so settings refuse more and the retention job clamps a stored value to it.
|
|
87
|
+
*/
|
|
88
|
+
export declare const MAX_CLIENT_DATA_DAYS = 30;
|
|
89
|
+
/** `clientDataDays` as the retention job applies it: never above `MAX_CLIENT_DATA_DAYS`. */
|
|
90
|
+
export declare function clientDataDays(retention: Pick<RetentionSettings, "clientDataDays">): number;
|
|
91
|
+
/** Word lists are stored trimmed, de-duplicated by their folded form, in the order given. */
|
|
92
|
+
export declare function normalizeWordList(name: string, words: unknown): string[];
|
|
93
|
+
/**
|
|
94
|
+
* Applies a patch to the current settings and checks the result. Throws `CommentSettingsError`
|
|
95
|
+
* for anything outside the allowed values; unknown keys are refused so a typo is not silently
|
|
96
|
+
* dropped.
|
|
97
|
+
*/
|
|
98
|
+
export declare function applySettingsInput(current: CommentSettings, input: CommentSettingsInput): CommentSettings;
|
|
99
|
+
/**
|
|
100
|
+
* Whether posts without JavaScript (no proof of work, no Turnstile token) are accepted (§6.2 step
|
|
101
|
+
* 7, D11): when the owner allows them, and, while Turnstile is on, only when the owner allowed them
|
|
102
|
+
* under Turnstile as well.
|
|
103
|
+
*/
|
|
104
|
+
export declare function withoutScriptAllowed(settings: Pick<CommentSettings, "allowWithoutScript" | "allowWithoutScriptWithTurnstile" | "protection">): boolean;
|
|
105
|
+
/** Whether new comments are accepted in a collection (site and collection both on). */
|
|
106
|
+
export declare function collectionEnabled(settings: CommentSettings, collection: string): boolean;
|
|
107
|
+
/** Days after first publication a thread in this collection closes (`null` = never). */
|
|
108
|
+
export declare function closeAfterDaysFor(settings: CommentSettings, collection: string): number | null;
|