-
Notifications
You must be signed in to change notification settings - Fork 44
/
utils.go
165 lines (140 loc) · 4.7 KB
/
utils.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
package simulation
// DONTCOVER
import (
"math/rand"
"time"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/desmos-labs/desmos/v7/x/posts/types"
)
var (
mediaTypes = []string{
"image/png",
"image/jpeg",
"image/gif",
"video/mp4",
}
allowedReplySettings = []types.ReplySetting{
types.REPLY_SETTING_EVERYONE,
types.REPLY_SETTING_MENTIONS,
types.REPLY_SETTING_FOLLOWERS,
types.REPLY_SETTING_MUTUAL,
}
)
// GenerateRandomPost generates a random post
func GenerateRandomPost(r *rand.Rand, accounts []simtypes.Account, subspaceID uint64, sectionID uint32, postID uint64, params types.Params) types.Post {
author, _ := simtypes.RandomAcc(r, accounts)
owner, _ := simtypes.RandomAcc(r, accounts)
return types.NewPost(
subspaceID,
sectionID,
postID,
GenerateRandomText(r, 20),
GenerateRandomText(r, params.MaxTextLength),
author.Address.String(),
0,
nil,
GenerateRandomTags(r, 10),
nil,
RandomReplySettings(r),
time.Now(),
nil,
owner.Address.String(),
)
}
// GenerateRandomText returns a random text that does not exceed the given max length
func GenerateRandomText(r *rand.Rand, maxLength uint32) string {
return simtypes.RandStringOfLength(r, int(maxLength))
}
// GenerateRandomTags returns a slice containing tagsNumber randomly generated tags
func GenerateRandomTags(r *rand.Rand, tagsNumber int) []string {
tags := make([]string, tagsNumber)
for i := 0; i < tagsNumber; i++ {
tags[i] = simtypes.RandStringOfLength(r, 10)
}
return tags
}
// RandomReplySettings returns a random valid ReplySetting for the post
func RandomReplySettings(r *rand.Rand) types.ReplySetting {
return allowedReplySettings[r.Intn(len(allowedReplySettings))]
}
// RandomPost returns a random post from the slice given
func RandomPost(r *rand.Rand, posts []types.Post) types.Post {
return posts[r.Intn(len(posts))]
}
// GenerateRandomAttachment generates a random attachment
func GenerateRandomAttachment(r *rand.Rand, post types.Post, id uint32) types.Attachment {
return types.NewAttachment(
post.SubspaceID,
post.ID,
id,
GenerateRandomAttachmentContent(r, time.Now()),
)
}
// GenerateRandomAttachmentContent returns a randomly generated attachment content
func GenerateRandomAttachmentContent(r *rand.Rand, currentTime time.Time) types.AttachmentContent {
// 50% of being a poll
if r.Intn(101) < 50 {
return GenerateRandomPoll(r, currentTime)
}
return GenerateRandomMedia(r)
}
// GenerateRandomMedia returns a randomly generated media content
func GenerateRandomMedia(r *rand.Rand) *types.Media {
return types.NewMedia(
GenerateRandomText(r, 50),
mediaTypes[r.Intn(len(mediaTypes))],
)
}
// GenerateRandomPoll returns a randomly generated poll content
func GenerateRandomPoll(r *rand.Rand, currentTime time.Time) *types.Poll {
answersNumber := r.Intn(5) + 2 // At least 2 answers are required to make sense
answers := make([]types.Poll_ProvidedAnswer, answersNumber)
for index := 0; index < answersNumber; index++ {
answers[index] = types.NewProvidedAnswer(GenerateRandomText(r, 10), nil)
}
// 50% of accepting multiple answer edits
acceptsMultipleAnswers := r.Intn(101) < 50
// 50% of allowing answers edits
allowAnswerEdits := r.Intn(101) < 50
return types.NewPoll(
GenerateRandomText(r, 30),
answers,
currentTime.Add(30*24*time.Hour),
acceptsMultipleAnswers,
allowAnswerEdits,
nil,
)
}
// RandomAttachment returns a random attachment from the ones given
func RandomAttachment(r *rand.Rand, attachments []types.Attachment) types.Attachment {
return attachments[r.Intn(len(attachments))]
}
// RandomAnswersIndexes returns a random answers indexes slice based on the given poll
func RandomAnswersIndexes(r *rand.Rand, poll *types.Poll) (answersIndexes []uint32) {
maxAnswersNumber := 1
if poll.AllowsMultipleAnswers {
maxAnswersNumber = r.Intn(len(poll.ProvidedAnswers)) + 1
}
// Generate some answer indexes
indexes := make([]uint32, maxAnswersNumber)
for index := 0; index < maxAnswersNumber; index++ {
indexes[index] = uint32(r.Intn(len(poll.ProvidedAnswers)))
}
// Eliminate duplicated generated indexes
uniqueIndexes := map[uint32]bool{}
for _, index := range indexes {
if _, inserted := uniqueIndexes[index]; !inserted {
answersIndexes = append(answersIndexes, index)
}
uniqueIndexes[index] = true
}
return answersIndexes
}
// RandomMaxTextLength returns a random max text length in the [30, 999] range
func RandomMaxTextLength(r *rand.Rand) uint32 {
return uint32(r.Intn(950)) + 30
}
// RandomPostOwnerTransferRequest returns a random post owner transfer request from the ones given
func RandomPostOwnerTransferRequest(r *rand.Rand, requests []types.PostOwnerTransferRequest) types.PostOwnerTransferRequest {
return requests[r.Intn(len(requests))]
}