(filter: SubscriptionFilter)
| 38 | |
| 39 | export const isEventMatchingFilter = |
| 40 | (filter: SubscriptionFilter) => |
| 41 | (event: Event): boolean => { |
| 42 | const startsWith = (input: string) => (prefix: string) => input.startsWith(prefix) |
| 43 | const isMatchingGenericTagCriterion = (key: string, criterion: string) => (tag: Tag): boolean => { |
| 44 | const [, tagName] = key |
| 45 | if (tag[0] !== tagName) { |
| 46 | return false |
| 47 | } |
| 48 | |
| 49 | if (isGeohashPrefixCriterion(key, criterion)) { |
| 50 | return tag[1].startsWith(stripGeohashPrefixWildcard(criterion)) |
| 51 | } |
| 52 | |
| 53 | return tag[1] === criterion |
| 54 | } |
| 55 | |
| 56 | // NIP-01: Basic protocol flow description |
| 57 | |
| 58 | if (Array.isArray(filter.ids) && !filter.ids.some(startsWith(event.id))) { |
| 59 | return false |
| 60 | } |
| 61 | |
| 62 | if (Array.isArray(filter.kinds) && !filter.kinds.includes(event.kind)) { |
| 63 | return false |
| 64 | } |
| 65 | |
| 66 | if (typeof filter.since === 'number' && event.created_at < filter.since) { |
| 67 | return false |
| 68 | } |
| 69 | |
| 70 | if (typeof filter.until === 'number' && event.created_at > filter.until) { |
| 71 | return false |
| 72 | } |
| 73 | |
| 74 | if (Array.isArray(filter.authors)) { |
| 75 | if (!filter.authors.some(startsWith(event.pubkey))) { |
| 76 | return false |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // NIP-27: Multicast |
| 81 | // const targetMulticastGroups: string[] = event.tags.reduce( |
| 82 | // (acc, tag) => (tag[0] === EventTags.Multicast) |
| 83 | // ? [...acc, tag[1]] |
| 84 | // : acc, |
| 85 | // [] as string[] |
| 86 | // ) |
| 87 | |
| 88 | // if (targetMulticastGroups.length && !Array.isArray(filter['#m'])) { |
| 89 | // return false |
| 90 | // } |
| 91 | |
| 92 | // NIP-01: Support #e and #p tags |
| 93 | // NIP-12: Support generic tag queries |
| 94 | |
| 95 | if ( |
| 96 | Object.entries(filter) |
| 97 | .filter(([key, criteria]) => isGenericTagQuery(key) && Array.isArray(criteria)) |
no test coverage detected