2022-08-09 13:48:23 +00:00
|
|
|
package gowaku
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/filter"
|
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/pb"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FilterArgument struct {
|
2022-08-18 19:26:23 +00:00
|
|
|
Topic string `json:"pubsubTopic,omitempty"`
|
2022-08-09 13:48:23 +00:00
|
|
|
ContentFilters []pb.ContentFilter `json:"contentFilters,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func toContentFilter(filterJSON string) (filter.ContentFilter, error) {
|
|
|
|
var f FilterArgument
|
|
|
|
err := json.Unmarshal([]byte(filterJSON), &f)
|
|
|
|
if err != nil {
|
|
|
|
return filter.ContentFilter{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := filter.ContentFilter{
|
|
|
|
Topic: f.Topic,
|
|
|
|
}
|
|
|
|
for _, cf := range f.ContentFilters {
|
|
|
|
result.ContentTopics = append(result.ContentTopics, cf.ContentTopic)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func FilterSubscribe(filterJSON string, peerID string, ms int) string {
|
|
|
|
cf, err := toContentFilter(filterJSON)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if wakuNode == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(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()
|
|
|
|
}
|
|
|
|
|
|
|
|
var fOptions []filter.FilterSubscribeOption
|
|
|
|
if peerID != "" {
|
|
|
|
p, err := peer.Decode(peerID)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
fOptions = append(fOptions, filter.WithPeer(p))
|
|
|
|
} else {
|
|
|
|
fOptions = append(fOptions, filter.WithAutomaticPeerSelection())
|
|
|
|
}
|
|
|
|
|
|
|
|
_, f, err := wakuNode.Filter().Subscribe(ctx, cf, fOptions...)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func(f filter.Filter) {
|
|
|
|
for envelope := range f.Chan {
|
|
|
|
send("message", toSubscriptionMessage(envelope))
|
|
|
|
}
|
|
|
|
}(f)
|
|
|
|
|
2022-10-09 15:08:46 +00:00
|
|
|
return PrepareJSONResponse(true, nil)
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func FilterUnsubscribe(filterJSON string, ms int) string {
|
|
|
|
cf, err := toContentFilter(filterJSON)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if wakuNode == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(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()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = wakuNode.Filter().UnsubscribeFilter(ctx, cf)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(nil)
|
2022-08-09 13:48:23 +00:00
|
|
|
}
|