Add keyword policy

This commit is contained in:
Alex Gleason 2023-03-26 16:40:35 -05:00
parent 31fd761278
commit d38f5e06cf
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 48 additions and 0 deletions

View File

@ -3,6 +3,7 @@
import { import {
antiDuplicationPolicy, antiDuplicationPolicy,
hellthreadPolicy, hellthreadPolicy,
keywordPolicy,
noopPolicy, noopPolicy,
pipeline, pipeline,
pubkeyBanPolicy, pubkeyBanPolicy,
@ -18,6 +19,7 @@ for await (const msg of readStdin()) {
[antiDuplicationPolicy, { ttl: 60000, minLength: 50 }], [antiDuplicationPolicy, { ttl: 60000, minLength: 50 }],
[rateLimitPolicy, { whitelist: ['127.0.0.1'] }], [rateLimitPolicy, { whitelist: ['127.0.0.1'] }],
[pubkeyBanPolicy, ['e810fafa1e89cdf80cced8e013938e87e21b699b24c8570537be92aec4b12c18']], [pubkeyBanPolicy, ['e810fafa1e89cdf80cced8e013938e87e21b699b24c8570537be92aec4b12c18']],
[keywordPolicy, ['https://t.me/']],
]); ]);
writeStdout(result); writeStdout(result);

2
mod.ts
View File

@ -1,9 +1,11 @@
export { default as antiDuplicationPolicy } from './src/policies/anti-duplication-policy.ts'; export { default as antiDuplicationPolicy } from './src/policies/anti-duplication-policy.ts';
export { default as hellthreadPolicy } from './src/policies/hellthread-policy.ts'; export { default as hellthreadPolicy } from './src/policies/hellthread-policy.ts';
export { default as keywordPolicy } from './src/policies/keyword-policy.ts';
export { default as noopPolicy } from './src/policies/noop-policy.ts'; export { default as noopPolicy } from './src/policies/noop-policy.ts';
export { default as pubkeyBanPolicy } from './src/policies/pubkey-ban-policy.ts'; export { default as pubkeyBanPolicy } from './src/policies/pubkey-ban-policy.ts';
export { default as rateLimitPolicy } from './src/policies/rate-limit-policy.ts'; export { default as rateLimitPolicy } from './src/policies/rate-limit-policy.ts';
export { default as readOnlyPolicy } from './src/policies/read-only-policy.ts'; export { default as readOnlyPolicy } from './src/policies/read-only-policy.ts';
export { default as whitelistPolicy } from './src/policies/whitelist-policy.ts';
export { readStdin, writeStdout } from './src/io.ts'; export { readStdin, writeStdout } from './src/io.ts';
export { default as pipeline } from './src/pipeline.ts'; export { default as pipeline } from './src/pipeline.ts';

View File

@ -0,0 +1,15 @@
import { assert } from '../deps.ts';
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 msg0 = buildInputMessage();
const msg1 = buildInputMessage({ event: buildEvent({ content: '🔥🔥🔥 https://t.me/spam 我想死' }) });
assert((await keywordPolicy(msg0, words)).action === 'accept');
assert((await keywordPolicy(msg1, words)).action === 'reject');
assert((await keywordPolicy(msg1, [])).action === 'accept');
});

View File

@ -0,0 +1,29 @@
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 (isMatch) {
return {
id,
action: 'reject',
msg: 'Event contains a banned word or phrase.',
};
}
return {
id,
action: 'accept',
msg: '',
};
};
export default keywordPolicy;