2021-11-09 14:15:45 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/node"
|
|
|
|
"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-01-18 18:17:06 +00:00
|
|
|
"go.uber.org/zap"
|
2021-11-09 14:15:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type StoreService struct {
|
|
|
|
node *node.WakuNode
|
2022-05-30 15:55:30 +00:00
|
|
|
log *zap.Logger
|
2021-11-09 14:15:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// cursor *pb.Index
|
|
|
|
// pageSize uint64
|
|
|
|
// asc bool
|
|
|
|
|
|
|
|
type StorePagingOptions struct {
|
|
|
|
PageSize uint64 `json:"pageSize,omitempty"`
|
|
|
|
Cursor *pb.Index `json:"cursor,omitempty"`
|
|
|
|
Forward bool `json:"forward,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type StoreMessagesArgs struct {
|
|
|
|
Topic string `json:"pubsubTopic,omitempty"`
|
|
|
|
ContentFilters []string `json:"contentFilters,omitempty"`
|
2022-02-23 15:01:53 +00:00
|
|
|
StartTime int64 `json:"startTime,omitempty"`
|
|
|
|
EndTime int64 `json:"endTime,omitempty"`
|
2021-11-09 14:15:45 +00:00
|
|
|
PagingOptions StorePagingOptions `json:"pagingOptions,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type StoreMessagesReply struct {
|
2023-02-17 03:35:22 +00:00
|
|
|
Messages []*RPCWakuMessage `json:"messages,omitempty"`
|
2021-11-09 14:15:45 +00:00
|
|
|
PagingInfo StorePagingOptions `json:"pagingInfo,omitempty"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StoreService) GetV1Messages(req *http.Request, args *StoreMessagesArgs, reply *StoreMessagesReply) error {
|
|
|
|
options := []store.HistoryRequestOption{
|
2023-07-19 16:25:35 +00:00
|
|
|
store.WithAutomaticRequestID(),
|
2021-11-09 14:15:45 +00:00
|
|
|
store.WithAutomaticPeerSelection(),
|
|
|
|
store.WithPaging(args.PagingOptions.Forward, args.PagingOptions.PageSize),
|
|
|
|
store.WithCursor(args.PagingOptions.Cursor),
|
|
|
|
}
|
|
|
|
res, err := s.node.Store().Query(
|
|
|
|
req.Context(),
|
|
|
|
store.Query{
|
|
|
|
Topic: args.Topic,
|
|
|
|
ContentTopics: args.ContentFilters,
|
|
|
|
StartTime: args.StartTime,
|
|
|
|
EndTime: args.EndTime,
|
|
|
|
},
|
|
|
|
options...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-05-30 15:55:30 +00:00
|
|
|
s.log.Error("querying messages", zap.Error(err))
|
2021-11-09 14:15:45 +00:00
|
|
|
reply.Error = err.Error()
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-13 18:30:35 +00:00
|
|
|
|
2023-02-17 03:35:22 +00:00
|
|
|
reply.Messages = make([]*RPCWakuMessage, len(res.Messages))
|
|
|
|
for i := range res.Messages {
|
|
|
|
reply.Messages[i] = ProtoToRPC(res.Messages[i])
|
|
|
|
}
|
2022-06-13 18:30:35 +00:00
|
|
|
|
2021-11-09 14:15:45 +00:00
|
|
|
reply.PagingInfo = StorePagingOptions{
|
|
|
|
PageSize: args.PagingOptions.PageSize,
|
|
|
|
Cursor: res.Cursor(),
|
|
|
|
Forward: args.PagingOptions.Forward,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|