-
Notifications
You must be signed in to change notification settings - Fork 44
/
models.go
155 lines (123 loc) · 3.83 KB
/
models.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
package types
import (
"fmt"
"strings"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/desmos-labs/desmos/x/commons"
)
// NewPictures is a constructor function for Pictures
func NewPictures(profile, cover string) Pictures {
return Pictures{
Profile: profile,
Cover: cover,
}
}
// Validate check the validity of the Pictures
func (pic Pictures) Validate() error {
if pic.Profile != "" {
valid := commons.IsURIValid(pic.Profile)
if !valid {
return fmt.Errorf("invalid profile picture uri provided")
}
}
if pic.Cover != "" {
valid := commons.IsURIValid(pic.Cover)
if !valid {
return fmt.Errorf("invalid profile cover uri provided")
}
}
return nil
}
// ___________________________________________________________________________________________________________________
// NewProfile builds a new profile having the given dtag, creator and creation date
func NewProfile(dtag string, moniker, bio string, pictures Pictures, creationDate time.Time, creator string) Profile {
return Profile{
Dtag: dtag,
Moniker: moniker,
Bio: bio,
Pictures: pictures,
Creator: creator,
CreationDate: creationDate,
}
}
// Update updates the fields of a given profile. An error is
// returned if the resulting profile contains invalid values.
func (profile Profile) Update(p2 Profile) (Profile, error) {
if p2.Dtag == DoNotModify {
p2.Dtag = profile.Dtag
}
if p2.Moniker == DoNotModify {
p2.Moniker = profile.Moniker
}
if p2.Bio == DoNotModify {
p2.Bio = profile.Bio
}
if p2.Pictures.Profile == DoNotModify {
p2.Pictures.Profile = profile.Pictures.Profile
}
if p2.Pictures.Cover == DoNotModify {
p2.Pictures.Cover = profile.Pictures.Cover
}
if p2.CreationDate.IsZero() {
p2.CreationDate = profile.CreationDate
}
if p2.Creator == DoNotModify {
p2.Creator = profile.Creator
}
newProfile := NewProfile(p2.Dtag, p2.Moniker, p2.Bio, p2.Pictures, p2.CreationDate, p2.Creator)
err := newProfile.Validate()
if err != nil {
return Profile{}, err
}
return newProfile, nil
}
// Validate check the validity of the Profile
func (profile Profile) Validate() error {
if strings.TrimSpace(profile.Dtag) == "" || profile.Dtag == DoNotModify {
return fmt.Errorf("invalid profile DTag: %s", profile.Dtag)
}
if profile.Moniker == DoNotModify {
return fmt.Errorf("invalid profile moniker: %s", profile.Moniker)
}
if profile.Bio == DoNotModify {
return fmt.Errorf("invalid profile bio: %s", profile.Bio)
}
if profile.Pictures.Profile == DoNotModify {
return fmt.Errorf("invalid profile picture: %s", profile.Pictures.Profile)
}
if profile.Pictures.Cover == DoNotModify {
return fmt.Errorf("invalid profile cover: %s", profile.Pictures.Cover)
}
_, err := sdk.AccAddressFromBech32(profile.Creator)
if err != nil {
return fmt.Errorf("invalid creator address: %s", profile.Creator)
}
return profile.Pictures.Validate()
}
// ___________________________________________________________________________________________________________________
func NewDTagTransferRequest(dtagToTrade string, sender, receiver string) DTagTransferRequest {
return DTagTransferRequest{
DtagToTrade: dtagToTrade,
Receiver: receiver,
Sender: sender,
}
}
// Validate checks the request validity
func (request DTagTransferRequest) Validate() error {
_, err := sdk.AccAddressFromBech32(request.Sender)
if err != nil {
return fmt.Errorf("invalid DTag transfer request sender address: %s", request.Sender)
}
_, err = sdk.AccAddressFromBech32(request.Receiver)
if err != nil {
return fmt.Errorf("invalid receiver address: %s", request.Receiver)
}
if request.Receiver == request.Sender {
return fmt.Errorf("the sender and receiver must be different")
}
if strings.TrimSpace(request.DtagToTrade) == "" {
return fmt.Errorf("invalid DTag to trade: %s", request.DtagToTrade)
}
return nil
}