MCPcopy Index your code
hub / github.com/cameri/nostream / isEventMatchingFilter

Function isEventMatchingFilter

src/utils/event.ts:40–106  ·  view source on GitHub ↗
(filter: SubscriptionFilter)

Source from the content-addressed store, hash-verified

38
39export 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))

Callers 1

event.spec.tsFile · 0.90

Calls 3

isGenericTagQueryFunction · 0.90
startsWithFunction · 0.85

Tested by

no test coverage detected