2022-01-02 08:44:18 -03:00
|
|
|
package nostr
|
2021-02-20 17:44:05 -03:00
|
|
|
|
2022-11-26 09:25:51 -03:00
|
|
|
import (
|
2023-01-01 20:22:40 -03:00
|
|
|
"context"
|
2023-04-06 16:21:25 -03:00
|
|
|
"fmt"
|
2023-03-17 16:43:48 -03:00
|
|
|
"strconv"
|
2022-11-26 09:25:51 -03:00
|
|
|
"sync"
|
|
|
|
)
|
2022-11-16 10:07:15 -03:00
|
|
|
|
2021-02-20 17:44:05 -03:00
|
|
|
type Subscription struct {
|
2023-03-18 15:09:49 -03:00
|
|
|
label string
|
|
|
|
counter int
|
|
|
|
conn *Connection
|
|
|
|
mutex sync.Mutex
|
2021-02-20 17:44:05 -03:00
|
|
|
|
2022-11-26 09:25:51 -03:00
|
|
|
Relay *Relay
|
2022-11-19 14:00:29 -03:00
|
|
|
Filters Filters
|
2023-01-26 09:04:27 -03:00
|
|
|
Events chan *Event
|
2022-11-12 21:49:57 -03:00
|
|
|
EndOfStoredEvents chan struct{}
|
2023-03-16 14:15:16 -03:00
|
|
|
Context context.Context
|
2023-03-21 14:50:34 -03:00
|
|
|
cancel context.CancelFunc
|
2022-02-07 11:33:45 -03:00
|
|
|
|
2022-11-16 10:07:15 -03:00
|
|
|
stopped bool
|
|
|
|
emitEose sync.Once
|
2021-02-20 17:59:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
type EventMessage struct {
|
2022-01-02 08:44:18 -03:00
|
|
|
Event Event
|
2021-02-20 17:59:47 -03:00
|
|
|
Relay string
|
2021-02-20 17:44:05 -03:00
|
|
|
}
|
|
|
|
|
2023-03-18 08:39:31 -03:00
|
|
|
// SetLabel puts a label on the subscription that is prepended to the id that is sent to relays,
|
|
|
|
// it's only useful for debugging and sanity purposes.
|
|
|
|
func (sub *Subscription) SetLabel(label string) {
|
|
|
|
sub.label = label
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetID return the Nostr subscription ID as given to the relay, it will be a sequential number, stringified.
|
2023-03-17 16:43:48 -03:00
|
|
|
func (sub *Subscription) GetID() string {
|
2023-03-18 15:09:49 -03:00
|
|
|
return sub.label + ":" + strconv.Itoa(sub.counter)
|
2023-03-17 16:43:48 -03:00
|
|
|
}
|
|
|
|
|
2023-01-16 06:27:11 -05:00
|
|
|
// Unsub closes the subscription, sending "CLOSE" to relay as in NIP-01.
|
|
|
|
// Unsub() also closes the channel sub.Events.
|
2022-11-26 09:25:51 -03:00
|
|
|
func (sub *Subscription) Unsub() {
|
|
|
|
sub.mutex.Lock()
|
|
|
|
defer sub.mutex.Unlock()
|
2021-02-20 17:44:05 -03:00
|
|
|
|
2023-04-12 12:14:16 -03:00
|
|
|
message := []any{"CLOSE", sub.GetID()}
|
|
|
|
debugLog("{%s} sending %v", sub.Relay.URL, message)
|
|
|
|
sub.conn.WriteJSON(message)
|
|
|
|
|
2022-11-26 09:25:51 -03:00
|
|
|
if sub.stopped == false && sub.Events != nil {
|
2022-11-06 21:15:42 -03:00
|
|
|
close(sub.Events)
|
2021-02-20 17:44:05 -03:00
|
|
|
}
|
2022-11-26 09:25:51 -03:00
|
|
|
sub.stopped = true
|
2021-02-20 17:44:05 -03:00
|
|
|
}
|
|
|
|
|
2023-01-16 06:27:11 -05:00
|
|
|
// Sub sets sub.Filters and then calls sub.Fire(ctx).
|
2023-01-01 20:22:40 -03:00
|
|
|
func (sub *Subscription) Sub(ctx context.Context, filters Filters) {
|
2022-11-19 14:00:29 -03:00
|
|
|
sub.Filters = filters
|
2023-03-21 14:50:34 -03:00
|
|
|
sub.Fire()
|
2022-11-19 14:00:29 -03:00
|
|
|
}
|
2021-12-16 20:47:53 -03:00
|
|
|
|
2023-01-16 06:27:11 -05:00
|
|
|
// Fire sends the "REQ" command to the relay.
|
2023-03-21 14:50:34 -03:00
|
|
|
func (sub *Subscription) Fire() error {
|
2023-03-18 15:09:49 -03:00
|
|
|
sub.Relay.subscriptions.Store(sub.GetID(), sub)
|
|
|
|
|
2023-04-12 12:14:24 -03:00
|
|
|
message := []any{"REQ", sub.GetID()}
|
2022-11-19 14:00:29 -03:00
|
|
|
for _, filter := range sub.Filters {
|
2021-12-16 20:47:53 -03:00
|
|
|
message = append(message, filter)
|
|
|
|
}
|
|
|
|
|
2023-04-11 09:07:37 -03:00
|
|
|
debugLog("{%s} sending %v", sub.Relay.URL, message)
|
2023-04-11 00:28:52 -03:00
|
|
|
|
2023-03-18 08:39:31 -03:00
|
|
|
err := sub.conn.WriteJSON(message)
|
|
|
|
if err != nil {
|
2023-03-21 14:50:34 -03:00
|
|
|
sub.cancel()
|
2023-04-06 16:21:25 -03:00
|
|
|
return fmt.Errorf("failed to write: %w", err)
|
2023-03-18 08:39:31 -03:00
|
|
|
}
|
2023-01-01 20:22:40 -03:00
|
|
|
|
|
|
|
// the subscription ends once the context is canceled
|
|
|
|
go func() {
|
2023-03-21 14:50:34 -03:00
|
|
|
<-sub.Context.Done()
|
2023-01-01 20:22:40 -03:00
|
|
|
sub.Unsub()
|
|
|
|
}()
|
2023-03-16 14:15:16 -03:00
|
|
|
|
|
|
|
// or when the relay connection is closed
|
|
|
|
go func() {
|
|
|
|
<-sub.Relay.ConnectionContext.Done()
|
|
|
|
|
2023-03-18 16:07:19 -03:00
|
|
|
// cancel the context -- this will cause the other context cancelation cause above to be called
|
2023-03-21 14:50:34 -03:00
|
|
|
sub.cancel()
|
2023-03-16 14:15:16 -03:00
|
|
|
}()
|
2023-03-18 08:39:31 -03:00
|
|
|
|
|
|
|
return nil
|
2021-02-20 17:44:05 -03:00
|
|
|
}
|