-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.ts
77 lines (67 loc) · 1.83 KB
/
filters.ts
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
export function text_filter(
text: string,
whitelist: string[],
blacklist: string[],
): boolean {
const delimiters = /[\s\t\n\r!,-\.#?()]+/;
const normalizedWhitelist = whitelist.map((word) =>
word.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "")
);
const normalizedBlacklist = blacklist.map((word) =>
word.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "")
);
const normalizedWords = new Set(
text.normalize("NFD").replace(
/[\u0300-\u036f]/g,
"",
).toLowerCase().split(delimiters),
);
return passes_whitelist(normalizedWords, normalizedWhitelist) &&
passes_blacklist(normalizedWords, normalizedBlacklist);
}
function passes_whitelist(words: Set<string>, whitelist: string[]): boolean {
if (whitelist.length === 0) return true;
// Check for at least one common element
for (const word of words) {
for (const allowed_word of whitelist) {
if (word.startsWith(allowed_word)) {
return true;
}
}
}
return false;
}
function passes_blacklist(words: Set<string>, blacklist: string[]): boolean {
if (blacklist.length === 0) return true;
for (const word of words) {
for (const blocked_word of blacklist) {
if (word.startsWith(blocked_word)) {
return false;
}
}
}
return true;
}
export function passes_reply(
tags: string[][],
reply_allowed: boolean,
): boolean {
if (reply_allowed) return true;
else if (
getTagValue(tags, "e").length === 0 && getTagValue(tags, "q").length === 0
) {
{
//console.log(tags);
return true;
}
} else return false;
}
function getTagValue(tags: string[][], key: string): string[] {
const result = tags.find((subList) => subList[0] === key);
if (result != undefined) {
if (result.length > 1) {
return result.slice(1);
}
}
return [];
}