-
Notifications
You must be signed in to change notification settings - Fork 46
/
grpc_query.go
146 lines (112 loc) · 5.04 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
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/v4/x/reactions/types"
)
var _ types.QueryServer = &Keeper{}
// Reactions implements the QueryReactions gRPC method
func (k Keeper) Reactions(ctx context.Context, request *types.QueryReactionsRequest) (*types.QueryReactionsResponse, error) {
if request.SubspaceId == 0 {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid subspace id")
}
if request.PostId == 0 {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid post id")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
store := sdkCtx.KVStore(k.storeKey)
storePrefix := types.PostReactionsPrefix(request.SubspaceId, request.PostId)
reactionsStore := prefix.NewStore(store, storePrefix)
var reactions []types.Reaction
pageRes, err := query.FilteredPaginate(reactionsStore, request.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
var reaction types.Reaction
if err := k.cdc.Unmarshal(value, &reaction); err != nil {
return false, status.Error(codes.Internal, err.Error())
}
// Skip all the reactions from a different author if the user is specified
if request.User != "" && request.User != reaction.Author {
return false, nil
}
if accumulate {
reactions = append(reactions, reaction)
}
return true, nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryReactionsResponse{Reactions: reactions, Pagination: pageRes}, nil
}
// Reaction implements the QueryReaction gRPC method
func (k Keeper) Reaction(ctx context.Context, request *types.QueryReactionRequest) (*types.QueryReactionResponse, error) {
if request.SubspaceId == 0 {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid subspace id")
}
if request.PostId == 0 {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid post id")
}
if request.ReactionId == 0 {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid reaction id")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
reaction, found := k.GetReaction(sdkCtx, request.SubspaceId, request.PostId, request.ReactionId)
if !found {
return nil, sdkerrors.Wrapf(sdkerrors.ErrNotFound, "reaction with id %d not found inside subspace %d", request.ReactionId, request.SubspaceId)
}
return &types.QueryReactionResponse{Reaction: reaction}, nil
}
// RegisteredReactions implements the QueryRegisteredReactions gRPC method
func (k Keeper) RegisteredReactions(ctx context.Context, request *types.QueryRegisteredReactionsRequest) (*types.QueryRegisteredReactionsResponse, error) {
if request.SubspaceId == 0 {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid subspace id")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
store := sdkCtx.KVStore(k.storeKey)
storePrefix := types.SubspaceRegisteredReactionsPrefix(request.SubspaceId)
reactionsStore := prefix.NewStore(store, storePrefix)
var reactions []types.RegisteredReaction
pageRes, err := query.Paginate(reactionsStore, request.Pagination, func(key []byte, value []byte) error {
var reaction types.RegisteredReaction
if err := k.cdc.Unmarshal(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{RegisteredReactions: reactions, Pagination: pageRes}, nil
}
// RegisteredReaction implements the QueryRegisteredReaction gRPC method
func (k Keeper) RegisteredReaction(ctx context.Context, request *types.QueryRegisteredReactionRequest) (*types.QueryRegisteredReactionResponse, error) {
if request.SubspaceId == 0 {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid subspace id")
}
if request.ReactionId == 0 {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid registered reaction id")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
reaction, found := k.GetRegisteredReaction(sdkCtx, request.SubspaceId, request.ReactionId)
if !found {
return nil, sdkerrors.Wrapf(sdkerrors.ErrNotFound, "registered reaction with id %d not found inside subspace %d", request.ReactionId, request.SubspaceId)
}
return &types.QueryRegisteredReactionResponse{RegisteredReaction: reaction}, nil
}
// ReactionsParams implements the QueryReactionsParams gRPC method
func (k Keeper) ReactionsParams(ctx context.Context, request *types.QueryReactionsParamsRequest) (*types.QueryReactionsParamsResponse, error) {
if request.SubspaceId == 0 {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid subspace id")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
params, err := k.GetSubspaceReactionsParams(sdkCtx, request.SubspaceId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryReactionsParamsResponse{Params: params}, nil
}