-
Notifications
You must be signed in to change notification settings - Fork 44
/
reactions.go
106 lines (84 loc) · 3.26 KB
/
reactions.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
package types
import (
"fmt"
"strings"
"github.com/cosmos/cosmos-sdk/codec"
subspacestypes "github.com/desmos-labs/desmos/x/staging/subspaces/types"
"github.com/desmos-labs/desmos/x/commons"
)
// NewRegisteredReaction returns a new RegisteredReaction
func NewRegisteredReaction(creator string, shortCode, value, subspace string) RegisteredReaction {
return RegisteredReaction{
ShortCode: shortCode,
Value: value,
Subspace: subspace,
Creator: creator,
}
}
// Validate implements validator
func (reaction RegisteredReaction) Validate() error {
if reaction.Creator == "" {
return fmt.Errorf("invalid reaction creator: %s", reaction.Creator)
}
if !IsValidReactionCode(reaction.ShortCode) {
return fmt.Errorf("the specified shortcode is not valid. To be valid it must only contains a-z, 0-9, - and _ and must start and end with a ':'")
}
if !commons.IsURIValid(reaction.Value) {
return fmt.Errorf("reaction value should be a URL")
}
if !subspacestypes.IsValidSubspace(reaction.Subspace) {
return fmt.Errorf("reaction subspace must be a valid sha-256 hash")
}
if _, found := GetEmojiByShortCodeOrValue(reaction.ShortCode); found {
return fmt.Errorf("reaction has emoji shortcode: %s", reaction.ShortCode)
}
return nil
}
// MustMarshalRegisteredReaction serializes the given registered reaction using the provided BinaryMarshaler
func MustMarshalRegisteredReaction(cdc codec.BinaryMarshaler, reaction RegisteredReaction) []byte {
return cdc.MustMarshalBinaryBare(&reaction)
}
// MustUnmarshalRegisteredReaction deserializes the given byte array as a registered reaction using
// the provided BinaryMarshaler
func MustUnmarshalRegisteredReaction(cdc codec.BinaryMarshaler, bz []byte) RegisteredReaction {
var reaction RegisteredReaction
cdc.MustUnmarshalBinaryBare(bz, &reaction)
return reaction
}
// ___________________________________________________________________________________________________________________
// NewPostReaction returns a new PostReaction
func NewPostReaction(postID, shortcode, value, owner string) PostReaction {
return PostReaction{
PostID: postID,
ShortCode: shortcode,
Value: value,
Owner: owner,
}
}
// Validate implements validator
func (reaction PostReaction) Validate() error {
if !IsValidPostID(reaction.PostID) {
return fmt.Errorf("invalid post id: %s", reaction.PostID)
}
if reaction.Owner == "" {
return fmt.Errorf("invalid reaction owner: %s", reaction.Owner)
}
if len(strings.TrimSpace(reaction.Value)) == 0 {
return fmt.Errorf("reaction value cannot be empty or blank")
}
if !IsValidReactionCode(reaction.ShortCode) {
return fmt.Errorf("the specified shortcode is not valid. To be valid it must only contains a-z, 0-9, - and _ and must start and end with a ':'")
}
return nil
}
// MustMarshalPostReaction serializes the given post reaction using the provided BinaryMarshaler
func MustMarshalPostReaction(cdc codec.BinaryMarshaler, reaction PostReaction) []byte {
return cdc.MustMarshalBinaryBare(&reaction)
}
// MustUnmarshalPostReaction deserializes the given byte array as a post reaction using
// the provided BinaryMarshaler
func MustUnmarshalPostReaction(cdc codec.BinaryMarshaler, bz []byte) PostReaction {
var reaction PostReaction
cdc.MustUnmarshalBinaryBare(bz, &reaction)
return reaction
}