forked from relayable-org/strfry-policies
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add filterPolicy to block events that don't match the filter
- Loading branch information
1 parent
734c8ff
commit f309142
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |