-
Notifications
You must be signed in to change notification settings - Fork 5
/
handle_msg.go
131 lines (103 loc) · 3.92 KB
/
handle_msg.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
package posts
import (
"github.com/cosmos/cosmos-sdk/x/authz"
"github.com/desmos-labs/athena/x/filters"
poststypes "github.com/desmos-labs/desmos/v6/x/posts/types"
"github.com/desmos-labs/athena/types"
"github.com/rs/zerolog/log"
sdk "github.com/cosmos/cosmos-sdk/types"
juno "github.com/forbole/juno/v5/types"
)
// HandleMsgExec implements modules.AuthzMessageModule
func (m *Module) HandleMsgExec(index int, _ *authz.MsgExec, _ int, executedMsg sdk.Msg, tx *juno.Tx) error {
return m.HandleMsg(index, executedMsg, tx)
}
// HandleMsg implements modules.MessageModule
func (m *Module) HandleMsg(index int, msg sdk.Msg, tx *juno.Tx) error {
if len(tx.Logs) == 0 || !filters.ShouldMsgBeParsed(msg) {
return nil
}
switch desmosMsg := msg.(type) {
case *poststypes.MsgCreatePost:
return m.handleMsgCreatePost(tx, index, desmosMsg)
case *poststypes.MsgEditPost:
return m.handleMsgEditPost(tx, desmosMsg)
case *poststypes.MsgDeletePost:
return m.handleMsgDeletePost(tx, desmosMsg)
case *poststypes.MsgAddPostAttachment:
return m.handleMsgAddPostAttachment(tx, desmosMsg)
case *poststypes.MsgRemovePostAttachment:
return m.handleMsgRemovePostAttachment(tx, desmosMsg)
case *poststypes.MsgAnswerPoll:
return m.handleMsgAnswerPoll(tx, desmosMsg)
}
log.Debug().Str("module", "reactions").Str("message", sdk.MsgTypeURL(msg)).
Int64("height", tx.Height).Msg("handled message")
return nil
}
// handleMsgCreatePost handles a MsgCreatePost
func (m *Module) handleMsgCreatePost(tx *juno.Tx, index int, msg *poststypes.MsgCreatePost) error {
event, err := tx.FindEventByType(index, poststypes.EventTypeCreatePost)
if err != nil {
return err
}
postIDStr, err := tx.FindAttributeByKey(event, poststypes.AttributeKeyPostID)
if err != nil {
return err
}
postID, err := poststypes.ParsePostID(postIDStr)
if err != nil {
return err
}
// Update the post
err = m.updatePost(tx.Height, msg.SubspaceID, postID)
if err != nil {
return err
}
// Update the post attachments
err = m.updatePostAttachments(tx.Height, msg.SubspaceID, postID)
if err != nil {
return err
}
// Save the related transaction
return m.db.SavePostTx(types.NewPostTransaction(msg.SubspaceID, postID, tx.TxHash))
}
// handleMsgEditPost handles a MsgEditPost
func (m *Module) handleMsgEditPost(tx *juno.Tx, msg *poststypes.MsgEditPost) error {
// Update the post
err := m.updatePost(tx.Height, msg.SubspaceID, msg.PostID)
if err != nil {
return err
}
// Save the related transaction
return m.db.SavePostTx(types.NewPostTransaction(msg.SubspaceID, msg.PostID, tx.TxHash))
}
// handleMsgDeletePost handles a MsgDeletePost
func (m *Module) handleMsgDeletePost(tx *juno.Tx, msg *poststypes.MsgDeletePost) error {
return m.db.DeletePost(tx.Height, msg.SubspaceID, msg.PostID)
}
// handleMsgAddPostAttachment handles a MsgAddPostAttachment
func (m *Module) handleMsgAddPostAttachment(tx *juno.Tx, msg *poststypes.MsgAddPostAttachment) error {
// Update the attachments
err := m.updatePostAttachments(tx.Height, msg.SubspaceID, msg.PostID)
if err != nil {
return err
}
// Store the related post transaction
return m.db.SavePostTx(types.NewPostTransaction(msg.SubspaceID, msg.PostID, tx.TxHash))
}
// handleMsgRemovePostAttachment handles a MsgRemovePostAttachment
func (m *Module) handleMsgRemovePostAttachment(tx *juno.Tx, msg *poststypes.MsgRemovePostAttachment) error {
// Delete the attachment
err := m.db.DeletePostAttachment(tx.Height, msg.SubspaceID, msg.PostID, msg.AttachmentID)
if err != nil {
return err
}
// Store the related post transaction
return m.db.SavePostTx(types.NewPostTransaction(msg.SubspaceID, msg.PostID, tx.TxHash))
}
// handleMsgAnswerPoll handles a MsgAnswerPoll
func (m *Module) handleMsgAnswerPoll(tx *juno.Tx, msg *poststypes.MsgAnswerPoll) error {
answer := poststypes.NewUserAnswer(msg.SubspaceID, msg.PostID, msg.PollID, msg.AnswersIndexes, msg.Signer)
return m.db.SavePollAnswer(types.NewPollAnswer(answer, tx.Height))
}