-
Notifications
You must be signed in to change notification settings - Fork 44
/
models.go
94 lines (75 loc) · 2.74 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
package types
import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewRelationship returns a new relationships with the given recipient and subspace
func NewRelationship(user string, counterparty string, subspaceID uint64) Relationship {
return Relationship{
Creator: user,
Counterparty: counterparty,
SubspaceID: subspaceID,
}
}
// Validate implement Validator
func (r Relationship) Validate() error {
_, err := sdk.AccAddressFromBech32(r.Creator)
if err != nil {
return fmt.Errorf("invalid creator address: %s", r.Creator)
}
_, err = sdk.AccAddressFromBech32(r.Counterparty)
if err != nil {
return fmt.Errorf("invalid counterparty address: %s", r.Counterparty)
}
if r.Creator == r.Counterparty {
return fmt.Errorf("creator and recipient cannot be the same user")
}
return nil
}
// MustMarshalRelationship serializes the given relationship using the provided BinaryCodec
func MustMarshalRelationship(cdc codec.BinaryCodec, relationship Relationship) []byte {
return cdc.MustMarshal(&relationship)
}
// MustUnmarshalRelationship deserializes the given byte array as a relationship using
// the provided BinaryCodec
func MustUnmarshalRelationship(cdc codec.BinaryCodec, bz []byte) Relationship {
var relationship Relationship
cdc.MustUnmarshal(bz, &relationship)
return relationship
}
// --------------------------------------------------------------------------------------------------------------------
// NewUserBlock returns a new object representing the fact that one user has blocked another one
// for a specific reason on the given subspace.
func NewUserBlock(blocker, blocked string, reason string, subspaceID uint64) UserBlock {
return UserBlock{
Blocker: blocker,
Blocked: blocked,
Reason: reason,
SubspaceID: subspaceID,
}
}
// Validate implements validator
func (ub UserBlock) Validate() error {
if len(ub.Blocker) == 0 {
return fmt.Errorf("blocker address cannot be empty")
}
if len(ub.Blocked) == 0 {
return fmt.Errorf("the address of the blocked user cannot be empty")
}
if ub.Blocker == ub.Blocked {
return fmt.Errorf("blocker and blocked addresses cannot be equals")
}
return nil
}
// --------------------------------------------------------------------------------------------------------------------
// MustMarshalUserBlock serializes the given user block using the provided BinaryCodec
func MustMarshalUserBlock(cdc codec.BinaryCodec, userBlock UserBlock) []byte {
return cdc.MustMarshal(&userBlock)
}
// MustUnmarshalUserBlock deserializes the given byte array as a UserBlock using the provided BinaryCodec
func MustUnmarshalUserBlock(cdc codec.BinaryCodec, bz []byte) UserBlock {
var block UserBlock
cdc.MustUnmarshal(bz, &block)
return block
}