summaryrefslogtreecommitdiffstats
path: root/filter.go
blob: dde04a53c36d5554a62ba541b03160eec403fcc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package nostr

import (
	"encoding/json"
	"strings"
)

// Filter represents a subscription filter as defined in NIP-01.
type Filter struct {
	IDs     []string            `json:"ids,omitempty"`
	Kinds   []int               `json:"kinds,omitempty"`
	Authors []string            `json:"authors,omitempty"`
	Tags    map[string][]string `json:"-"` // Custom marshaling for #e, #p, etc.
	Since   *int64              `json:"since,omitempty"`
	Until   *int64              `json:"until,omitempty"`
	Limit   int                 `json:"limit,omitempty"`
}

// MarshalJSON implements json.Marshaler for Filter.
// Converts Tags map to #e, #p format.
func (f Filter) MarshalJSON() ([]byte, error) {
	// Create a map for custom marshaling
	m := make(map[string]interface{})

	if len(f.IDs) > 0 {
		m["ids"] = f.IDs
	}
	if len(f.Kinds) > 0 {
		m["kinds"] = f.Kinds
	}
	if len(f.Authors) > 0 {
		m["authors"] = f.Authors
	}
	if f.Since != nil {
		m["since"] = *f.Since
	}
	if f.Until != nil {
		m["until"] = *f.Until
	}
	if f.Limit > 0 {
		m["limit"] = f.Limit
	}

	// Add tag filters with # prefix
	for key, values := range f.Tags {
		if len(values) > 0 {
			m["#"+key] = values
		}
	}

	return json.Marshal(m)
}

// UnmarshalJSON implements json.Unmarshaler for Filter.
// Extracts #e, #p format into Tags map.
func (f *Filter) UnmarshalJSON(data []byte) error {
	// First unmarshal into a raw map
	var raw map[string]json.RawMessage
	if err := json.Unmarshal(data, &raw); err != nil {
		return err
	}

	// Extract known fields
	if v, ok := raw["ids"]; ok {
		if err := json.Unmarshal(v, &f.IDs); err != nil {
			return err
		}
	}
	if v, ok := raw["kinds"]; ok {
		if err := json.Unmarshal(v, &f.Kinds); err != nil {
			return err
		}
	}
	if v, ok := raw["authors"]; ok {
		if err := json.Unmarshal(v, &f.Authors); err != nil {
			return err
		}
	}
	if v, ok := raw["since"]; ok {
		var since int64
		if err := json.Unmarshal(v, &since); err != nil {
			return err
		}
		f.Since = &since
	}
	if v, ok := raw["until"]; ok {
		var until int64
		if err := json.Unmarshal(v, &until); err != nil {
			return err
		}
		f.Until = &until
	}
	if v, ok := raw["limit"]; ok {
		if err := json.Unmarshal(v, &f.Limit); err != nil {
			return err
		}
	}

	// Extract tag filters (fields starting with #)
	f.Tags = make(map[string][]string)
	for key, value := range raw {
		if strings.HasPrefix(key, "#") {
			tagKey := strings.TrimPrefix(key, "#")
			var values []string
			if err := json.Unmarshal(value, &values); err != nil {
				return err
			}
			f.Tags[tagKey] = values
		}
	}

	return nil
}

// Matches checks if an event matches this filter.
func (f *Filter) Matches(event *Event) bool {
	// Check IDs (prefix match)
	if len(f.IDs) > 0 {
		found := false
		for _, id := range f.IDs {
			if strings.HasPrefix(event.ID, id) {
				found = true
				break
			}
		}
		if !found {
			return false
		}
	}

	// Check authors (prefix match)
	if len(f.Authors) > 0 {
		found := false
		for _, author := range f.Authors {
			if strings.HasPrefix(event.PubKey, author) {
				found = true
				break
			}
		}
		if !found {
			return false
		}
	}

	// Check kinds
	if len(f.Kinds) > 0 {
		found := false
		for _, kind := range f.Kinds {
			if event.Kind == kind {
				found = true
				break
			}
		}
		if !found {
			return false
		}
	}

	// Check since
	if f.Since != nil && event.CreatedAt < *f.Since {
		return false
	}

	// Check until
	if f.Until != nil && event.CreatedAt > *f.Until {
		return false
	}

	// Check tag filters
	for tagKey, values := range f.Tags {
		if len(values) == 0 {
			continue
		}
		found := false
		for _, val := range values {
			if event.Tags.ContainsValue(tagKey, val) {
				found = true
				break
			}
		}
		if !found {
			return false
		}
	}

	return true
}

// Clone creates a deep copy of the filter.
func (f *Filter) Clone() *Filter {
	clone := &Filter{
		Limit: f.Limit,
	}

	if f.IDs != nil {
		clone.IDs = make([]string, len(f.IDs))
		copy(clone.IDs, f.IDs)
	}
	if f.Kinds != nil {
		clone.Kinds = make([]int, len(f.Kinds))
		copy(clone.Kinds, f.Kinds)
	}
	if f.Authors != nil {
		clone.Authors = make([]string, len(f.Authors))
		copy(clone.Authors, f.Authors)
	}
	if f.Since != nil {
		since := *f.Since
		clone.Since = &since
	}
	if f.Until != nil {
		until := *f.Until
		clone.Until = &until
	}
	if f.Tags != nil {
		clone.Tags = make(map[string][]string)
		for k, v := range f.Tags {
			clone.Tags[k] = make([]string, len(v))
			copy(clone.Tags[k], v)
		}
	}

	return clone
}