2022-04-12 12:12:14 +00:00
|
|
|
package gowaku
|
|
|
|
|
|
|
|
import (
|
|
|
|
"C"
|
|
|
|
"encoding/json"
|
|
|
|
|
2023-02-06 22:16:20 +00:00
|
|
|
wpb "github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/store"
|
2023-02-06 22:16:20 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/store/pb"
|
2022-04-12 12:12:14 +00:00
|
|
|
)
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2022-04-12 12:12:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type storePagingOptions struct {
|
|
|
|
PageSize uint64 `json:"pageSize,omitempty"`
|
|
|
|
Cursor *pb.Index `json:"cursor,omitempty"`
|
|
|
|
Forward bool `json:"forward,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type storeMessagesArgs struct {
|
2023-02-06 22:16:20 +00:00
|
|
|
Topic string `json:"pubsubTopic,omitempty"`
|
|
|
|
ContentFilters []*pb.ContentFilter `json:"contentFilters,omitempty"`
|
|
|
|
StartTime int64 `json:"startTime,omitempty"`
|
|
|
|
EndTime int64 `json:"endTime,omitempty"`
|
|
|
|
PagingOptions storePagingOptions `json:"pagingOptions,omitempty"`
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type storeMessagesReply struct {
|
2023-02-06 22:16:20 +00:00
|
|
|
Messages []*wpb.WakuMessage `json:"messages,omitempty"`
|
2022-04-12 12:12:14 +00:00
|
|
|
PagingInfo storePagingOptions `json:"pagingInfo,omitempty"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-02-10 20:17:23 +00:00
|
|
|
func queryResponse(ctx context.Context, args storeMessagesArgs, options []store.HistoryRequestOption) string {
|
|
|
|
var contentTopics []string
|
|
|
|
for _, ct := range args.ContentFilters {
|
|
|
|
contentTopics = append(contentTopics, ct.ContentTopic)
|
|
|
|
}
|
|
|
|
|
2023-02-16 22:09:21 +00:00
|
|
|
res, err := wakuState.node.Store().Query(
|
2023-02-10 20:17:23 +00:00
|
|
|
ctx,
|
|
|
|
store.Query{
|
|
|
|
Topic: args.Topic,
|
|
|
|
ContentTopics: contentTopics,
|
|
|
|
StartTime: args.StartTime,
|
|
|
|
EndTime: args.EndTime,
|
|
|
|
},
|
|
|
|
options...,
|
|
|
|
)
|
|
|
|
|
|
|
|
reply := storeMessagesReply{}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
reply.Error = err.Error()
|
|
|
|
return PrepareJSONResponse(reply, nil)
|
|
|
|
}
|
|
|
|
reply.Messages = res.Messages
|
|
|
|
reply.PagingInfo = storePagingOptions{
|
|
|
|
PageSize: args.PagingOptions.PageSize,
|
|
|
|
Cursor: res.Cursor(),
|
|
|
|
Forward: args.PagingOptions.Forward,
|
|
|
|
}
|
|
|
|
|
|
|
|
return PrepareJSONResponse(reply, nil)
|
|
|
|
}
|
|
|
|
|
2022-04-12 12:12:14 +00:00
|
|
|
func StoreQuery(queryJSON string, peerID string, ms int) string {
|
2023-02-16 22:09:21 +00:00
|
|
|
if wakuState.node == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var args storeMessagesArgs
|
|
|
|
err := json.Unmarshal([]byte(queryJSON), &args)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
options := []store.HistoryRequestOption{
|
|
|
|
store.WithAutomaticRequestId(),
|
|
|
|
store.WithPaging(args.PagingOptions.Forward, args.PagingOptions.PageSize),
|
|
|
|
store.WithCursor(args.PagingOptions.Cursor),
|
|
|
|
}
|
|
|
|
|
|
|
|
if peerID != "" {
|
|
|
|
p, err := peer.Decode(peerID)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
options = append(options, store.WithPeer(p))
|
|
|
|
} else {
|
|
|
|
options = append(options, store.WithAutomaticPeerSelection())
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-10 20:17:23 +00:00
|
|
|
return queryResponse(ctx, args, options)
|
|
|
|
}
|
2022-08-09 18:26:57 +00:00
|
|
|
|
2023-02-10 20:17:23 +00:00
|
|
|
func StoreLocalQuery(queryJSON string) string {
|
2023-02-16 22:09:21 +00:00
|
|
|
if wakuState.node == nil {
|
2023-02-10 20:17:23 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
|
|
|
}
|
2022-04-12 12:12:14 +00:00
|
|
|
|
2023-02-10 20:17:23 +00:00
|
|
|
var args storeMessagesArgs
|
|
|
|
err := json.Unmarshal([]byte(queryJSON), &args)
|
2022-04-12 12:12:14 +00:00
|
|
|
if err != nil {
|
2023-02-10 20:17:23 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
2023-02-10 20:17:23 +00:00
|
|
|
|
|
|
|
options := []store.HistoryRequestOption{
|
|
|
|
store.WithAutomaticRequestId(),
|
|
|
|
store.WithPaging(args.PagingOptions.Forward, args.PagingOptions.PageSize),
|
|
|
|
store.WithCursor(args.PagingOptions.Cursor),
|
|
|
|
store.WithLocalQuery(),
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 20:17:23 +00:00
|
|
|
return queryResponse(context.TODO(), args, options)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|