/
msg_server.go
311 lines (250 loc) · 11.3 KB
/
msg_server.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package keeper
import (
"context"
"fmt"
poststypes "github.com/desmos-labs/desmos/v7/x/posts/types"
"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/desmos-labs/desmos/v7/x/reactions/types"
subspacestypes "github.com/desmos-labs/desmos/v7/x/subspaces/types"
)
type msgServer struct {
Keeper
}
// NewMsgServerImpl returns an implementation of the stored MsgServer interface for the provided keeper
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{Keeper: keeper}
}
var _ types.MsgServer = &msgServer{}
// AddReaction defines the rpc method for Msg/AddReaction
func (k msgServer) AddReaction(goCtx context.Context, msg *types.MsgAddReaction) (*types.MsgAddReactionResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Check if the user has a profile
if !k.HasProfile(ctx, msg.User) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "you cannot add a reaction without a profile")
}
// Check if the subspace exists
if !k.HasSubspace(ctx, msg.SubspaceID) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "subspace with id %d not found", msg.SubspaceID)
}
// Check if the post exists
post, found := k.GetPost(ctx, msg.SubspaceID, msg.PostID)
if !found {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "post with id %d not found inside subspace %d", msg.PostID, msg.SubspaceID)
}
// Make sure the post author has not blocked the user
if k.HasUserBlocked(ctx, post.Owner, msg.User, msg.SubspaceID) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "the post author has blocked you on this subspace")
}
// Check the permission to react
if !k.HasPermission(ctx, post.SubspaceID, post.SectionID, msg.User, types.PermissionsReact) {
return nil, errors.Wrap(subspacestypes.ErrPermissionDenied, "you cannot add reactions inside this subspace")
}
value, ok := msg.Value.GetCachedValue().(types.ReactionValue)
if !ok {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid reaction value: %s", msg.Value)
}
// Make sure the reaction does not exist already
if k.HasReacted(ctx, msg.SubspaceID, msg.PostID, msg.User, value) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "you have already reacted with the same value to this post")
}
// Get the next reaction id
reactionID, err := k.GetNextReactionID(ctx, msg.SubspaceID, msg.PostID)
if err != nil {
return nil, err
}
// Create and validate the reaction
reaction := types.NewReaction(
msg.SubspaceID,
msg.PostID,
reactionID,
value,
msg.User,
)
err = k.ValidateReaction(ctx, reaction)
if err != nil {
return nil, err
}
// Store the reaction
k.SaveReaction(ctx, reaction)
// Update the id for the next reaction
k.SetNextReactionID(ctx, msg.SubspaceID, msg.PostID, reaction.ID+1)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeAddedReaction,
sdk.NewAttribute(subspacestypes.AttributeKeySubspaceID, fmt.Sprintf("%d", msg.SubspaceID)),
sdk.NewAttribute(poststypes.AttributeKeyPostID, fmt.Sprintf("%d", msg.PostID)),
sdk.NewAttribute(types.AttributeKeyReactionID, fmt.Sprintf("%d", reaction.ID)),
sdk.NewAttribute(types.AttributeKeyUser, msg.User),
),
})
return &types.MsgAddReactionResponse{
ReactionID: reaction.ID,
}, nil
}
// RemoveReaction defines the rpc method for Msg/RemoveReaction
func (k msgServer) RemoveReaction(goCtx context.Context, msg *types.MsgRemoveReaction) (*types.MsgRemoveReactionResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Check if the subspace exists
if !k.HasSubspace(ctx, msg.SubspaceID) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "subspace with id %d not found", msg.SubspaceID)
}
// Check if the post exists
post, found := k.GetPost(ctx, msg.SubspaceID, msg.PostID)
if !found {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "post with id %d not found inside subspace %d", msg.PostID, msg.SubspaceID)
}
// Check if the reaction exists
reaction, found := k.GetReaction(ctx, msg.SubspaceID, msg.PostID, msg.ReactionID)
if !found {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "reaction does not exist")
}
// Make sure the user matches the author
if reaction.Author != msg.User {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "you are not the author of this reaction")
}
// Check the permission to remove reaction
if !k.HasPermission(ctx, post.SubspaceID, post.SectionID, msg.User, types.PermissionsReact) {
return nil, errors.Wrap(subspacestypes.ErrPermissionDenied, "you cannot remove reactions inside this subspace")
}
// Remove the reaction
k.DeleteReaction(ctx, msg.SubspaceID, msg.PostID, msg.ReactionID)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeRemovedReaction,
sdk.NewAttribute(subspacestypes.AttributeKeySubspaceID, fmt.Sprintf("%d", msg.SubspaceID)),
sdk.NewAttribute(poststypes.AttributeKeyPostID, fmt.Sprintf("%d", msg.PostID)),
sdk.NewAttribute(types.AttributeKeyReactionID, fmt.Sprintf("%d", msg.ReactionID)),
),
})
return &types.MsgRemoveReactionResponse{}, nil
}
// AddRegisteredReaction defines the rpc method for Msg/AddRegisteredReaction
func (k msgServer) AddRegisteredReaction(goCtx context.Context, msg *types.MsgAddRegisteredReaction) (*types.MsgAddRegisteredReactionResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Check if the subspace exists
if !k.HasSubspace(ctx, msg.SubspaceID) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "subspace with id %d not found", msg.SubspaceID)
}
// Check the permission to manage the registered reactions
if !k.HasPermission(ctx, msg.SubspaceID, subspacestypes.RootSectionID, msg.User, types.PermissionManageRegisteredReactions) {
return nil, errors.Wrap(subspacestypes.ErrPermissionDenied, "you cannot manage the registered reactions inside this subspace")
}
// Get the next reaction id
registeredReactionID, err := k.GetNextRegisteredReactionID(ctx, msg.SubspaceID)
if err != nil {
return nil, err
}
// Create the registered reaction and validate it
reaction := types.NewRegisteredReaction(
msg.SubspaceID,
registeredReactionID,
msg.ShorthandCode,
msg.DisplayValue,
)
err = reaction.Validate()
if err != nil {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, err.Error())
}
// Store the registered reaction
k.SaveRegisteredReaction(ctx, reaction)
// Update the id for the next reaction
k.SetNextRegisteredReactionID(ctx, msg.SubspaceID, reaction.ID+1)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeAddedRegisteredReaction,
sdk.NewAttribute(subspacestypes.AttributeKeySubspaceID, fmt.Sprintf("%d", msg.SubspaceID)),
sdk.NewAttribute(types.AttributeKeyRegisteredReactionID, fmt.Sprintf("%d", reaction.ID)),
),
})
return &types.MsgAddRegisteredReactionResponse{
RegisteredReactionID: reaction.ID,
}, nil
}
// EditRegisteredReaction defines the rpc method for Msg/EditRegisteredReaction
func (k msgServer) EditRegisteredReaction(goCtx context.Context, msg *types.MsgEditRegisteredReaction) (*types.MsgEditRegisteredReactionResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Check if the subspace exists
if !k.HasSubspace(ctx, msg.SubspaceID) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "subspace with id %d not found", msg.SubspaceID)
}
// Check if the registered reaction exists
reaction, found := k.GetRegisteredReaction(ctx, msg.SubspaceID, msg.RegisteredReactionID)
if !found {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "registered reaction with id %d not found", msg.RegisteredReactionID)
}
// Check the permission to manage the registered reactions
if !k.HasPermission(ctx, msg.SubspaceID, subspacestypes.RootSectionID, msg.User, types.PermissionManageRegisteredReactions) {
return nil, errors.Wrap(subspacestypes.ErrPermissionDenied, "you cannot manage the registered reactions inside this subspace")
}
// Update the reaction and validate it
updated := reaction.Update(types.NewRegisteredReactionUpdate(msg.ShorthandCode, msg.DisplayValue))
err := updated.Validate()
if err != nil {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, err.Error())
}
// Save the reaction
k.SaveRegisteredReaction(ctx, updated)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.ActionEditRegisteredReaction,
sdk.NewAttribute(subspacestypes.AttributeKeySubspaceID, fmt.Sprintf("%d", msg.SubspaceID)),
sdk.NewAttribute(types.AttributeKeyRegisteredReactionID, fmt.Sprintf("%d", msg.RegisteredReactionID)),
),
})
return &types.MsgEditRegisteredReactionResponse{}, nil
}
// RemoveRegisteredReaction defines the rpc method for Msg/RemoveRegisteredReaction
func (k msgServer) RemoveRegisteredReaction(goCtx context.Context, msg *types.MsgRemoveRegisteredReaction) (*types.MsgRemoveRegisteredReactionResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Check if the subspace exists
if !k.HasSubspace(ctx, msg.SubspaceID) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "subspace with id %d not found", msg.SubspaceID)
}
// Check if the registered reaction exists
if !k.HasRegisteredReaction(ctx, msg.SubspaceID, msg.RegisteredReactionID) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "registered reaction with id %d not found", msg.RegisteredReactionID)
}
// Check the permission to manage the registered reactions
if !k.HasPermission(ctx, msg.SubspaceID, subspacestypes.RootSectionID, msg.User, types.PermissionManageRegisteredReactions) {
return nil, errors.Wrap(subspacestypes.ErrPermissionDenied, "you cannot manage the registered reactions inside this subspace")
}
// Delete the registered reaction
k.DeleteRegisteredReaction(ctx, msg.SubspaceID, msg.RegisteredReactionID)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeRemovedRegisteredReaction,
sdk.NewAttribute(subspacestypes.AttributeKeySubspaceID, fmt.Sprintf("%d", msg.SubspaceID)),
sdk.NewAttribute(types.AttributeKeyRegisteredReactionID, fmt.Sprintf("%d", msg.RegisteredReactionID)),
),
})
return &types.MsgRemoveRegisteredReactionResponse{}, nil
}
// SetReactionsParams defines the rpc method for Msg/SetReactionsParams
func (k msgServer) SetReactionsParams(goCtx context.Context, msg *types.MsgSetReactionsParams) (*types.MsgSetReactionsParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Check if the subspace exists
if !k.HasSubspace(ctx, msg.SubspaceID) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "subspace with id %d not found", msg.SubspaceID)
}
// Check the permission to manage the reaction params
if !k.HasPermission(ctx, msg.SubspaceID, subspacestypes.RootSectionID, msg.User, types.PermissionManageReactionParams) {
return nil, errors.Wrap(subspacestypes.ErrPermissionDenied, "you cannot manage the reactions params inside this subspace")
}
// Create and validate the params
params := types.NewSubspaceReactionsParams(msg.SubspaceID, msg.RegisteredReaction, msg.FreeText)
err := params.Validate()
if err != nil {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, err.Error())
}
// Store the params
k.SaveSubspaceReactionsParams(ctx, params)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeSetReactionsParams,
sdk.NewAttribute(subspacestypes.AttributeKeySubspaceID, fmt.Sprintf("%d", msg.SubspaceID)),
),
})
return &types.MsgSetReactionsParamsResponse{}, nil
}