mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-17 21:32:56 +01:00
The `Sub()` function retrieves the list of filters from the `Subscripton` object itself, and therefore it's unnecessary to pass it in. The argument is unused. Here, we remove the function argument from the definition, and from the single call site. We then pass the filters to the initializer. This makes it so that filters are in fact passed to the relay on the wire.
91 lines
1.7 KiB
Go
91 lines
1.7 KiB
Go
package nostr
|
|
|
|
import (
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
type Subscription struct {
|
|
channel string
|
|
relays map[string]*websocket.Conn
|
|
|
|
filters EventFilters
|
|
Events chan EventMessage
|
|
|
|
started bool
|
|
UniqueEvents chan Event
|
|
}
|
|
|
|
type EventMessage struct {
|
|
Event Event
|
|
Relay string
|
|
}
|
|
|
|
func (subscription Subscription) Unsub() {
|
|
for _, ws := range subscription.relays {
|
|
ws.WriteJSON([]interface{}{
|
|
"CLOSE",
|
|
subscription.channel,
|
|
})
|
|
}
|
|
|
|
if subscription.Events != nil {
|
|
close(subscription.Events)
|
|
}
|
|
if subscription.UniqueEvents != nil {
|
|
close(subscription.UniqueEvents)
|
|
}
|
|
}
|
|
|
|
func (subscription Subscription) Sub() {
|
|
for _, ws := range subscription.relays {
|
|
message := []interface{}{
|
|
"REQ",
|
|
subscription.channel,
|
|
}
|
|
for _, filter := range subscription.filters {
|
|
message = append(message, filter)
|
|
}
|
|
|
|
ws.WriteJSON(message)
|
|
}
|
|
|
|
if !subscription.started {
|
|
go subscription.startHandlingUnique()
|
|
}
|
|
}
|
|
|
|
func (subscription Subscription) startHandlingUnique() {
|
|
seen := make(map[string]struct{})
|
|
for em := range subscription.Events {
|
|
if _, ok := seen[em.Event.ID]; ok {
|
|
continue
|
|
}
|
|
seen[em.Event.ID] = struct{}{}
|
|
subscription.UniqueEvents <- em.Event
|
|
}
|
|
}
|
|
|
|
func (subscription Subscription) removeRelay(relay string) {
|
|
if ws, ok := subscription.relays[relay]; ok {
|
|
delete(subscription.relays, relay)
|
|
ws.WriteJSON([]interface{}{
|
|
"CLOSE",
|
|
subscription.channel,
|
|
})
|
|
}
|
|
}
|
|
|
|
func (subscription Subscription) addRelay(relay string, ws *websocket.Conn) {
|
|
subscription.relays[relay] = ws
|
|
|
|
message := []interface{}{
|
|
"REQ",
|
|
subscription.channel,
|
|
}
|
|
for _, filter := range subscription.filters {
|
|
message = append(message, filter)
|
|
}
|
|
|
|
ws.WriteJSON(message)
|
|
}
|