-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathmsg_server.go
721 lines (589 loc) · 25.7 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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
package keeper
import (
"context"
"fmt"
"sort"
"strings"
"time"
"github.com/desmos-labs/desmos/v6/types/utils"
"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/desmos-labs/desmos/v6/x/posts/types"
subspacestypes "github.com/desmos-labs/desmos/v6/x/subspaces/types"
)
type msgServer struct {
*Keeper
}
// NewMsgServerImpl returns an implementation of the stored MsgServer interface
// for the provided k
func NewMsgServerImpl(keeper *Keeper) types.MsgServer {
return &msgServer{Keeper: keeper}
}
var _ types.MsgServer = &msgServer{}
// CreatePost defines the rpc method for Msg/CreatePost
func (k msgServer) CreatePost(goCtx context.Context, msg *types.MsgCreatePost) (*types.MsgCreatePostResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Check if the author has a profile
if !k.HasProfile(ctx, msg.Author) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "you cannot create a post without having 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 section exists
if !k.HasSection(ctx, msg.SubspaceID, msg.SectionID) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "subspace section with id %d not found", msg.SectionID)
}
// Check the permission to create this post
canWrite := k.HasPermission(ctx, msg.SubspaceID, msg.SectionID, msg.Author, types.PermissionWrite)
canComment := msg.ConversationID != 0 && k.HasPermission(ctx, msg.SubspaceID, msg.SectionID, msg.Author, types.PermissionComment)
if !canWrite && !canComment {
return nil, errors.Wrap(subspacestypes.ErrPermissionDenied, "you cannot create posts nor comment inside this section")
}
// Get the next post id
postID, err := k.GetNextPostID(ctx, msg.SubspaceID)
if err != nil {
return nil, err
}
// Create and validate the post
post := types.NewPost(
msg.SubspaceID,
msg.SectionID,
postID,
msg.ExternalID,
msg.Text,
msg.Author,
msg.ConversationID,
msg.Entities,
msg.Tags,
msg.ReferencedPosts,
msg.ReplySettings,
ctx.BlockTime(),
nil,
msg.Author,
)
err = k.ValidatePost(ctx, post)
if err != nil {
return nil, err
}
// Store the post
k.SavePost(ctx, post)
// Update the id for the next post
k.SetNextPostID(ctx, msg.SubspaceID, post.ID+1)
// Unpack the attachments
attachments, err := types.UnpackAttachments(k.cdc, msg.Attachments)
if err != nil {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid attachments: %s", err)
}
// Store the attachments
for _, content := range attachments {
_, err = k.storePostAttachment(ctx, post.SubspaceID, post.ID, content)
if err != nil {
return nil, err
}
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeCreatedPost,
sdk.NewAttribute(subspacestypes.AttributeKeySubspaceID, fmt.Sprintf("%d", msg.SubspaceID)),
sdk.NewAttribute(subspacestypes.AttributeKeySectionID, fmt.Sprintf("%d", msg.SectionID)),
sdk.NewAttribute(types.AttributeKeyPostID, fmt.Sprintf("%d", post.ID)),
sdk.NewAttribute(types.AttributeKeyAuthor, msg.Author),
sdk.NewAttribute(types.AttributeKeyCreationTime, post.CreationDate.Format(time.RFC3339)),
),
})
return &types.MsgCreatePostResponse{
PostID: post.ID,
CreationDate: post.CreationDate,
}, nil
}
// EditPost defines the rpc method for Msg/EditPost
func (k msgServer) EditPost(goCtx context.Context, msg *types.MsgEditPost) (*types.MsgEditPostResponse, 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)
}
// Get the post
post, found := k.GetPost(ctx, msg.SubspaceID, msg.PostID)
if !found {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "post with id %d not found", msg.PostID)
}
// Make sure the editor matches the owner
if post.Owner != msg.Editor {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "you are not the owner of this post")
}
// Check the permission to create content
if !k.HasPermission(ctx, msg.SubspaceID, post.SectionID, msg.Editor, types.PermissionEditOwnContent) {
return nil, errors.Wrap(subspacestypes.ErrPermissionDenied, "you cannot edit content inside this subspace")
}
// Update the post and validate it
updateTime := ctx.BlockTime()
update := types.NewPostUpdate(msg.Text, msg.Entities, msg.Tags, updateTime)
updatedPost := post.Update(update)
err := k.ValidatePost(ctx, updatedPost)
if err != nil {
return nil, err
}
// Store the update
k.SavePost(ctx, updatedPost)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeEditedPost,
sdk.NewAttribute(subspacestypes.AttributeKeySubspaceID, fmt.Sprintf("%d", msg.SubspaceID)),
sdk.NewAttribute(types.AttributeKeyPostID, fmt.Sprintf("%d", msg.PostID)),
sdk.NewAttribute(types.AttributeKeyLastEditTime, updateTime.Format(time.RFC3339)),
),
})
return &types.MsgEditPostResponse{
EditDate: updateTime,
}, nil
}
// DeletePost defines the rpc method for Msg/DeletePost
func (k msgServer) DeletePost(goCtx context.Context, msg *types.MsgDeletePost) (*types.MsgDeletePostResponse, 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)
}
// Get the post
post, found := k.GetPost(ctx, msg.SubspaceID, msg.PostID)
if !found {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "post with id %d does not exist", msg.PostID)
}
// Check the permission to remove the post
isModerator := k.HasPermission(ctx, msg.SubspaceID, post.SectionID, msg.Signer, types.PermissionModerateContent)
canEdit := post.Owner == msg.Signer && k.HasPermission(ctx, msg.SubspaceID, post.SectionID, msg.Signer, types.PermissionEditOwnContent)
if !isModerator && !canEdit {
return nil, errors.Wrap(subspacestypes.ErrPermissionDenied, "you cannot edit content inside this subspace")
}
// Delete the post
k.Keeper.DeletePost(ctx, msg.SubspaceID, msg.PostID)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeDeletedPost,
sdk.NewAttribute(subspacestypes.AttributeKeySubspaceID, fmt.Sprintf("%d", msg.SubspaceID)),
sdk.NewAttribute(types.AttributeKeyPostID, fmt.Sprintf("%d", msg.PostID)),
),
})
return &types.MsgDeletePostResponse{}, nil
}
// storePostAttachment allows to easily store a post attachment, returning the attachment id used and any error
func (k msgServer) storePostAttachment(ctx sdk.Context, subspaceID uint64, postID uint64, content types.AttachmentContent) (attachmentID uint32, err error) {
// Perform poll checks
if poll, ok := content.(*types.Poll); ok {
// Make sure no tally results are provided
if poll.FinalTallyResults != nil {
return 0, errors.Wrapf(sdkerrors.ErrInvalidRequest, "poll tally results must be nil")
}
// Make sure the end date is in the future
if poll.EndDate.Before(ctx.BlockTime()) {
return 0, errors.Wrapf(sdkerrors.ErrInvalidRequest, "poll end date must be in the future")
}
}
// Get the next attachment id
attachmentID, err = k.GetNextAttachmentID(ctx, subspaceID, postID)
if err != nil {
return
}
// Create the attachment and validate it
attachment := types.NewAttachment(subspaceID, postID, attachmentID, content)
err = attachment.Validate()
if err != nil {
return 0, errors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid attachment content: %s", err)
}
// Save the attachment
k.SaveAttachment(ctx, attachment)
// Store the poll inside the active queue
if types.IsPoll(attachment) {
k.InsertActivePollQueue(ctx, attachment)
}
// Update the id for the next attachment
k.SetNextAttachmentID(ctx, subspaceID, postID, attachment.ID+1)
return attachmentID, nil
}
// AddPostAttachment defines the rpc method for Msg/AddPostAttachment
func (k msgServer) AddPostAttachment(goCtx context.Context, msg *types.MsgAddPostAttachment) (*types.MsgAddPostAttachmentResponse, 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)
}
// Get the post
post, found := k.GetPost(ctx, msg.SubspaceID, msg.PostID)
if !found {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "post with id %d does not exist", msg.PostID)
}
// Make sure the editor matches the owner
if post.Owner != msg.Editor {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "you are not the author of this post")
}
// Check the permission to edit content
if !k.HasPermission(ctx, msg.SubspaceID, post.SectionID, msg.Editor, types.PermissionEditOwnContent) {
return nil, errors.Wrap(subspacestypes.ErrPermissionDenied, "you cannot edit content inside this subspace")
}
// Unpack the content
var content types.AttachmentContent
err := k.cdc.UnpackAny(msg.Content, &content)
if err != nil {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid attachment content: %s", err)
}
// Save the attachment
attachmentID, err := k.storePostAttachment(ctx, msg.SubspaceID, msg.PostID, msg.Content.GetCachedValue().(types.AttachmentContent))
if err != nil {
return nil, err
}
// Update the post edit time
updateTime := ctx.BlockTime()
post.LastEditedDate = &updateTime
err = k.ValidatePost(ctx, post)
if err != nil {
return nil, err
}
k.SavePost(ctx, post)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeAddedPostAttachment,
sdk.NewAttribute(subspacestypes.AttributeKeySubspaceID, fmt.Sprintf("%d", msg.SubspaceID)),
sdk.NewAttribute(types.AttributeKeyPostID, fmt.Sprintf("%d", msg.PostID)),
sdk.NewAttribute(types.AttributeKeyAttachmentID, fmt.Sprintf("%d", attachmentID)),
sdk.NewAttribute(types.AttributeKeyLastEditTime, post.LastEditedDate.Format(time.RFC3339)),
),
})
return &types.MsgAddPostAttachmentResponse{
AttachmentID: attachmentID,
EditDate: updateTime,
}, nil
}
// RemovePostAttachment defines the rpc method for Msg/RemovePostAttachment
func (k msgServer) RemovePostAttachment(goCtx context.Context, msg *types.MsgRemovePostAttachment) (*types.MsgRemovePostAttachmentResponse, 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)
}
// Get the post
post, found := k.GetPost(ctx, msg.SubspaceID, msg.PostID)
if !found {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "post with id %d does not exist", msg.PostID)
}
// Check the permission to remove the attachment
isModerator := k.HasPermission(ctx, msg.SubspaceID, post.SectionID, msg.Editor, types.PermissionModerateContent)
canEdit := post.Owner == msg.Editor && k.HasPermission(ctx, msg.SubspaceID, post.SectionID, msg.Editor, types.PermissionEditOwnContent)
if !isModerator && !canEdit {
return nil, errors.Wrap(subspacestypes.ErrPermissionDenied, "you cannot edit content inside this subspace")
}
// Check if the attachment exists
if !k.HasAttachment(ctx, msg.SubspaceID, msg.PostID, msg.AttachmentID) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "attachment with id %d not found", msg.AttachmentID)
}
// Remove the post attachment
k.DeleteAttachment(ctx, msg.SubspaceID, msg.PostID, msg.AttachmentID)
// Update the post edit time and validate it
updateTime := ctx.BlockTime()
post.LastEditedDate = &updateTime
err := k.ValidatePost(ctx, post)
if err != nil {
return nil, err
}
// Save the post
k.SavePost(ctx, post)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeRemovedPostAttachment,
sdk.NewAttribute(subspacestypes.AttributeKeySubspaceID, fmt.Sprintf("%d", msg.SubspaceID)),
sdk.NewAttribute(types.AttributeKeyPostID, fmt.Sprintf("%d", msg.PostID)),
sdk.NewAttribute(types.AttributeKeyAttachmentID, fmt.Sprintf("%d", msg.AttachmentID)),
sdk.NewAttribute(types.AttributeKeyLastEditTime, post.LastEditedDate.Format(time.RFC3339)),
),
})
return &types.MsgRemovePostAttachmentResponse{
EditDate: updateTime,
}, nil
}
// AnswerPoll defines the rpc method for Msg/AnswerPoll
func (k msgServer) AnswerPoll(goCtx context.Context, msg *types.MsgAnswerPoll) (*types.MsgAnswerPollResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Check if the author has a profile
if !k.HasProfile(ctx, msg.Signer) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "you cannot answer a poll without having 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)
}
// Make sure the post exists
post, found := k.GetPost(ctx, msg.SubspaceID, msg.PostID)
if !found {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "post with id %d does not exist", msg.PostID)
}
// Check the permission to interact with content
if !k.HasPermission(ctx, msg.SubspaceID, post.SectionID, msg.Signer, types.PermissionInteractWithContent) {
return nil, errors.Wrap(subspacestypes.ErrPermissionDenied, "you cannot interact with content inside this subspace")
}
// Get the poll
poll, found := k.GetPoll(ctx, msg.SubspaceID, msg.PostID, msg.PollID)
if !found {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "poll with id %d does not exist", msg.PollID)
}
// Make sure the poll is still active
if ctx.BlockTime().After(poll.EndDate) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "the poll voting period has already ended")
}
alreadyAnswered := k.HasUserAnswer(ctx, msg.SubspaceID, msg.PostID, msg.PollID, msg.Signer)
// Make sure the user is not trying to edit the answer when the poll does not allow it
if alreadyAnswered && !poll.AllowsAnswerEdits {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "you cannot edit this poll's answer")
}
// Make sure the user not answering with multiple options when the poll does not allow it
if len(msg.AnswersIndexes) > 1 && !poll.AllowsMultipleAnswers {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "only one answer is allowed on this post")
}
// Sort the answers indexes
sort.Slice(msg.AnswersIndexes, func(i, j int) bool {
return msg.AnswersIndexes[i] < msg.AnswersIndexes[j]
})
// Make sure the answer indexes exist
maxProvidedIndex := uint32(len(poll.ProvidedAnswers) - 1)
maxAnswerIndex := msg.AnswersIndexes[len(msg.AnswersIndexes)-1]
if maxAnswerIndex > maxProvidedIndex {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid answer index: %d", maxAnswerIndex)
}
// Store the user answer
answer := types.NewUserAnswer(msg.SubspaceID, msg.PostID, msg.PollID, msg.AnswersIndexes, msg.Signer)
k.SaveUserAnswer(ctx, answer)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeAnsweredPoll,
sdk.NewAttribute(subspacestypes.AttributeKeySubspaceID, fmt.Sprintf("%d", msg.SubspaceID)),
sdk.NewAttribute(types.AttributeKeyPostID, fmt.Sprintf("%d", msg.PostID)),
sdk.NewAttribute(types.AttributeKeyPollID, fmt.Sprintf("%d", msg.PollID)),
sdk.NewAttribute(types.AttributeKeyAnswersIndexes, strings.Join(utils.Map(msg.AnswersIndexes, func(index uint32) string {
return fmt.Sprintf("%d", index)
}), ",")),
sdk.NewAttribute(types.AttributeKeyAnswerer, msg.Signer),
),
})
return &types.MsgAnswerPollResponse{}, nil
}
// UpdateParams updates the module parameters
func (m msgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
authority := m.authority
if authority != msg.Authority {
return nil, errors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", authority, msg.Authority)
}
ctx := sdk.UnwrapSDKContext(goCtx)
m.SetParams(ctx, msg.Params)
return &types.MsgUpdateParamsResponse{}, nil
}
// MovePost defines the rpc method for Msg/MovePost
func (k msgServer) MovePost(goCtx context.Context, msg *types.MsgMovePost) (*types.MsgMovePostResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Make sure the post exists
post, found := k.GetPost(ctx, msg.SubspaceID, msg.PostID)
if !found {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "post with id %d does not exist", msg.PostID)
}
// Check if the target subspace exists
if !k.HasSubspace(ctx, msg.TargetSubspaceID) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "subspace with id %d not found", msg.TargetSubspaceID)
}
// Check if the target section exists
if !k.HasSection(ctx, msg.TargetSubspaceID, msg.TargetSectionID) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "subspace section with id %d not found", msg.TargetSectionID)
}
// Check the sender is the owner of the post
if post.Owner != msg.Owner {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "you are not the owner of the post")
}
// Check the permission to move the post
if !k.HasPermission(ctx, msg.TargetSubspaceID, msg.TargetSectionID, msg.Owner, types.PermissionWrite) {
return nil, errors.Wrap(subspacestypes.ErrPermissionDenied, "you don't have write permission on the target section")
}
// Get the post id within the new subspace
newPostID, err := k.GetNextPostID(ctx, msg.TargetSubspaceID)
if err != nil {
return nil, err
}
// Update the post and validate it
updateTime := ctx.BlockTime()
postMove := types.NewPostMove(msg.TargetSubspaceID, msg.TargetSectionID, newPostID, updateTime)
updatedPost := postMove.Update(post)
err = k.ValidatePost(ctx, updatedPost)
if err != nil {
return nil, err
}
k.Keeper.SavePost(ctx, updatedPost)
// Update the id for the next post
k.SetNextPostID(ctx, msg.TargetSubspaceID, newPostID+1)
// Move all post attachments
maxAttachmentID := uint32(0)
k.IteratePostAttachments(ctx, msg.SubspaceID, msg.PostID, func(attachment types.Attachment) (stop bool) {
attachmentMove := types.NewAttachmentMove(updatedPost.SubspaceID, updatedPost.ID)
updatedAttachment := attachmentMove.Update(attachment)
k.SaveAttachment(ctx, updatedAttachment)
if updatedAttachment.ID > maxAttachmentID {
maxAttachmentID = updatedAttachment.ID
}
if k.isActivePoll(ctx, attachment) {
k.InsertActivePollQueue(ctx, updatedAttachment)
}
return false
})
// Update the id for the next attachment
k.SetNextAttachmentID(ctx, msg.TargetSubspaceID, newPostID, maxAttachmentID+1)
// Delete the post
k.Keeper.DeletePost(ctx, msg.SubspaceID, msg.PostID)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeMovedPost,
sdk.NewAttribute(subspacestypes.AttributeKeySubspaceID, fmt.Sprintf("%d", msg.SubspaceID)),
sdk.NewAttribute(types.AttributeKeyPostID, fmt.Sprintf("%d", msg.PostID)),
sdk.NewAttribute(types.AttributeKeyNewSubspaceID, fmt.Sprintf("%d", msg.TargetSubspaceID)),
sdk.NewAttribute(types.AttributeKeyNewPostID, fmt.Sprintf("%d", updatedPost.ID)),
),
})
return &types.MsgMovePostResponse{}, nil
}
// RequestPostOwnerTransfer defines the rpc method for Msg/RequestPostOwnerTransfer
func (k msgServer) RequestPostOwnerTransfer(goCtx context.Context, msg *types.MsgRequestPostOwnerTransfer) (*types.MsgRequestPostOwnerTransferResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Check if the sender has profile
if !k.HasProfile(ctx, msg.Sender) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "you cannot transfer a post without having a profile")
}
// Check if the receiver has profile
if !k.HasProfile(ctx, msg.Receiver) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "you cannot transfer a post to the user having no profile")
}
// Make sure the receiver has not blocked the sender
if k.HasUserBlocked(ctx, msg.Receiver, msg.Sender, msg.SubspaceID) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "receiver has blocked you")
}
// Get the post
post, found := k.GetPost(ctx, msg.SubspaceID, msg.PostID)
if !found {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "post with id %d does not exist", msg.PostID)
}
// Make sure the sender matches the owner
if post.Owner != msg.Sender {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "you cannot transfer a post that you do not own")
}
// Check if the post owner transfer request exists
if k.HasPostOwnerTransferRequest(ctx, msg.SubspaceID, msg.PostID) {
return nil, errors.Wrap(sdkerrors.ErrInvalidRequest, "post owner transfer request already exists")
}
// Save the post owner transfer request
k.SavePostOwnerTransferRequest(ctx, types.NewPostOwnerTransferRequest(
msg.SubspaceID,
msg.PostID,
msg.Receiver,
msg.Sender,
))
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeRequestedPostOwnerTransfer,
sdk.NewAttribute(subspacestypes.AttributeKeySubspaceID, fmt.Sprintf("%d", msg.SubspaceID)),
sdk.NewAttribute(types.AttributeKeyPostID, fmt.Sprintf("%d", msg.PostID)),
sdk.NewAttribute(types.AttributeKeyReceiver, msg.Receiver),
sdk.NewAttribute(types.AttributeKeySender, msg.Sender),
),
})
return &types.MsgRequestPostOwnerTransferResponse{}, nil
}
// CancelPostOwnerTransferRequest defines the rpc method for Msg/CancelPostOwnerTransfer
func (k msgServer) CancelPostOwnerTransferRequest(goCtx context.Context, msg *types.MsgCancelPostOwnerTransferRequest) (*types.MsgCancelPostOwnerTransferRequestResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Get the post owner transfer request
request, found := k.GetPostOwnerTransferRequest(ctx, msg.SubspaceID, msg.PostID)
if !found {
return nil, errors.Wrap(sdkerrors.ErrInvalidRequest, "post owner transfer request does not exists")
}
// Make sure the request sender matches the sender
if request.Sender != msg.Sender {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "you cannot cancel a post owner transfer request that you are not the sender")
}
// Delete the post owner transfer request
k.DeletePostOwnerTransferRequest(ctx, msg.SubspaceID, msg.PostID)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeCanceledPostOwnerTransfer,
sdk.NewAttribute(subspacestypes.AttributeKeySubspaceID, fmt.Sprintf("%d", msg.SubspaceID)),
sdk.NewAttribute(types.AttributeKeyPostID, fmt.Sprintf("%d", msg.PostID)),
sdk.NewAttribute(types.AttributeKeySender, msg.Sender),
),
})
return &types.MsgCancelPostOwnerTransferRequestResponse{}, nil
}
// AcceptPostOwnerTransferRequest defines the rpc method for Msg/AcceptPostOwnerTransfer
func (k msgServer) AcceptPostOwnerTransferRequest(goCtx context.Context, msg *types.MsgAcceptPostOwnerTransferRequest) (*types.MsgAcceptPostOwnerTransferRequestResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Check if the receiver has profile
if !k.HasProfile(ctx, msg.Receiver) {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "you cannot accept a post owner transfer request without having a profile")
}
// Get the post owner transfer request
request, found := k.GetPostOwnerTransferRequest(ctx, msg.SubspaceID, msg.PostID)
if !found {
return nil, errors.Wrap(sdkerrors.ErrInvalidRequest, "post owner transfer request does not exists")
}
// Make sure the request receiver matches the receiver
if request.Receiver != msg.Receiver {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "you cannot accept a post owner transfer request that you are not the receiver")
}
// Get the post
post, found := k.GetPost(ctx, msg.SubspaceID, msg.PostID)
if !found {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "post with id %d does not exist", msg.PostID)
}
// Make sure the post owner matches the sender
if post.Owner != request.Sender {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "the sender of the post owner transfer request does not own the post")
}
// Update the post and validate it
newPost := types.NewOwnerTransfer(msg.Receiver, ctx.BlockTime()).Update(post)
err := newPost.Validate()
if err != nil {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, err.Error())
}
// Store the update
k.SavePost(ctx, newPost)
// Delete the post owner transfer request
k.DeletePostOwnerTransferRequest(ctx, msg.SubspaceID, msg.PostID)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeAcceptedPostOwnerTransfer,
sdk.NewAttribute(subspacestypes.AttributeKeySubspaceID, fmt.Sprintf("%d", msg.SubspaceID)),
sdk.NewAttribute(types.AttributeKeyPostID, fmt.Sprintf("%d", msg.PostID)),
sdk.NewAttribute(types.AttributeKeyReceiver, msg.Receiver),
),
})
return &types.MsgAcceptPostOwnerTransferRequestResponse{}, nil
}
// RefusePostOwnerTransferRequest defines the rpc method for Msg/RefusePostOwnerTransfer
func (k msgServer) RefusePostOwnerTransferRequest(goCtx context.Context, msg *types.MsgRefusePostOwnerTransferRequest) (*types.MsgRefusePostOwnerTransferRequestResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Get the post owner transfer request
request, found := k.GetPostOwnerTransferRequest(ctx, msg.SubspaceID, msg.PostID)
if !found {
return nil, errors.Wrap(sdkerrors.ErrInvalidRequest, "post owner transfer request does not exists")
}
// Make sure the request receiver matches the receiver
if request.Receiver != msg.Receiver {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest, "you cannot refuse a post owner transfer request that you are not the receiver")
}
// Delete the post owner transfer request
k.DeletePostOwnerTransferRequest(ctx, msg.SubspaceID, msg.PostID)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeRefusedPostOwnerTransfer,
sdk.NewAttribute(subspacestypes.AttributeKeySubspaceID, fmt.Sprintf("%d", msg.SubspaceID)),
sdk.NewAttribute(types.AttributeKeyPostID, fmt.Sprintf("%d", msg.PostID)),
sdk.NewAttribute(types.AttributeKeyReceiver, msg.Receiver),
),
})
return &types.MsgRefusePostOwnerTransferRequestResponse{}, nil
}