More refactoring
This commit is contained in:
parent
48501f3c5f
commit
b32bfef342
@ -16,10 +16,10 @@ interface AntiDuplication {
|
||||
* It stores a hashcode for each content in an SQLite database and rate-limits them.
|
||||
* Only messages that meet the minimum length criteria are selected.
|
||||
*/
|
||||
const antiDuplicationPolicy: Policy<AntiDuplication> = async (msg, opts) => {
|
||||
const ttl = opts?.ttl ?? 60000;
|
||||
const minLength = opts?.minLength ?? 50;
|
||||
const databaseUrl = opts?.databaseUrl || 'sqlite:///tmp/strfry-anti-duplication-policy.sqlite3';
|
||||
const antiDuplicationPolicy: Policy<AntiDuplication> = async (msg, opts = {}) => {
|
||||
const ttl = opts.ttl ?? 60000;
|
||||
const minLength = opts.minLength ?? 50;
|
||||
const databaseUrl = opts.databaseUrl ?? 'sqlite:///tmp/strfry-anti-duplication-policy.sqlite3';
|
||||
|
||||
const { kind, content } = msg.event;
|
||||
|
||||
|
@ -4,12 +4,14 @@ import { buildEvent, buildInputMessage } from '../test.ts';
|
||||
import keywordPolicy from './keyword-policy.ts';
|
||||
|
||||
Deno.test('blocks banned pubkeys', async () => {
|
||||
const words = ['https://t.me/spam'];
|
||||
const words = ['https://t.me/spam', 'hello world'];
|
||||
|
||||
const msg0 = buildInputMessage();
|
||||
const msg1 = buildInputMessage({ event: buildEvent({ content: '🔥🔥🔥 https://t.me/spam 我想死' }) });
|
||||
const msg2 = buildInputMessage({ event: buildEvent({ content: 'hElLo wOrLd!' }) });
|
||||
|
||||
assertEquals((await keywordPolicy(msg0, words)).action, 'accept');
|
||||
assertEquals((await keywordPolicy(msg1, words)).action, 'reject');
|
||||
assertEquals((await keywordPolicy(msg1, [])).action, 'accept');
|
||||
assertEquals((await keywordPolicy(msg2, words)).action, 'reject');
|
||||
});
|
||||
|
@ -2,23 +2,16 @@ import { Policy } from '../types.ts';
|
||||
|
||||
/** Reject events containing any of the strings in its content. */
|
||||
const keywordPolicy: Policy<Iterable<string>> = ({ event: { id, content } }, words = []) => {
|
||||
let isMatch = false;
|
||||
|
||||
for (const word of words) {
|
||||
if (content.toLocaleLowerCase().includes(word.toLowerCase())) {
|
||||
isMatch = true;
|
||||
break;
|
||||
if (content.toLowerCase().includes(word.toLowerCase())) {
|
||||
return {
|
||||
id,
|
||||
action: 'reject',
|
||||
msg: 'blocked: contains a banned word or phrase.',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (isMatch) {
|
||||
return {
|
||||
id,
|
||||
action: 'reject',
|
||||
msg: 'blocked: contains a banned word or phrase.',
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
id,
|
||||
action: 'accept',
|
||||
|
@ -18,11 +18,13 @@ interface RateLimit {
|
||||
* IPs are stored in an SQLite database. If you are running internal services,
|
||||
* it's a good idea to at least whitelist `127.0.0.1` etc.
|
||||
*/
|
||||
const rateLimitPolicy: Policy<RateLimit> = async (msg, opts) => {
|
||||
const interval = opts?.interval ?? 60000;
|
||||
const max = opts?.max ?? 10;
|
||||
const whitelist = opts?.whitelist || [];
|
||||
const databaseUrl = opts?.databaseUrl || 'sqlite:///tmp/strfry-rate-limit-policy.sqlite3';
|
||||
const rateLimitPolicy: Policy<RateLimit> = async (msg, opts = {}) => {
|
||||
const {
|
||||
interval = 60000,
|
||||
max = 10,
|
||||
whitelist = [],
|
||||
databaseUrl = 'sqlite:///tmp/strfry-rate-limit-policy.sqlite3',
|
||||
} = opts;
|
||||
|
||||
if ((msg.sourceType === 'IP4' || msg.sourceType === 'IP6') && !whitelist.includes(msg.sourceInfo)) {
|
||||
const db = new Keydb(databaseUrl);
|
||||
|
@ -2,9 +2,7 @@ import { Policy } from '../types.ts';
|
||||
|
||||
/** Reject events whose content matches the regex. */
|
||||
const regexPolicy: Policy<RegExp> = ({ event: { id, content } }, regex) => {
|
||||
const isMatch = regex ? regex.test(content) : false;
|
||||
|
||||
if (isMatch) {
|
||||
if (regex?.test(content)) {
|
||||
return {
|
||||
id,
|
||||
action: 'reject',
|
||||
|
Loading…
Reference in New Issue
Block a user