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-06-22 18:55:51 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/filter"
|
2022-08-09 13:48:23 +00:00
|
|
|
)
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
type filterArgument struct {
|
2023-06-22 18:55:51 +00:00
|
|
|
Topic string `json:"pubsubTopic,omitempty"`
|
|
|
|
ContentTopics []string `json:"contentTopics,omitempty"`
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 18:55:51 +00:00
|
|
|
func toContentFilter(filterJSON string) (filter.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-06-22 18:55:51 +00:00
|
|
|
return filter.ContentFilter{}, err
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 18:55:51 +00:00
|
|
|
return filter.ContentFilter{
|
|
|
|
Topic: f.Topic,
|
|
|
|
ContentTopics: f.ContentTopics,
|
|
|
|
}, nil
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
// FilterSubscribe is used to create a subscription to a filter node to receive messages
|
|
|
|
func FilterSubscribe(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-02-16 22:09:21 +00:00
|
|
|
if wakuState.node == nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return "", errWakuNodeNotReady
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
|
|
|
|
if ms > 0 {
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(int(ms))*time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
} else {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
|
|
|
|
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-06-22 18:55:51 +00:00
|
|
|
subscriptionDetails, err := wakuState.node.FilterLightnode().Subscribe(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-06-22 18:55:51 +00:00
|
|
|
go func(subscriptionDetails *filter.SubscriptionDetails) {
|
|
|
|
for envelope := range subscriptionDetails.C {
|
2022-08-09 13:48:23 +00:00
|
|
|
send("message", toSubscriptionMessage(envelope))
|
|
|
|
}
|
2023-06-22 18:55:51 +00:00
|
|
|
}(subscriptionDetails)
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
return marshalJSON(subscriptionDetails)
|
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
|
|
|
|
func FilterPing(peerID string, ms int) error {
|
2023-06-22 18:55:51 +00:00
|
|
|
if wakuState.node == nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return errWakuNodeNotReady
|
2023-06-22 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
|
|
|
|
if ms > 0 {
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(int(ms))*time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
} else {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-10 13:30:38 +00:00
|
|
|
return wakuState.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
|
|
|
|
func FilterUnsubscribe(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-02-16 22:09:21 +00:00
|
|
|
if wakuState.node == nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return errWakuNodeNotReady
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
|
|
|
|
if ms > 0 {
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(int(ms))*time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
} else {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
|
|
|
|
2023-06-22 18:55:51 +00:00
|
|
|
var fOptions []filter.FilterUnsubscribeOption
|
|
|
|
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
|
|
|
}
|
|
|
|
fOptions = append(fOptions, filter.Peer(p))
|
|
|
|
} else {
|
2023-08-10 13:30:38 +00:00
|
|
|
return errors.New("peerID is required")
|
2023-06-22 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pushResult, err := wakuState.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-06-22 18:55:51 +00:00
|
|
|
result := <-pushResult
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
return result.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
|
|
|
|
func FilterUnsubscribeAll(peerID string, ms int) (string, error) {
|
2023-06-22 18:55:51 +00:00
|
|
|
if wakuState.node == nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return "", errWakuNodeNotReady
|
2023-06-22 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
|
|
|
|
if ms > 0 {
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(int(ms))*time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
} else {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
|
|
|
|
|
|
|
var fOptions []filter.FilterUnsubscribeOption
|
|
|
|
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
|
|
|
}
|
|
|
|
fOptions = append(fOptions, filter.Peer(p))
|
|
|
|
} else {
|
|
|
|
fOptions = append(fOptions, filter.UnsubscribeAll())
|
|
|
|
}
|
|
|
|
|
|
|
|
pushResult, err := wakuState.node.FilterLightnode().UnsubscribeAll(ctx, fOptions...)
|
|
|
|
if err != nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return "", err
|
2023-06-22 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var unsubscribeResult []unsubscribeAllResult
|
|
|
|
|
|
|
|
for result := range pushResult {
|
|
|
|
ur := unsubscribeAllResult{
|
|
|
|
PeerID: result.PeerID.Pretty(),
|
|
|
|
}
|
|
|
|
if result.Err != nil {
|
|
|
|
ur.Error = result.Err.Error()
|
|
|
|
}
|
|
|
|
unsubscribeResult = append(unsubscribeResult, ur)
|
|
|
|
}
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
return marshalJSON(unsubscribeResult)
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|