/
models.go
130 lines (103 loc) · 2.63 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
package item
import (
"encoding/json"
"sort"
"strings"
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
type Type string
const (
TypeDao Type = "dao"
TypeProposal Type = "proposal"
None TimelineAction = ""
DaoCreated TimelineAction = "dao.created"
DaoUpdated TimelineAction = "dao.updated"
ProposalCreated TimelineAction = "proposal.created"
ProposalUpdated TimelineAction = "proposal.updated"
ProposalVotingStartsSoon TimelineAction = "proposal.voting.starts_soon"
ProposalVotingEndsSoon TimelineAction = "proposal.voting.ends_soon"
ProposalVotingStarted TimelineAction = "proposal.voting.started"
ProposalVotingQuorumReached TimelineAction = "proposal.voting.quorum_reached"
ProposalVotingEnded TimelineAction = "proposal.voting.ended"
)
type FeedItem struct {
ID uuid.UUID `gorm:"primarykey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
TriggeredAt time.Time `gorm:"index"`
DaoID uuid.UUID
ProposalID string
DiscussionID string
Type Type
Action TimelineAction
Snapshot json.RawMessage
Timeline Timeline `gorm:"serializer:json"`
}
type Timeline []TimelineItem
func (t *Timeline) AddUniqueAction(createdAt time.Time, action TimelineAction) (isNew bool) {
if *t == nil {
*t = make(Timeline, 0, 1)
}
for i := range *t {
if (*t)[i].Action.Equals(action) {
return false
}
}
*t = append(*t, TimelineItem{
CreatedAt: createdAt,
Action: action,
})
return true
}
func (t *Timeline) AddNonUniqueAction(createdAt time.Time, action TimelineAction) {
if *t == nil {
*t = make(Timeline, 0, 1)
}
*t = append(*t, TimelineItem{
CreatedAt: createdAt,
Action: action,
})
}
func (t *Timeline) ContainsAction(action TimelineAction) bool {
if t == nil || len(*t) == 0 {
return false
}
for _, item := range *t {
if item.Action.Equals(action) {
return true
}
}
return false
}
func (t *Timeline) Sort() {
if t == nil || len(*t) == 0 {
return
}
sort.SliceStable(*t, func(i, j int) bool {
return (*t)[i].CreatedAt.Before((*t)[j].CreatedAt)
})
}
func (t *Timeline) LastAction() TimelineAction {
if t == nil {
return None
}
if len(*t) == 0 {
return None
}
return (*t)[len(*t)-1].Action
}
type TimelineItem struct {
CreatedAt time.Time `json:"created_at"`
Action TimelineAction `json:"action"`
}
type TimelineAction string
func (a TimelineAction) Equals(action TimelineAction) bool {
return strings.EqualFold(string(a), string(action))
}
type FeedList struct {
Items []FeedItem
TotalCount int64
}