diff options
| author | bndw <ben@bdw.to> | 2026-02-13 17:58:29 -0800 |
|---|---|---|
| committer | bndw <ben@bdw.to> | 2026-02-13 17:58:29 -0800 |
| commit | 28d6d0ea2f86d69ad003557656466a50545fc0c9 (patch) | |
| tree | 7fba1ecd6d1733cd5fbdec12d9e45318613a0249 /internal/subscription/manager.go | |
| parent | fcba12d7ae3cdb361c6321519fdaf5a537a6a871 (diff) | |
feat: implement Subscribe with real-time event streaming
Subscription manager:
- Track active subscriptions across connections
- Filter matching with full NIP-01 support
- Event fan-out to matching subscribers
Subscribe RPC:
- Query stored events (past)
- Stream them to client
- Keep stream open for new events (real-time)
- Auto-generate subscription ID if not provided
PublishEvent now:
- Stores event
- Notifies all matching active subscriptions
- Streams to gRPC clients in real-time
4 new tests, all 41 tests passing
Diffstat (limited to 'internal/subscription/manager.go')
| -rw-r--r-- | internal/subscription/manager.go | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/internal/subscription/manager.go b/internal/subscription/manager.go new file mode 100644 index 0000000..0e737d8 --- /dev/null +++ b/internal/subscription/manager.go | |||
| @@ -0,0 +1,190 @@ | |||
| 1 | package subscription | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "sync" | ||
| 5 | |||
| 6 | pb "northwest.io/nostr-grpc/api/nostr/v1" | ||
| 7 | ) | ||
| 8 | |||
| 9 | type Subscription struct { | ||
| 10 | ID string | ||
| 11 | Filters []*pb.Filter | ||
| 12 | Events chan *pb.Event | ||
| 13 | done chan struct{} | ||
| 14 | once sync.Once | ||
| 15 | } | ||
| 16 | |||
| 17 | func (s *Subscription) InitDone() { | ||
| 18 | s.done = make(chan struct{}) | ||
| 19 | } | ||
| 20 | |||
| 21 | func (s *Subscription) Done() <-chan struct{} { | ||
| 22 | return s.done | ||
| 23 | } | ||
| 24 | |||
| 25 | func (s *Subscription) Close() { | ||
| 26 | s.once.Do(func() { | ||
| 27 | close(s.done) | ||
| 28 | close(s.Events) | ||
| 29 | }) | ||
| 30 | } | ||
| 31 | |||
| 32 | func (s *Subscription) IsClosed() bool { | ||
| 33 | select { | ||
| 34 | case <-s.done: | ||
| 35 | return true | ||
| 36 | default: | ||
| 37 | return false | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | type Manager struct { | ||
| 42 | mu sync.RWMutex | ||
| 43 | subscriptions map[string]*Subscription | ||
| 44 | } | ||
| 45 | |||
| 46 | func NewManager() *Manager { | ||
| 47 | return &Manager{ | ||
| 48 | subscriptions: make(map[string]*Subscription), | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | func (m *Manager) Add(sub *Subscription) { | ||
| 53 | m.mu.Lock() | ||
| 54 | defer m.mu.Unlock() | ||
| 55 | m.subscriptions[sub.ID] = sub | ||
| 56 | } | ||
| 57 | |||
| 58 | func (m *Manager) Remove(id string) { | ||
| 59 | m.mu.Lock() | ||
| 60 | defer m.mu.Unlock() | ||
| 61 | |||
| 62 | if sub, ok := m.subscriptions[id]; ok { | ||
| 63 | sub.Close() | ||
| 64 | delete(m.subscriptions, id) | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | func (m *Manager) Get(id string) (*Subscription, bool) { | ||
| 69 | m.mu.RLock() | ||
| 70 | defer m.mu.RUnlock() | ||
| 71 | sub, ok := m.subscriptions[id] | ||
| 72 | return sub, ok | ||
| 73 | } | ||
| 74 | |||
| 75 | func (m *Manager) MatchAndFan(event *pb.Event) { | ||
| 76 | m.mu.RLock() | ||
| 77 | defer m.mu.RUnlock() | ||
| 78 | |||
| 79 | for _, sub := range m.subscriptions { | ||
| 80 | if sub.IsClosed() { | ||
| 81 | continue | ||
| 82 | } | ||
| 83 | |||
| 84 | if matchesAnyFilter(event, sub.Filters) { | ||
| 85 | select { | ||
| 86 | case sub.Events <- event: | ||
| 87 | case <-sub.done: | ||
| 88 | default: | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | func matchesAnyFilter(event *pb.Event, filters []*pb.Filter) bool { | ||
| 95 | for _, filter := range filters { | ||
| 96 | if matchesFilter(event, filter) { | ||
| 97 | return true | ||
| 98 | } | ||
| 99 | } | ||
| 100 | return false | ||
| 101 | } | ||
| 102 | |||
| 103 | func matchesFilter(event *pb.Event, filter *pb.Filter) bool { | ||
| 104 | if len(filter.Ids) > 0 { | ||
| 105 | if !matchesPrefix(event.Id, filter.Ids) { | ||
| 106 | return false | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | if len(filter.Authors) > 0 { | ||
| 111 | if !matchesPrefix(event.Pubkey, filter.Authors) { | ||
| 112 | return false | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | if len(filter.Kinds) > 0 { | ||
| 117 | found := false | ||
| 118 | for _, kind := range filter.Kinds { | ||
| 119 | if event.Kind == kind { | ||
| 120 | found = true | ||
| 121 | break | ||
| 122 | } | ||
| 123 | } | ||
| 124 | if !found { | ||
| 125 | return false | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | if filter.Since != nil && *filter.Since > 0 { | ||
| 130 | if event.CreatedAt < *filter.Since { | ||
| 131 | return false | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | if filter.Until != nil && *filter.Until > 0 { | ||
| 136 | if event.CreatedAt > *filter.Until { | ||
| 137 | return false | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | if len(filter.ETags) > 0 { | ||
| 142 | if !hasTag(event, "e", filter.ETags) { | ||
| 143 | return false | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | if len(filter.PTags) > 0 { | ||
| 148 | if !hasTag(event, "p", filter.PTags) { | ||
| 149 | return false | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | for tagName, tagFilter := range filter.TagFilters { | ||
| 154 | if len(tagFilter.Values) > 0 { | ||
| 155 | if !hasTag(event, tagName, tagFilter.Values) { | ||
| 156 | return false | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | return true | ||
| 162 | } | ||
| 163 | |||
| 164 | func matchesPrefix(value string, prefixes []string) bool { | ||
| 165 | for _, prefix := range prefixes { | ||
| 166 | if len(prefix) == len(value) { | ||
| 167 | if value == prefix { | ||
| 168 | return true | ||
| 169 | } | ||
| 170 | } else if len(value) > len(prefix) { | ||
| 171 | if value[:len(prefix)] == prefix { | ||
| 172 | return true | ||
| 173 | } | ||
| 174 | } | ||
| 175 | } | ||
| 176 | return false | ||
| 177 | } | ||
| 178 | |||
| 179 | func hasTag(event *pb.Event, tagName string, values []string) bool { | ||
| 180 | for _, tag := range event.Tags { | ||
| 181 | if len(tag.Values) >= 2 && tag.Values[0] == tagName { | ||
| 182 | for _, val := range values { | ||
| 183 | if tag.Values[1] == val { | ||
| 184 | return true | ||
| 185 | } | ||
| 186 | } | ||
| 187 | } | ||
| 188 | } | ||
| 189 | return false | ||
| 190 | } | ||
