-
Notifications
You must be signed in to change notification settings - Fork 44
/
tx.go
294 lines (236 loc) · 7.99 KB
/
tx.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
package cli
// DONTCOVER
import (
"fmt"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/version"
"github.com/spf13/cobra"
"github.com/desmos-labs/desmos/v7/x/reports/types"
subspacestypes "github.com/desmos-labs/desmos/v7/x/subspaces/types"
)
// NewTxCmd returns a new command to perform reports transactions
func NewTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: "Reports transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
cmd.AddCommand(
GetCmdCreateReport(),
GetCmdDeleteReport(),
NewReasonsTxCmd(),
)
return cmd
}
// GetCmdCreateReport returns the command allowing to create a report
func GetCmdCreateReport() *cobra.Command {
cmd := &cobra.Command{
Use: "create [subspace-id] [reasons-ids]",
Args: cobra.ExactArgs(2),
Short: "Create a report for user or a post, optionally specifying a message",
Long: fmt.Sprintf(`
Report the specified user or post inside the specific subspace for the reasons having the given ids.
Multiple reasons can be specified. If so, each reason id must be separated using a comma.
To report a user, --%s must be used.
To report a post, --%s must be used instead.`, FlagUser, FlagPostID),
Example: fmt.Sprintf(`
%[1]s tx reports report 1 1,2,3 \
--%s desmos1cs0gu6006rz9wnmltjuhnuz8k3a2wg6jzmmgyu \
--message "This user is spammer" \
--from alice
%[1]s tx reports report 1 1,2,3 \
--%[3]s 1 \
--message "This post is spam" \
--from alice
`, version.AppName, FlagUser, FlagPostID),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := subspacestypes.ParseSubspaceID(args[0])
if err != nil {
return err
}
reasons, err := types.ParseReasonsIDs(args[1])
if err != nil {
return err
}
target, err := ReadReportTarget(cmd.Flags())
if err != nil {
return err
}
if target == nil {
return fmt.Errorf("at least one of --%s or --%s must be specfieid", FlagUser, FlagPostID)
}
message, err := cmd.Flags().GetString(FlagMessage)
if err != nil {
return err
}
reporter := clientCtx.FromAddress.String()
msg := types.NewMsgCreateReport(subspaceID, reasons, message, target, reporter)
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().String(FlagUser, "", "Address of the user to be reported")
cmd.Flags().Uint64(FlagPostID, 0, "Id the post to be reported")
cmd.Flags().String(FlagMessage, "", "Optional message associated with the report")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdDeleteReport returns the command allowing to delete a report
func GetCmdDeleteReport() *cobra.Command {
cmd := &cobra.Command{
Use: "delete [subspace-id] [report-id]",
Args: cobra.ExactArgs(2),
Short: "Delete a report",
Long: "Delete the report having the given id from the specified subspace",
Example: fmt.Sprintf(`%s tx reports delete 1 1 --from alice`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := subspacestypes.ParseSubspaceID(args[0])
if err != nil {
return err
}
reportID, err := types.ParseReportID(args[1])
if err != nil {
return err
}
signer := clientCtx.FromAddress.String()
msg := types.NewMsgDeleteReport(subspaceID, reportID, signer)
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// --------------------------------------------------------------------------------------------------------------------
// NewReasonsTxCmd returns a new command to perform reasons transactions
func NewReasonsTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "reasons",
Short: "Reports reasons transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
cmd.AddCommand(
GetCmdSupportStandardReason(),
GetCmdAddReason(),
GetCmdRemoveReason(),
)
return cmd
}
// GetCmdSupportStandardReason returns the command allowing to support a standard reason
func GetCmdSupportStandardReason() *cobra.Command {
cmd := &cobra.Command{
Use: "support-standard [subspace-id] [reason-id]",
Args: cobra.ExactArgs(2),
Short: "Support a standard reporting reason",
Long: "Add the support for the specific standard reporting reason inside the subspace",
Example: fmt.Sprintf(`%s tx reports reasons support-standard 1 1 --from alice`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := subspacestypes.ParseSubspaceID(args[0])
if err != nil {
return err
}
reasonID, err := types.ParseReasonID(args[1])
if err != nil {
return err
}
signer := clientCtx.FromAddress.String()
msg := types.NewMsgSupportStandardReason(subspaceID, reasonID, signer)
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdAddReason returns the command allowing to add a new reporting reason
func GetCmdAddReason() *cobra.Command {
cmd := &cobra.Command{
Use: "add [subspace-id] [title] [[description]]",
Args: cobra.RangeArgs(2, 3),
Short: "Add a new reporting reason",
Long: "Add a new reporting reason with the given title and optional description to a subspace",
Example: fmt.Sprintf(`
%s tx reports reasons add 1 "Spam" "Spam content or spammer user" \
--from alice
`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := subspacestypes.ParseSubspaceID(args[0])
if err != nil {
return err
}
title := args[1]
var description string
if len(args) > 2 {
description = args[2]
}
signer := clientCtx.FromAddress.String()
msg := types.NewMsgAddReason(subspaceID, title, description, signer)
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdRemoveReason returns the command allowing to remove a reporting reason
func GetCmdRemoveReason() *cobra.Command {
cmd := &cobra.Command{
Use: "remove [subspace-id] [reason-id]",
Args: cobra.ExactArgs(2),
Short: "Remove a reporting reason",
Long: "Remove the reporting reason having the given id from the specified subspace",
Example: fmt.Sprintf(`%s tx reports reasons remove 1 1 --from alice`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := subspacestypes.ParseSubspaceID(args[0])
if err != nil {
return err
}
reasonID, err := types.ParseReasonID(args[1])
if err != nil {
return err
}
signer := clientCtx.FromAddress.String()
msg := types.NewMsgRemoveReason(subspaceID, reasonID, signer)
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}