summaryrefslogtreecommitdiffstats
path: root/internal/subscription/manager.go
blob: 0e737d856444770e11c3e41b862a65a726c6b911 (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
package subscription

import (
	"sync"

	pb "northwest.io/nostr-grpc/api/nostr/v1"
)

type Subscription struct {
	ID      string
	Filters []*pb.Filter
	Events  chan *pb.Event
	done    chan struct{}
	once    sync.Once
}

func (s *Subscription) InitDone() {
	s.done = make(chan struct{})
}

func (s *Subscription) Done() <-chan struct{} {
	return s.done
}

func (s *Subscription) Close() {
	s.once.Do(func() {
		close(s.done)
		close(s.Events)
	})
}

func (s *Subscription) IsClosed() bool {
	select {
	case <-s.done:
		return true
	default:
		return false
	}
}

type Manager struct {
	mu            sync.RWMutex
	subscriptions map[string]*Subscription
}

func NewManager() *Manager {
	return &Manager{
		subscriptions: make(map[string]*Subscription),
	}
}

func (m *Manager) Add(sub *Subscription) {
	m.mu.Lock()
	defer m.mu.Unlock()
	m.subscriptions[sub.ID] = sub
}

func (m *Manager) Remove(id string) {
	m.mu.Lock()
	defer m.mu.Unlock()

	if sub, ok := m.subscriptions[id]; ok {
		sub.Close()
		delete(m.subscriptions, id)
	}
}

func (m *Manager) Get(id string) (*Subscription, bool) {
	m.mu.RLock()
	defer m.mu.RUnlock()
	sub, ok := m.subscriptions[id]
	return sub, ok
}

func (m *Manager) MatchAndFan(event *pb.Event) {
	m.mu.RLock()
	defer m.mu.RUnlock()

	for _, sub := range m.subscriptions {
		if sub.IsClosed() {
			continue
		}

		if matchesAnyFilter(event, sub.Filters) {
			select {
			case sub.Events <- event:
			case <-sub.done:
			default:
			}
		}
	}
}

func matchesAnyFilter(event *pb.Event, filters []*pb.Filter) bool {
	for _, filter := range filters {
		if matchesFilter(event, filter) {
			return true
		}
	}
	return false
}

func matchesFilter(event *pb.Event, filter *pb.Filter) bool {
	if len(filter.Ids) > 0 {
		if !matchesPrefix(event.Id, filter.Ids) {
			return false
		}
	}

	if len(filter.Authors) > 0 {
		if !matchesPrefix(event.Pubkey, filter.Authors) {
			return false
		}
	}

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

	if filter.Since != nil && *filter.Since > 0 {
		if event.CreatedAt < *filter.Since {
			return false
		}
	}

	if filter.Until != nil && *filter.Until > 0 {
		if event.CreatedAt > *filter.Until {
			return false
		}
	}

	if len(filter.ETags) > 0 {
		if !hasTag(event, "e", filter.ETags) {
			return false
		}
	}

	if len(filter.PTags) > 0 {
		if !hasTag(event, "p", filter.PTags) {
			return false
		}
	}

	for tagName, tagFilter := range filter.TagFilters {
		if len(tagFilter.Values) > 0 {
			if !hasTag(event, tagName, tagFilter.Values) {
				return false
			}
		}
	}

	return true
}

func matchesPrefix(value string, prefixes []string) bool {
	for _, prefix := range prefixes {
		if len(prefix) == len(value) {
			if value == prefix {
				return true
			}
		} else if len(value) > len(prefix) {
			if value[:len(prefix)] == prefix {
				return true
			}
		}
	}
	return false
}

func hasTag(event *pb.Event, tagName string, values []string) bool {
	for _, tag := range event.Tags {
		if len(tag.Values) >= 2 && tag.Values[0] == tagName {
			for _, val := range values {
				if tag.Values[1] == val {
					return true
				}
			}
		}
	}
	return false
}