2023-08-10 13:30:38 +00:00
|
|
|
package library
|
2022-04-12 12:12:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
|
2024-09-25 09:15:20 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
2022-04-12 12:12:14 +00:00
|
|
|
)
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
// RelayEnoughPeers determines if there are enough peers to publish a message on a topic
|
2023-12-15 14:46:21 +00:00
|
|
|
func RelayEnoughPeers(instance *WakuInstance, topic string) (bool, error) {
|
|
|
|
if err := validateInstance(instance, MustBeStarted); err != nil {
|
|
|
|
return false, err
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 14:56:26 +00:00
|
|
|
topicToCheck := protocol.DefaultPubsubTopic{}.String()
|
2022-04-12 12:12:14 +00:00
|
|
|
if topic != "" {
|
|
|
|
topicToCheck = topic
|
|
|
|
}
|
|
|
|
|
2023-12-15 14:46:21 +00:00
|
|
|
return instance.node.Relay().EnoughPeersToPublishToTopic(topicToCheck), nil
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:46:21 +00:00
|
|
|
func relayPublish(instance *WakuInstance, msg *pb.WakuMessage, pubsubTopic string, ms int) (string, error) {
|
|
|
|
if err := validateInstance(instance, MustBeStarted); err != nil {
|
|
|
|
return "", err
|
2022-04-12 12:12:14 +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-04-12 12:12:14 +00:00
|
|
|
defer cancel()
|
|
|
|
} else {
|
2023-12-15 14:46:21 +00:00
|
|
|
ctx = instance.ctx
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:46:21 +00:00
|
|
|
hash, err := instance.node.Relay().Publish(ctx, msg, relay.WithPubSubTopic(pubsubTopic))
|
2024-05-03 16:07:03 +00:00
|
|
|
|
|
|
|
return hash.String(), err
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
// RelayPublish publishes a message using waku relay and returns the message ID
|
2023-12-15 14:46:21 +00:00
|
|
|
func RelayPublish(instance *WakuInstance, messageJSON string, topic string, ms int) (string, error) {
|
2022-04-12 12:12:14 +00:00
|
|
|
msg, err := wakuMessage(messageJSON)
|
|
|
|
if err != nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return "", err
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:46:21 +00:00
|
|
|
return relayPublish(instance, msg, topic, int(ms))
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:46:21 +00:00
|
|
|
func relaySubscribe(instance *WakuInstance, filterJSON string) error {
|
2023-10-28 23:37:53 +00:00
|
|
|
cf, err := toContentFilter(filterJSON)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:46:21 +00:00
|
|
|
subscriptions, err := instance.node.Relay().Subscribe(context.Background(), cf)
|
2022-04-12 12:12:14 +00:00
|
|
|
if err != nil {
|
2023-02-17 17:30:59 +00:00
|
|
|
return err
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-10-28 23:37:53 +00:00
|
|
|
for _, sub := range subscriptions {
|
|
|
|
go func(subscription *relay.Subscription) {
|
2024-09-25 09:15:20 +00:00
|
|
|
defer utils.LogOnPanic()
|
2023-10-28 23:37:53 +00:00
|
|
|
for envelope := range subscription.Ch {
|
2023-12-15 14:46:21 +00:00
|
|
|
send(instance, "message", toSubscriptionMessage(envelope))
|
2023-10-28 23:37:53 +00:00
|
|
|
}
|
|
|
|
}(sub)
|
|
|
|
}
|
2022-04-12 12:12:14 +00:00
|
|
|
|
2023-02-17 17:30:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
// RelaySubscribe subscribes to a WakuRelay topic.
|
2023-12-15 14:46:21 +00:00
|
|
|
func RelaySubscribe(instance *WakuInstance, contentFilterJSON string) error {
|
|
|
|
if err := validateInstance(instance, MustBeStarted); err != nil {
|
|
|
|
return err
|
2023-02-17 17:30:59 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:46:21 +00:00
|
|
|
return relaySubscribe(instance, contentFilterJSON)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
// RelayTopics returns a list of pubsub topics the node is subscribed to in WakuRelay
|
2023-12-15 14:46:21 +00:00
|
|
|
func RelayTopics(instance *WakuInstance) (string, error) {
|
|
|
|
if err := validateInstance(instance, MustBeStarted); err != nil {
|
|
|
|
return "", err
|
2023-03-29 19:20:31 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:46:21 +00:00
|
|
|
return marshalJSON(instance.node.Relay().Topics())
|
2023-03-29 19:20:31 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
// RelayUnsubscribe closes the pubsub subscription to a pubsub topic
|
2023-12-15 14:46:21 +00:00
|
|
|
func RelayUnsubscribe(instance *WakuInstance, contentFilterJSON string) error {
|
2023-10-28 23:37:53 +00:00
|
|
|
cf, err := toContentFilter(contentFilterJSON)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:46:21 +00:00
|
|
|
if err := validateInstance(instance, MustBeStarted); err != nil {
|
|
|
|
return err
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:46:21 +00:00
|
|
|
return instance.node.Relay().Unsubscribe(context.Background(), cf)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|