-
Notifications
You must be signed in to change notification settings - Fork 7
/
tx_comment.go
executable file
·230 lines (195 loc) · 7.57 KB
/
tx_comment.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
package cli
import (
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/coinexchain/cet-sdk/modules/comment/internal/types"
"github.com/coinexchain/cosmos-utils/client/cliutil"
)
const (
FlagToken = "token"
FlagDonation = "donation"
FlagTitle = "title"
FlagContent = "content"
FlagContentType = "content-type"
FlagRewardTo = "reward-to"
FlagFollow = "follow"
)
var createNewThreadFlags = []string{
FlagToken,
FlagDonation,
FlagTitle,
FlagContent,
FlagContentType,
}
var createFollowupCommentFlags = []string{
FlagToken,
FlagDonation,
FlagTitle,
FlagContent,
FlagContentType,
FlagFollow,
}
var rewardCommentsFlags = []string{
FlagToken,
FlagRewardTo,
}
func CreateNewThreadCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "new-thread",
Short: "Create a new thread of comments under some token",
Long: `Post a comment under some token, which creates a new thread, instead of following any other comments.
Example:
cetcli tx comment new-thread --token=cet --donation=2
--title="I love cet." --content="CET to da moon!!!" --content-type=UTF8Text
`,
RunE: func(cmd *cobra.Command, args []string) error {
return createAndBroadcastComment(cdc, "new-thread", nil)
},
}
markCreateNewThreadFlags(cmd)
return cmd
}
func markCreateNewThreadFlags(cmd *cobra.Command) {
cmd.Flags().String(FlagToken, "cet", "The token you want to comment about")
cmd.Flags().Int(FlagDonation, 0, "The donation to community pool. The more you donate, the more your comment weights.")
cmd.Flags().String(FlagTitle, "", "The comment's title")
cmd.Flags().String(FlagContent, "", "The comment's content")
cmd.Flags().String(FlagContentType, "UTF8Text", "The type of the comment's content (IPFS, Magnet, HTTP, UTF8Text, ShortHanzi or RawBytes)")
cmd.Flags().Bool(cliutil.FlagGenerateUnsignedTx, false, "Generate a unsigned tx")
for _, flag := range createNewThreadFlags {
cmd.MarkFlagRequired(flag)
}
}
func CreateFollowupCommentCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "follow-up",
Short: "Create a follow-up comment in a thread",
Long: `Post a comment to follow another comment in a thread.
Example:
cetcli tx comment follow-up --token=cet --donation=0 --follow="10001;coinex1qw508d6qejxtdg4y5r2zarvary0c0xw9kv8f3t4;2;cet;like,favorite"
--title="I love cet too." --content="CET to da moon!!!" --content-type=UTF8Text
`,
RunE: func(cmd *cobra.Command, args []string) error {
return createAndBroadcastComment(cdc, "follow-up", nil)
},
}
markCreateFollowupCommentFlags(cmd)
return cmd
}
func markCreateFollowupCommentFlags(cmd *cobra.Command) {
cmd.Flags().String(FlagToken, "cet", "The token you want to comment about")
cmd.Flags().Int(FlagDonation, 0, "The donation to community pool. If you have negative opinion against the comment you are following, please donate some cet tokens to prove your dissatisfaction.")
cmd.Flags().String(FlagFollow, "", "Some information about the comment you are following. Should be like this: \"<comment-id>;<the-sender-of-the-comment>;<reward-amount>;<reward-token>;<comma-separated-attitued-list>\". Valid attitudes include: like, dislike, laugh, cry, angry, surprise, heart, sweat, speechless, favorite, condolences.")
cmd.Flags().String(FlagTitle, "", "The comment's title")
cmd.Flags().String(FlagContent, "", "The comment's content")
cmd.Flags().String(FlagContentType, "UTF8Text", "The type of the comment's content (IPFS, Magnet, HTTP, UTF8Text, ShortHanzi or RawBytes)")
cmd.Flags().Bool(cliutil.FlagGenerateUnsignedTx, false, "Generate a unsigned tx")
for _, flag := range createFollowupCommentFlags {
cmd.MarkFlagRequired(flag)
}
}
func RewardCommentsCmd(cdc *codec.Codec) *cobra.Command {
var rewardsArray []string
cmd := &cobra.Command{
Use: "reward-comments",
Short: "Reward some comments that you like",
Long: `Reward the senders and some comments that you like, while showing why you like them individually.
Example:
cetcli tx comment reward-comments --token=cet --reward-to="10001;coinex1qi598e62ejitdg4yur3zarvary0c5xw7kv8f3t4;2;cet;like,favorite" --reward-to="20021;coinex1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4;1;cet;like"
`,
RunE: func(cmd *cobra.Command, args []string) error {
err := createAndBroadcastComment(cdc, "reward-comments", &rewardsArray)
rewardsArray = rewardsArray[:0]
return err
},
}
cmd.Flags().String(FlagToken, "cet", "The token you want to comment about")
cmd.Flags().StringArrayVar(&rewardsArray, FlagRewardTo, nil, "You can use this option multiple times to reward multiple comments. This option specify some information about one comment you want to reward. Should be like this: \"<comment-id>;<the-sender-of-the-comment>;<reward-amount>;<reward-token>;<comma-separated-attitued-list>\". Valid attitudes include: like, dislike, laugh, cry, angry, surprise, heart, sweat, speechless, favorite, condolences.")
cmd.Flags().Bool(cliutil.FlagGenerateUnsignedTx, false, "Generate a unsigned tx")
cmd.MarkFlagRequired(FlagToken)
cmd.MarkFlagRequired(FlagRewardTo)
return cmd
}
func createAndBroadcastComment(cdc *codec.Codec, subcmd string, rewardsArrayPtr *[]string) error {
msg, err := parseFlags(rewardsArrayPtr)
if err != nil {
return errors.Errorf("tx flag is error (%v), please see help : "+
"$ cetcli tx comment %s -h", err, subcmd)
}
if len(msg.References) <= 1 && len(msg.Title) == 0 {
msg.Title = "reward-comments"
}
if len(msg.Content) == 0 {
msg.Content = []byte("No-Content")
}
return cliutil.CliRunCommand(cdc, msg)
}
func parseRewardLine(line string) (*types.CommentRef, error) {
symbols := strings.Split(line, ";")
if len(symbols) != 5 {
return nil, errors.Errorf("invalid format: " + line)
}
id, err := strconv.ParseUint(symbols[0], 10, 64)
if err != nil {
return nil, errors.Errorf("Not a valid comment id: " + symbols[0])
}
target, err := sdk.AccAddressFromBech32(symbols[1])
if err != nil {
return nil, errors.Errorf("Not a valid address: " + symbols[1])
}
amt, err := strconv.ParseInt(symbols[2], 10, 64)
if err != nil {
return nil, errors.Errorf("Not a valid amount: " + symbols[2])
}
attitudes := strings.Split(symbols[4], ",")
attList := make([]int32, len(attitudes))
for i, a := range attitudes {
attList[i] = types.ParseAttitude(a)
if attList[i] < 0 {
return nil, errors.Errorf("invalid attitude: " + a)
}
}
cref := &types.CommentRef{
ID: id,
RewardTarget: target,
RewardToken: symbols[3],
RewardAmount: amt,
Attitudes: attList,
}
return cref, nil
}
func parseFlags(rewardsArrayPtr *[]string) (*types.MsgCommentToken, error) {
ctstr := viper.GetString(FlagContentType)
ct := types.ParseContentType(ctstr)
if ct < 0 {
return nil, errors.Errorf(ctstr + " is not a valid content type.")
}
var references []types.CommentRef
followup := viper.GetString(FlagFollow)
if len(followup) != 0 {
cref, err := parseRewardLine(followup)
if err != nil {
return nil, err
}
references = []types.CommentRef{*cref}
} else if rewardsArrayPtr != nil {
references = make([]types.CommentRef, 0, len(*rewardsArrayPtr))
for _, line := range *rewardsArrayPtr {
cref, err := parseRewardLine(line)
if err != nil {
return nil, err
}
references = append(references, *cref)
}
}
token := viper.GetString(FlagToken)
donation := viper.GetInt64(FlagDonation)
title := viper.GetString(FlagTitle)
content := viper.GetString(FlagContent)
return types.NewMsgCommentToken(nil, token, donation, title, content, ct, references), nil
}