-
Notifications
You must be signed in to change notification settings - Fork 44
/
msgs_server.go
396 lines (322 loc) · 12.9 KB
/
msgs_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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package keeper
import (
"context"
"crypto/sha256"
"encoding/hex"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/desmos-labs/desmos/v2/x/staging/posts/types"
)
type msgServer struct {
Keeper
}
// NewMsgServerImpl returns an implementation of the posts MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{keeper}
}
func computePostID(ctx sdk.Context, msg *types.MsgCreatePost) string {
post := types.Post{
ParentID: msg.ParentID,
Message: msg.Message,
Created: ctx.BlockTime(),
CommentsState: msg.CommentsState,
Subspace: msg.Subspace,
AdditionalAttributes: msg.AdditionalAttributes,
Creator: msg.Creator,
Attachments: msg.Attachments,
Poll: msg.Poll,
}
bytes, err := post.Marshal()
if err != nil {
panic(err)
}
hash := sha256.Sum256(bytes)
return hex.EncodeToString(hash[:])
}
func (k msgServer) CreatePost(goCtx context.Context, msg *types.MsgCreatePost) (*types.MsgCreatePostResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
post := types.NewPost(
computePostID(ctx, msg),
msg.ParentID,
msg.Message,
msg.CommentsState,
msg.Subspace,
msg.AdditionalAttributes,
msg.Attachments,
msg.Poll,
time.Time{},
ctx.BlockTime(),
msg.Creator,
)
// Check if the subspace exists and if the user is allowed to perform the operation on it
if err := k.CheckUserPermissionOnSubspace(ctx, post.Subspace, post.Creator); err != nil {
return nil, err
}
// Validate the post
if err := k.ValidatePost(ctx, post); err != nil {
return nil, err
}
// Check for double posting
if k.DoesPostExist(ctx, post.PostID) {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest,
"the provided post conflicts with the one having id %s", post.PostID)
}
// Check if any of the tags have blocked the post creator
if err := k.IsCreatorBlockedBySomeTags(ctx, post.Attachments, post.Creator, post.Subspace); err != nil {
return nil, err
}
// If valid, check the parent post
if types.IsValidPostID(post.ParentID) {
parentPost, found := k.GetPost(ctx, post.ParentID)
if !found {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest,
"parent post with id %s not found", post.ParentID)
}
if parentPost.CommentsState == types.CommentsStateBlocked {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest,
"post with id %s does not allow comments", parentPost.PostID)
}
if parentPost.Subspace != post.Subspace {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest,
"the subspace of the comment is not same as the parent post with id %s", parentPost.PostID)
}
}
// Save the post
k.SavePost(ctx, post)
// Emit the event
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypePostCreated,
sdk.NewAttribute(types.AttributeKeyPostID, post.PostID),
sdk.NewAttribute(types.AttributeKeyPostParentID, post.ParentID),
sdk.NewAttribute(types.AttributeKeyPostCreationTime, post.Created.Format(time.RFC3339)),
sdk.NewAttribute(types.AttributeKeyPostOwner, post.Creator),
))
return &types.MsgCreatePostResponse{}, nil
}
func (k msgServer) EditPost(goCtx context.Context, msg *types.MsgEditPost) (*types.MsgEditPostResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Get the existing post
existing, found := k.GetPost(ctx, msg.PostID)
if !found {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "post with id %s not found", msg.PostID)
}
// Checks if the the msg sender is the same as the current owner
if msg.Editor != existing.Creator {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner")
}
// Check the validity of the current block height respect to the creation date of the post
if existing.Created.After(ctx.BlockTime()) {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "edit date cannot be before creation date")
}
// Check if the subspace exists and if the user is allowed to perform the operation on it
if err := k.CheckUserPermissionOnSubspace(ctx, existing.Subspace, existing.Creator); err != nil {
return nil, err
}
// Edit the post
existing.Message = msg.Message
if msg.Attachments != nil {
// Check if any of the tags have blocked the post creator
if err := k.IsCreatorBlockedBySomeTags(ctx, msg.Attachments, existing.Creator, existing.Subspace); err != nil {
return nil, err
}
existing.Attachments = msg.Attachments
}
if msg.Poll != nil {
existing.Poll = msg.Poll
}
if msg.CommentsState != types.CommentsStateUnspecified {
existing.CommentsState = msg.CommentsState
}
existing.LastEdited = ctx.BlockTime()
if err := k.ValidatePost(ctx, existing); err != nil {
return nil, err
}
k.SavePost(ctx, existing)
// Emit the event
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypePostEdited,
sdk.NewAttribute(types.AttributeKeyPostID, existing.PostID),
sdk.NewAttribute(types.AttributeKeyPostEditTime, existing.LastEdited.Format(time.RFC3339)),
))
return &types.MsgEditPostResponse{}, nil
}
func (k msgServer) AddPostReaction(goCtx context.Context, msg *types.MsgAddPostReaction) (*types.MsgAddPostReactionResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Get the post
post, found := k.GetPost(ctx, msg.PostID)
if !found {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "post with id %s not found", msg.PostID)
}
// Check if the subspace exists and if the user is allowed to perform the operation on it
if err := k.CheckUserPermissionOnSubspace(ctx, post.Subspace, msg.User); err != nil {
return nil, err
}
reactionShortcode, reactionValue, err := k.ExtractReactionValueAndShortcode(ctx, msg.Reaction, post.Subspace)
if err != nil {
return nil, err
}
postReaction := types.NewPostReaction(msg.PostID, reactionShortcode, reactionValue, msg.User)
if err := k.SavePostReaction(ctx, postReaction); err != nil {
return nil, err
}
// Emit the event
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypePostReactionAdded,
sdk.NewAttribute(types.AttributeKeyPostID, msg.PostID),
sdk.NewAttribute(types.AttributeKeyPostReactionOwner, msg.User),
sdk.NewAttribute(types.AttributeKeyPostReactionValue, reactionValue),
sdk.NewAttribute(types.AttributeKeyReactionShortCode, reactionShortcode),
))
return &types.MsgAddPostReactionResponse{}, nil
}
func (k msgServer) RemovePostReaction(goCtx context.Context, msg *types.MsgRemovePostReaction) (*types.MsgRemovePostReactionResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Get the post
post, found := k.GetPost(ctx, msg.PostID)
if !found {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "post with id %s not found", msg.PostID)
}
// Check if the subspace exists and if the user is allowed to perform the operation on it
if err := k.CheckUserPermissionOnSubspace(ctx, post.Subspace, msg.User); err != nil {
return nil, err
}
reactionShortcode, reactionValue, err := k.ExtractReactionValueAndShortcode(ctx, msg.Reaction, post.Subspace)
if err != nil {
return nil, err
}
// Remove the registeredReactions
reaction := types.NewPostReaction(msg.PostID, reactionShortcode, reactionValue, msg.User)
if err := k.DeletePostReaction(ctx, reaction); err != nil {
return nil, err
}
// Emit the event
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypePostReactionRemoved,
sdk.NewAttribute(types.AttributeKeyPostID, msg.PostID),
sdk.NewAttribute(types.AttributeKeyPostReactionOwner, msg.User),
sdk.NewAttribute(types.AttributeKeyPostReactionValue, reactionValue),
sdk.NewAttribute(types.AttributeKeyReactionShortCode, reactionShortcode),
))
return &types.MsgRemovePostReactionResponse{}, nil
}
func (k msgServer) RegisterReaction(goCtx context.Context, msg *types.MsgRegisterReaction) (*types.MsgRegisterReactionResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Check if the subspace exists and if the user is allowed to perform the operation on it
if err := k.CheckUserPermissionOnSubspace(ctx, msg.Subspace, msg.Creator); err != nil {
return nil, err
}
// Check if the shortcode is associated with an emoji
if _, found := types.GetEmojiByShortCodeOrValue(msg.ShortCode); found {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest,
"shortcode %s represents an emoji and thus can't be used to register a new registeredReactions", msg.ShortCode)
}
// Make sure the given reaction isn't already registered
if _, isAlreadyRegistered := k.GetRegisteredReaction(ctx, msg.ShortCode, msg.Subspace); isAlreadyRegistered {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest,
"registeredReactions with shortcode %s and subspace %s has already been registered", msg.ShortCode, msg.Subspace)
}
reaction := types.NewRegisteredReaction(msg.Creator, msg.ShortCode, msg.Value, msg.Subspace)
k.SaveRegisteredReaction(ctx, reaction)
// Emit the event
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeRegisterReaction,
sdk.NewAttribute(types.AttributeKeyReactionCreator, reaction.Creator),
sdk.NewAttribute(types.AttributeKeyReactionShortCode, reaction.ShortCode),
sdk.NewAttribute(types.AttributeKeyPostReactionValue, reaction.Value),
sdk.NewAttribute(types.AttributeKeyReactionSubSpace, reaction.Subspace),
))
return &types.MsgRegisterReactionResponse{}, nil
}
func (k msgServer) AnswerPoll(goCtx context.Context, msg *types.MsgAnswerPoll) (*types.MsgAnswerPollResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Checks if the post exists
post, found := k.GetPost(ctx, msg.PostID)
if !found {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest,
"post with id %s doesn't exist", msg.PostID)
}
// Check if the subspace exists and if the user is allowed to perform the operation on it
if err := k.CheckUserPermissionOnSubspace(ctx, post.Subspace, msg.Answerer); err != nil {
return nil, err
}
// Make sure the post has a poll
if post.Poll == nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest,
"no poll associated with ID: %s", msg.PostID)
}
// Make sure the poll is not closed
if post.Poll.EndDate.Before(ctx.BlockTime()) {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest,
"the poll associated with ID %s was closed at %s", post.PostID, post.Poll.EndDate)
}
// Check if the poll allows multiple answers
if len(msg.Answers) > 1 && !post.Poll.AllowsMultipleAnswers {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest,
"the poll associated with ID %s doesn't allow multiple answers", post.PostID)
}
// Check if the user answers are more than the answers provided by the poll
if len(msg.Answers) > len(post.Poll.ProvidedAnswers) {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"user's answers are more than the available ones inside the poll")
}
// Make sure that each answer provided by the user matches with one of the provided ones by the poll creator
for _, answer := range msg.Answers {
var found = false
for _, providedAnswer := range post.Poll.ProvidedAnswers {
if answer == providedAnswer.ID {
found = true
break
}
}
if !found {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest,
"answer with ID %s isn't one of the poll's provided answers", answer)
}
}
_, found = k.GetUserAnswer(ctx, post.PostID, msg.Answerer)
// Check if the poll allows to edit previous answers
if found && !post.Poll.AllowsAnswerEdits {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest,
"post with ID %s doesn't allow answers' edits", post.PostID)
}
userAnswer := types.NewUserAnswer(post.PostID, msg.Answerer, msg.Answers)
k.SaveUserAnswer(ctx, userAnswer)
// Emit the event
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeAnsweredPoll,
sdk.NewAttribute(types.AttributeKeyPostID, msg.PostID),
sdk.NewAttribute(types.AttributeKeyPollAnswerer, msg.Answerer),
))
return &types.MsgAnswerPollResponse{}, nil
}
func (k msgServer) ReportPost(goCtx context.Context, msg *types.MsgReportPost) (*types.MsgReportPostResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Check if the post to report exists
postID := msg.PostID
if !types.IsValidPostID(postID) {
return nil, sdkerrors.Wrap(types.ErrInvalidPostID, postID)
}
var post types.Post
var exist bool
if post, exist = k.GetPost(ctx, postID); !exist {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "post with ID: %s doesn't exist", postID)
}
// Check if the subspace exists and if the user is allowed to perform the operation on it
if err := k.CheckUserPermissionOnSubspace(ctx, post.Subspace, msg.User); err != nil {
return nil, err
}
// Create and store the report
report := types.NewReport(postID, msg.ReportType, msg.Message, msg.User)
err := k.SaveReport(ctx, report)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypePostReported,
sdk.NewAttribute(types.AttributeKeyPostID, msg.PostID),
sdk.NewAttribute(types.AttributeKeyReportOwner, msg.User),
))
return &types.MsgReportPostResponse{}, nil
}