-
Notifications
You must be signed in to change notification settings - Fork 44
/
grpc_query.go
205 lines (162 loc) · 6.9 KB
/
grpc_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package keeper
import (
"context"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/desmos-labs/desmos/x/staging/posts/types"
subspacestypes "github.com/desmos-labs/desmos/x/staging/subspaces/types"
)
var _ types.QueryServer = Keeper{}
// Posts implements the Query/Posts gRPC method
func (k Keeper) Posts(goCtx context.Context, req *types.QueryPostsRequest) (*types.QueryPostsResponse, error) {
var posts []types.Post
ctx := sdk.UnwrapSDKContext(goCtx)
if !subspacestypes.IsValidSubspace(req.SubspaceId) {
return nil, sdkerrors.Wrapf(subspacestypes.ErrInvalidSubspaceID, req.SubspaceId)
}
store := ctx.KVStore(k.storeKey)
postsStore := prefix.NewStore(store, types.SubspacePostsPrefix(req.SubspaceId))
pageRes, err := query.Paginate(postsStore, req.Pagination, func(key []byte, value []byte) error {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.PostStoreKey(string(value)))
var post types.Post
if err := k.cdc.UnmarshalBinaryBare(bz, &post); err != nil {
return status.Error(codes.Internal, err.Error())
}
posts = append(posts, post)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryPostsResponse{Posts: posts, Pagination: pageRes}, nil
}
// Post implements the Query/Post gRPC method
func (k Keeper) Post(goCtx context.Context, req *types.QueryPostRequest) (*types.QueryPostResponse, error) {
if !types.IsValidPostID(req.PostId) {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid post id: %s", req.PostId)
}
ctx := sdk.UnwrapSDKContext(goCtx)
post, found := k.GetPost(ctx, req.PostId)
if !found {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "post with id %s not found", req.PostId)
}
return &types.QueryPostResponse{Post: post}, nil
}
// UserAnswers implements the Query/UserAnswers gRPC method
func (k Keeper) UserAnswers(goCtx context.Context, req *types.QueryUserAnswersRequest) (*types.QueryUserAnswersResponse, error) {
if !types.IsValidPostID(req.PostId) {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid post id: %s", req.PostId)
}
ctx := sdk.UnwrapSDKContext(goCtx)
post, found := k.GetPost(ctx, req.PostId)
if !found {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "post with id %s not found", req.PostId)
}
if post.PollData == nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "post with id %s has no poll associated", req.PostId)
}
var answers []types.UserAnswer
store := ctx.KVStore(k.storeKey)
answersStore := prefix.NewStore(store, types.UserAnswersStoreKey(req.PostId, req.User))
pageRes, err := query.Paginate(answersStore, req.Pagination, func(key []byte, value []byte) error {
var answer types.UserAnswer
if err := k.cdc.UnmarshalBinaryBare(value, &answer); err != nil {
return status.Error(codes.Internal, err.Error())
}
answers = append(answers, answer)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryUserAnswersResponse{Answers: answers, Pagination: pageRes}, nil
}
// RegisteredReactions implements the Query/RegisteredReactions gRPC method
func (k Keeper) RegisteredReactions(goCtx context.Context, req *types.QueryRegisteredReactionsRequest) (*types.QueryRegisteredReactionsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
var reactions []types.RegisteredReaction
store := ctx.KVStore(k.storeKey)
reactionsStore := prefix.NewStore(store, types.RegisteredReactionsPrefix(req.SubspaceId))
pageRes, err := query.Paginate(reactionsStore, req.Pagination, func(key []byte, value []byte) error {
var reaction types.RegisteredReaction
if err := k.cdc.UnmarshalBinaryBare(value, &reaction); err != nil {
return status.Error(codes.Internal, err.Error())
}
reactions = append(reactions, reaction)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryRegisteredReactionsResponse{Reactions: reactions, Pagination: pageRes}, nil
}
// Reports implements the Query/Reports gRPC method
func (k Keeper) Reports(
ctx context.Context, request *types.QueryReportsRequest,
) (*types.QueryReportsResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
reports := k.GetPostReports(sdkCtx, request.PostId)
return &types.QueryReportsResponse{Reports: reports}, nil
}
func (k Keeper) Params(goCtx context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
params := k.GetParams(ctx)
return &types.QueryParamsResponse{Params: params}, nil
}
// PostReactions implements the Query/PostReactions gRPC method
func (k Keeper) PostReactions(goCtx context.Context, req *types.QueryPostReactionsRequest) (*types.QueryPostReactionsResponse, error) {
if !types.IsValidPostID(req.PostId) {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid post id: %s", req.PostId)
}
ctx := sdk.UnwrapSDKContext(goCtx)
_, found := k.GetPost(ctx, req.PostId)
if !found {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "post with id %s not found", req.PostId)
}
var reactions []types.PostReaction
store := ctx.KVStore(k.storeKey)
reactionsStore := prefix.NewStore(store, types.PostReactionsPrefix(req.PostId))
pageRes, err := query.Paginate(reactionsStore, req.Pagination, func(key []byte, value []byte) error {
var reaction types.PostReaction
if err := k.cdc.UnmarshalBinaryBare(value, &reaction); err != nil {
return status.Error(codes.Internal, err.Error())
}
reactions = append(reactions, reaction)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryPostReactionsResponse{Reactions: reactions, Pagination: pageRes}, nil
}
// PostComments implements the Query/PostComments gRPC method
func (k Keeper) PostComments(
goCtx context.Context, req *types.QueryPostCommentsRequest,
) (*types.QueryPostCommentsResponse, error) {
if !types.IsValidPostID(req.PostId) {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid post id: %s", req.PostId)
}
ctx := sdk.UnwrapSDKContext(goCtx)
_, found := k.GetPost(ctx, req.PostId)
if !found {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "post with id %s not found", req.PostId)
}
store := ctx.KVStore(k.storeKey)
commetsStore := prefix.NewStore(store, types.PostCommentsPrefix(req.PostId))
var comments []types.Post
pageRes, err := query.Paginate(commetsStore, req.Pagination, func(key []byte, value []byte) error {
// it assumes that the comment must exist
comment, _ := k.GetPost(ctx, string(value))
comments = append(comments, comment)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryPostCommentsResponse{Comments: comments, Pagination: pageRes}, nil
}