Skip to content

Commit

Permalink
Add filterPolicy to block events that don't match the filter
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgleason committed Mar 28, 2023
1 parent 734c8ff commit f309142
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
53 changes: 53 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions entrypoint.example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//bin/true; exec deno run -A "$0" "$@"
import {
antiDuplicationPolicy,
filterPolicy,
hellthreadPolicy,
keywordPolicy,
noopPolicy,
Expand All @@ -16,6 +17,7 @@ import {
for await (const msg of readStdin()) {
const result = await pipeline(msg, [
noopPolicy,
[filterPolicy, { kinds: [0, 1, 3, 5, 7, 1984, 9734, 9735, 10002] }],
[keywordPolicy, ['https://t.me/']],
[regexPolicy, /(🟠|🔥|😳)ChtaGPT/i],
[pubkeyBanPolicy, ['e810fafa1e89cdf80cced8e013938e87e21b699b24c8570537be92aec4b12c18']],
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as antiDuplicationPolicy } from './src/policies/anti-duplication-policy.ts';
export { default as filterPolicy } from './src/policies/filter-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';
Expand Down
13 changes: 13 additions & 0 deletions src/policies/filter-policy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { assertEquals } from '../deps.ts';
import { buildEvent, buildInputMessage } from '../test.ts';

import filterPolicy from './filter-policy.ts';

Deno.test('only allows events that match the filter', async () => {
const msg0 = buildInputMessage({ event: buildEvent({ kind: 0 }) });
const msg1 = buildInputMessage({ event: buildEvent({ kind: 1 }) });

assertEquals((await filterPolicy(msg0, { kinds: [1] })).action, 'reject');
assertEquals((await filterPolicy(msg1, { kinds: [1] })).action, 'accept');
assertEquals((await filterPolicy(msg1, { kinds: [1], authors: [] })).action, 'reject');
});
24 changes: 24 additions & 0 deletions src/policies/filter-policy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Policy } from '../types.ts';

import { Filter, matchFilter } from 'npm:nostr-tools@^1.7.4';

/** Reject all events which don't match the filter. */
const filterPolicy: Policy<Filter> = ({ event }, filter = {}) => {
if (matchFilter(filter, event)) {
return {
id: event.id,
action: 'accept',
msg: '',
};
}

return {
id: event.id,
action: 'reject',
msg: 'blocked: the event doesn\'t match the allowed filters',
};
};

export default filterPolicy;

export type { Filter };

0 comments on commit f309142

Please sign in to comment.