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"
|
2023-06-25 00:17:39 -03:00
|
|
|
"sync/atomic"
|
2022-11-26 09:25:51 -03:00
|
|
|
)
|
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
|
2023-06-09 11:01:42 -03:00
|
|
|
counter int
|
2021-02-20 17:44:05 -03:00
|
|
|
|
2023-06-21 19:55:40 -03:00
|
|
|
Relay *Relay
|
|
|
|
Filters Filters
|
|
|
|
|
2023-07-18 16:17:00 -03:00
|
|
|
// for this to be treated as a COUNT and not a REQ this must be set
|
|
|
|
countResult chan int64
|
|
|
|
|
2023-06-21 19:55:40 -03:00
|
|
|
// the Events channel emits all EVENTs that come in a Subscription
|
|
|
|
// will be closed when the subscription ends
|
|
|
|
Events chan *Event
|
2023-09-04 08:57:51 -03:00
|
|
|
mu sync.Mutex
|
2023-06-21 19:55:40 -03:00
|
|
|
|
|
|
|
// the EndOfStoredEvents channel gets closed when an EOSE comes for that subscription
|
2022-11-12 21:49:57 -03:00
|
|
|
EndOfStoredEvents chan struct{}
|
2022-02-07 11:33:45 -03:00
|
|
|
|
2023-11-28 18:54:24 -03:00
|
|
|
// the ClosedReason channel emits the reason when a CLOSED message is received
|
|
|
|
ClosedReason chan string
|
|
|
|
|
2023-06-21 19:55:40 -03:00
|
|
|
// Context will be .Done() when the subscription ends
|
|
|
|
Context context.Context
|
|
|
|
|
2023-08-22 10:41:58 -03:00
|
|
|
live atomic.Bool
|
|
|
|
eosed atomic.Bool
|
2023-11-28 18:54:24 -03:00
|
|
|
closed atomic.Bool
|
2023-08-22 10:41:58 -03:00
|
|
|
cancel context.CancelFunc
|
2023-09-04 08:33:31 -03:00
|
|
|
|
|
|
|
// this keeps track of the events we've received before the EOSE that we must dispatch before
|
|
|
|
// closing the EndOfStoredEvents channel
|
|
|
|
storedwg sync.WaitGroup
|
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-06-21 19:55:40 -03:00
|
|
|
// When instantiating relay connections, some options may be passed.
|
|
|
|
// SubscriptionOption is the type of the argument passed for that.
|
|
|
|
// Some examples are WithLabel.
|
|
|
|
type SubscriptionOption interface {
|
|
|
|
IsSubscriptionOption()
|
2023-03-18 08:39:31 -03:00
|
|
|
}
|
|
|
|
|
2023-06-21 19:55:40 -03:00
|
|
|
// WithLabel puts a label on the subscription (it is prepended to the automatic id) that is sent to relays.
|
|
|
|
type WithLabel string
|
|
|
|
|
|
|
|
func (_ WithLabel) IsSubscriptionOption() {}
|
|
|
|
|
|
|
|
var _ SubscriptionOption = (WithLabel)("")
|
|
|
|
|
|
|
|
// GetID return the Nostr subscription ID as given to the Relay
|
|
|
|
// it is a concatenation of the label and a serial number.
|
2023-03-17 16:43:48 -03:00
|
|
|
func (sub *Subscription) GetID() string {
|
2023-06-09 11:01:42 -03:00
|
|
|
return sub.label + ":" + strconv.Itoa(sub.counter)
|
2023-03-17 16:43:48 -03:00
|
|
|
}
|
|
|
|
|
2023-06-25 00:17:39 -03:00
|
|
|
func (sub *Subscription) start() {
|
2023-09-04 08:57:51 -03:00
|
|
|
<-sub.Context.Done()
|
|
|
|
// the subscription ends once the context is canceled (if not already)
|
|
|
|
sub.Unsub() // this will set sub.live to false
|
|
|
|
|
|
|
|
// do this so we don't have the possibility of closing the Events channel and then trying to send to it
|
|
|
|
sub.mu.Lock()
|
|
|
|
close(sub.Events)
|
|
|
|
sub.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sub *Subscription) dispatchEvent(evt *Event) {
|
|
|
|
added := false
|
|
|
|
if !sub.eosed.Load() {
|
|
|
|
sub.storedwg.Add(1)
|
|
|
|
added = true
|
2023-06-25 00:17:39 -03:00
|
|
|
}
|
2023-09-04 08:57:51 -03:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
sub.mu.Lock()
|
|
|
|
defer sub.mu.Unlock()
|
|
|
|
|
|
|
|
if sub.live.Load() {
|
|
|
|
select {
|
|
|
|
case sub.Events <- evt:
|
|
|
|
case <-sub.Context.Done():
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if added {
|
|
|
|
sub.storedwg.Done()
|
|
|
|
}
|
|
|
|
}()
|
2023-06-25 00:17:39 -03:00
|
|
|
}
|
|
|
|
|
2023-09-04 08:33:31 -03:00
|
|
|
func (sub *Subscription) dispatchEose() {
|
|
|
|
if sub.eosed.CompareAndSwap(false, true) {
|
|
|
|
go func() {
|
|
|
|
sub.storedwg.Wait()
|
2024-01-01 10:16:07 -03:00
|
|
|
sub.EndOfStoredEvents <- struct{}{}
|
2023-09-04 08:33:31 -03:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 18:54:24 -03:00
|
|
|
func (sub *Subscription) dispatchClosed(reason string) {
|
|
|
|
if sub.closed.CompareAndSwap(false, true) {
|
2024-01-01 10:16:07 -03:00
|
|
|
go func() {
|
|
|
|
sub.ClosedReason <- reason
|
|
|
|
}()
|
2023-11-28 18:54:24 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 06:27:11 -05:00
|
|
|
// Unsub closes the subscription, sending "CLOSE" to relay as in NIP-01.
|
2023-06-21 19:55:40 -03:00
|
|
|
// Unsub() also closes the channel sub.Events and makes a new one.
|
2022-11-26 09:25:51 -03:00
|
|
|
func (sub *Subscription) Unsub() {
|
2023-08-22 10:41:58 -03:00
|
|
|
// cancel the context (if it's not canceled already)
|
2023-06-25 00:17:39 -03:00
|
|
|
sub.cancel()
|
2023-06-23 16:22:57 -03:00
|
|
|
|
2023-08-22 10:41:58 -03:00
|
|
|
// mark subscription as closed and send a CLOSE to the relay (naïve sync.Once implementation)
|
2023-06-25 00:17:39 -03:00
|
|
|
if sub.live.CompareAndSwap(true, false) {
|
2023-08-22 10:41:58 -03:00
|
|
|
sub.Close()
|
2023-06-25 00:17:39 -03:00
|
|
|
}
|
2023-08-22 10:41:58 -03:00
|
|
|
|
|
|
|
// remove subscription from our map
|
|
|
|
sub.Relay.Subscriptions.Delete(sub.GetID())
|
2023-06-23 16:22:57 -03:00
|
|
|
}
|
2023-04-12 12:14:16 -03:00
|
|
|
|
2023-06-23 16:22:57 -03:00
|
|
|
// Close just sends a CLOSE message. You probably want Unsub() instead.
|
|
|
|
func (sub *Subscription) Close() {
|
2023-06-21 19:55:40 -03:00
|
|
|
if sub.Relay.IsConnected() {
|
2023-06-23 16:22:57 -03:00
|
|
|
id := sub.GetID()
|
2023-06-21 19:55:40 -03:00
|
|
|
closeMsg := CloseEnvelope(id)
|
|
|
|
closeb, _ := (&closeMsg).MarshalJSON()
|
2023-07-11 15:25:02 -03:00
|
|
|
debugLogf("{%s} sending %v", sub.Relay.URL, closeb)
|
2023-06-25 12:56:17 -03:00
|
|
|
<-sub.Relay.Write(closeb)
|
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-05-30 14:55:44 -03:00
|
|
|
// The subscription will be closed if the context expires.
|
2023-08-21 20:17:25 +08:00
|
|
|
func (sub *Subscription) Sub(_ 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-05-30 14:55:44 -03:00
|
|
|
id := sub.GetID()
|
2023-03-18 15:09:49 -03:00
|
|
|
|
2023-07-18 16:17:00 -03:00
|
|
|
var reqb []byte
|
|
|
|
if sub.countResult == nil {
|
|
|
|
reqb, _ = ReqEnvelope{id, sub.Filters}.MarshalJSON()
|
|
|
|
} else {
|
|
|
|
reqb, _ = CountEnvelope{id, sub.Filters, nil}.MarshalJSON()
|
|
|
|
}
|
2023-07-11 15:25:02 -03:00
|
|
|
debugLogf("{%s} sending %v", sub.Relay.URL, reqb)
|
2023-06-21 19:55:40 -03:00
|
|
|
|
2023-06-25 00:17:39 -03:00
|
|
|
sub.live.Store(true)
|
2023-06-22 10:46:31 -03:00
|
|
|
if err := <-sub.Relay.Write(reqb); 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
|
|
|
|
2023-03-18 08:39:31 -03:00
|
|
|
return nil
|
2021-02-20 17:44:05 -03:00
|
|
|
}
|