2023-08-10 13:30:38 +00:00
|
|
|
package library
|
2022-08-09 13:48:23 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2023-06-22 18:55:51 +00:00
|
|
|
"errors"
|
2022-08-09 13:48:23 +00:00
|
|
|
"time"
|
|
|
|
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2023-09-29 05:13:25 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
2023-06-22 18:55:51 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/filter"
|
2023-09-29 05:13:25 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/subscription"
|
2022-08-09 13:48:23 +00:00
|
|
|
)
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
type filterArgument struct {
|
2023-09-19 12:52:11 +00:00
|
|
|
PubsubTopic string `json:"pubsubTopic,omitempty"`
|
2023-06-22 18:55:51 +00:00
|
|
|
ContentTopics []string `json:"contentTopics,omitempty"`
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
2023-09-29 05:13:25 +00:00
|
|
|
func toContentFilter(filterJSON string) (protocol.ContentFilter, error) {
|
2023-08-10 13:30:38 +00:00
|
|
|
var f filterArgument
|
2022-08-09 13:48:23 +00:00
|
|
|
err := json.Unmarshal([]byte(filterJSON), &f)
|
|
|
|
if err != nil {
|
2023-09-29 05:13:25 +00:00
|
|
|
return protocol.ContentFilter{}, err
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
2023-09-29 05:13:25 +00:00
|
|
|
return protocol.ContentFilter{
|
2023-09-19 12:52:11 +00:00
|
|
|
PubsubTopic: f.PubsubTopic,
|
2023-09-29 05:13:25 +00:00
|
|
|
ContentTopics: protocol.NewContentTopicSet(f.ContentTopics...),
|
2023-06-22 18:55:51 +00:00
|
|
|
}, nil
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 05:56:55 +00:00
|
|
|
type subscribeResult struct {
|
2023-09-29 05:13:25 +00:00
|
|
|
Subscriptions []*subscription.SubscriptionDetails `json:"subscriptions"`
|
|
|
|
Error string `json:"error,omitempty"`
|
2023-09-20 05:56:55 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
// FilterSubscribe is used to create a subscription to a filter node to receive messages
|
2023-12-15 14:46:21 +00:00
|
|
|
func FilterSubscribe(instance *WakuInstance, filterJSON string, peerID string, ms int) (string, error) {
|
2022-08-09 13:48:23 +00:00
|
|
|
cf, err := toContentFilter(filterJSON)
|
|
|
|
if err != nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return "", err
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:46:21 +00:00
|
|
|
if err := validateInstance(instance, MustBeStarted); err != nil {
|
|
|
|
return "", err
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
|
|
|
|
if ms > 0 {
|
2023-12-15 14:46:21 +00:00
|
|
|
ctx, cancel = context.WithTimeout(instance.ctx, time.Duration(int(ms))*time.Millisecond)
|
2022-08-09 13:48:23 +00:00
|
|
|
defer cancel()
|
|
|
|
} else {
|
2023-12-15 14:46:21 +00:00
|
|
|
ctx = instance.ctx
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 18:55:51 +00:00
|
|
|
var fOptions []filter.FilterSubscribeOption
|
2022-08-09 13:48:23 +00:00
|
|
|
if peerID != "" {
|
|
|
|
p, err := peer.Decode(peerID)
|
|
|
|
if err != nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return "", err
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
2023-06-22 18:55:51 +00:00
|
|
|
fOptions = append(fOptions, filter.WithPeer(p))
|
2022-08-09 13:48:23 +00:00
|
|
|
} else {
|
2023-06-22 18:55:51 +00:00
|
|
|
fOptions = append(fOptions, filter.WithAutomaticPeerSelection())
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:46:21 +00:00
|
|
|
subscriptions, err := instance.node.FilterLightnode().Subscribe(ctx, cf, fOptions...)
|
2023-09-20 05:56:55 +00:00
|
|
|
if err != nil && subscriptions == nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return "", err
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 05:56:55 +00:00
|
|
|
for _, subscriptionDetails := range subscriptions {
|
2023-09-29 05:13:25 +00:00
|
|
|
go func(subscriptionDetails *subscription.SubscriptionDetails) {
|
2023-09-20 05:56:55 +00:00
|
|
|
for envelope := range subscriptionDetails.C {
|
2023-12-15 14:46:21 +00:00
|
|
|
send(instance, "message", toSubscriptionMessage(envelope))
|
2023-09-20 05:56:55 +00:00
|
|
|
}
|
|
|
|
}(subscriptionDetails)
|
|
|
|
}
|
|
|
|
var subResult subscribeResult
|
|
|
|
subResult.Subscriptions = subscriptions
|
2023-10-28 23:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
subResult.Error = err.Error()
|
|
|
|
}
|
|
|
|
|
2023-09-20 05:56:55 +00:00
|
|
|
return marshalJSON(subResult)
|
2023-06-22 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
// FilterPing is used to determine if a peer has an active subscription
|
2023-12-15 14:46:21 +00:00
|
|
|
func FilterPing(instance *WakuInstance, peerID string, ms int) error {
|
|
|
|
if err := validateInstance(instance, MustBeStarted); err != nil {
|
|
|
|
return err
|
2023-06-22 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
|
|
|
|
if ms > 0 {
|
2023-12-15 14:46:21 +00:00
|
|
|
ctx, cancel = context.WithTimeout(instance.ctx, time.Duration(int(ms))*time.Millisecond)
|
2023-06-22 18:55:51 +00:00
|
|
|
defer cancel()
|
|
|
|
} else {
|
2023-12-15 14:46:21 +00:00
|
|
|
ctx = instance.ctx
|
2023-06-22 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var pID peer.ID
|
|
|
|
var err error
|
|
|
|
if peerID != "" {
|
|
|
|
pID, err = peer.Decode(peerID)
|
|
|
|
if err != nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return err
|
2023-06-22 18:55:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-08-10 13:30:38 +00:00
|
|
|
return errors.New("peerID is required")
|
2023-06-22 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:46:21 +00:00
|
|
|
return instance.node.FilterLightnode().Ping(ctx, pID)
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
// FilterUnsubscribe is used to remove a filter criteria from an active subscription with a filter node
|
2023-12-15 14:46:21 +00:00
|
|
|
func FilterUnsubscribe(instance *WakuInstance, filterJSON string, peerID string, ms int) error {
|
2022-08-09 13:48:23 +00:00
|
|
|
cf, err := toContentFilter(filterJSON)
|
|
|
|
if err != nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return err
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:46:21 +00:00
|
|
|
if err := validateInstance(instance, MustBeStarted); err != nil {
|
|
|
|
return err
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
|
|
|
|
if ms > 0 {
|
2023-12-15 14:46:21 +00:00
|
|
|
ctx, cancel = context.WithTimeout(instance.ctx, time.Duration(int(ms))*time.Millisecond)
|
2022-08-09 13:48:23 +00:00
|
|
|
defer cancel()
|
|
|
|
} else {
|
2023-12-15 14:46:21 +00:00
|
|
|
ctx = instance.ctx
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 10:36:04 +00:00
|
|
|
var fOptions []filter.FilterSubscribeOption
|
2023-06-22 18:55:51 +00:00
|
|
|
if peerID != "" {
|
|
|
|
p, err := peer.Decode(peerID)
|
|
|
|
if err != nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return err
|
2023-06-22 18:55:51 +00:00
|
|
|
}
|
2023-09-21 10:36:04 +00:00
|
|
|
fOptions = append(fOptions, filter.WithPeer(p))
|
2023-06-22 18:55:51 +00:00
|
|
|
} else {
|
2023-08-10 13:30:38 +00:00
|
|
|
return errors.New("peerID is required")
|
2023-06-22 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:46:21 +00:00
|
|
|
result, err := instance.node.FilterLightnode().Unsubscribe(ctx, cf, fOptions...)
|
2022-08-09 13:48:23 +00:00
|
|
|
if err != nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return err
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
2023-11-04 07:16:24 +00:00
|
|
|
errs := result.Errors()
|
|
|
|
if len(errs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errs[0].Err
|
2023-06-22 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type unsubscribeAllResult struct {
|
|
|
|
PeerID string `json:"peerID"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
// FilterUnsubscribeAll is used to remove an active subscription to a peer. If no peerID is defined, it will stop all active filter subscriptions
|
2023-12-15 14:46:21 +00:00
|
|
|
func FilterUnsubscribeAll(instance *WakuInstance, peerID string, ms int) (string, error) {
|
|
|
|
if err := validateInstance(instance, MustBeStarted); err != nil {
|
|
|
|
return "", err
|
2023-06-22 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
|
|
|
|
if ms > 0 {
|
2023-12-15 14:46:21 +00:00
|
|
|
ctx, cancel = context.WithTimeout(instance.ctx, time.Duration(int(ms))*time.Millisecond)
|
2023-06-22 18:55:51 +00:00
|
|
|
defer cancel()
|
|
|
|
} else {
|
2023-12-15 14:46:21 +00:00
|
|
|
ctx = instance.ctx
|
2023-06-22 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 10:36:04 +00:00
|
|
|
var fOptions []filter.FilterSubscribeOption
|
2023-06-22 18:55:51 +00:00
|
|
|
if peerID != "" {
|
|
|
|
p, err := peer.Decode(peerID)
|
|
|
|
if err != nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return "", err
|
2023-06-22 18:55:51 +00:00
|
|
|
}
|
2023-09-21 10:36:04 +00:00
|
|
|
fOptions = append(fOptions, filter.WithPeer(p))
|
2023-06-22 18:55:51 +00:00
|
|
|
} else {
|
|
|
|
fOptions = append(fOptions, filter.UnsubscribeAll())
|
|
|
|
}
|
|
|
|
|
2023-12-15 14:46:21 +00:00
|
|
|
result, err := instance.node.FilterLightnode().UnsubscribeAll(ctx, fOptions...)
|
2023-06-22 18:55:51 +00:00
|
|
|
if err != nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return "", err
|
2023-06-22 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var unsubscribeResult []unsubscribeAllResult
|
|
|
|
|
2023-11-04 07:16:24 +00:00
|
|
|
for _, err := range result.Errors() {
|
2023-06-22 18:55:51 +00:00
|
|
|
ur := unsubscribeAllResult{
|
2023-11-04 07:16:24 +00:00
|
|
|
PeerID: err.PeerID.Pretty(),
|
2023-06-22 18:55:51 +00:00
|
|
|
}
|
2023-11-04 07:16:24 +00:00
|
|
|
if err.Err != nil {
|
|
|
|
ur.Error = err.Err.Error()
|
2023-06-22 18:55:51 +00:00
|
|
|
}
|
|
|
|
unsubscribeResult = append(unsubscribeResult, ur)
|
|
|
|
}
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
return marshalJSON(unsubscribeResult)
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|