-
Notifications
You must be signed in to change notification settings - Fork 44
/
grpc_query.go
100 lines (79 loc) · 3.3 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
package keeper
import (
"context"
"github.com/cosmos/cosmos-sdk/store/prefix"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/desmos-labs/desmos/v3/x/relationships/types"
)
var _ types.QueryServer = Keeper{}
// Relationships implements the Query/Relationships gRPC method
func (k Keeper) Relationships(ctx context.Context, request *types.QueryRelationshipsRequest) (*types.QueryRelationshipsResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
var relationships []types.Relationship
// Get user relationships prefix store
store := sdkCtx.KVStore(k.storeKey)
// If the user and counterparty are provided, get the specific relationship
if request.User != "" && request.Counterparty != "" {
relationship, found := k.GetRelationship(sdkCtx, request.User, request.Counterparty, request.SubspaceId)
if found {
relationships = append(relationships, relationship)
}
return &types.QueryRelationshipsResponse{Relationships: relationships, Pagination: nil}, nil
}
// Get the correct store prefix to be used
storePrefix := types.SubspaceRelationshipsPrefix(request.SubspaceId)
if request.User != "" {
storePrefix = types.UserRelationshipsSubspacePrefix(request.SubspaceId, request.User)
}
// Get paginated user relationships
relationshipsStore := prefix.NewStore(store, storePrefix)
pageRes, err := query.Paginate(relationshipsStore, request.Pagination, func(key []byte, value []byte) error {
var rel types.Relationship
if err := k.cdc.Unmarshal(value, &rel); err != nil {
return status.Error(codes.Internal, err.Error())
}
relationships = append(relationships, rel)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryRelationshipsResponse{Relationships: relationships, Pagination: pageRes}, nil
}
// Blocks implements the Query/Blocks gRPC method
func (k Keeper) Blocks(ctx context.Context, request *types.QueryBlocksRequest) (*types.QueryBlocksResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
var userBlocks []types.UserBlock
// Get user blocks prefix store
store := sdkCtx.KVStore(k.storeKey)
// If the blocker and blocked are provided, get the specific block
if request.Blocker != "" && request.Blocked != "" {
userBlock, found := k.GetUserBlock(sdkCtx, request.Blocker, request.Blocked, request.SubspaceId)
if found {
userBlocks = append(userBlocks, userBlock)
}
return &types.QueryBlocksResponse{Blocks: userBlocks, Pagination: nil}, nil
}
// Get the correct store prefix to be used
storePrefix := types.SubspaceBlocksPrefix(request.SubspaceId)
if request.Blocker != "" {
storePrefix = types.BlockerSubspacePrefix(request.SubspaceId, request.Blocker)
}
// Get paginated user blocks
userBlocksStore := prefix.NewStore(store, storePrefix)
pageRes, err := query.Paginate(userBlocksStore, request.Pagination, func(key []byte, value []byte) error {
var userBlock types.UserBlock
if err := k.cdc.Unmarshal(value, &userBlock); err != nil {
return status.Error(codes.Internal, err.Error())
}
userBlocks = append(userBlocks, userBlock)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryBlocksResponse{Blocks: userBlocks, Pagination: pageRes}, nil
}