forked from planetdecred/dcrlibwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoliteia.go
245 lines (197 loc) · 5.85 KB
/
politeia.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package dcrlibwallet
import (
"context"
"encoding/json"
"fmt"
"sync"
"decred.org/dcrwallet/v2/errors"
"github.com/asdine/storm"
"github.com/asdine/storm/q"
)
type Politeia struct {
mwRef *MultiWallet
host string
mu sync.RWMutex
ctx context.Context
cancelSync context.CancelFunc
client *politeiaClient
notificationListenersMu sync.RWMutex
notificationListeners map[string]ProposalNotificationListener
}
const (
ProposalCategoryAll int32 = iota + 1
ProposalCategoryPre
ProposalCategoryActive
ProposalCategoryApproved
ProposalCategoryRejected
ProposalCategoryAbandoned
)
func newPoliteia(mwRef *MultiWallet, host string) (*Politeia, error) {
p := &Politeia{
mwRef: mwRef,
host: host,
client: nil,
notificationListeners: make(map[string]ProposalNotificationListener),
}
return p, nil
}
func (p *Politeia) saveLastSyncedTimestamp(lastSyncedTimestamp int64) {
p.mwRef.SetLongConfigValueForKey(PoliteiaLastSyncedTimestampConfigKey, lastSyncedTimestamp)
}
func (p *Politeia) getLastSyncedTimestamp() int64 {
return p.mwRef.ReadLongConfigValueForKey(PoliteiaLastSyncedTimestampConfigKey, 0)
}
func (p *Politeia) saveOrOverwiteProposal(proposal *Proposal) error {
var oldProposal Proposal
err := p.mwRef.db.One("Token", proposal.Token, &oldProposal)
if err != nil && err != storm.ErrNotFound {
return errors.Errorf("error checking if proposal was already indexed: %s", err.Error())
}
if oldProposal.Token != "" {
// delete old record before saving new (if it exists)
p.mwRef.db.DeleteStruct(oldProposal)
}
return p.mwRef.db.Save(proposal)
}
// GetProposalsRaw fetches and returns a proposals from the db
func (p *Politeia) GetProposalsRaw(category int32, offset, limit int32, newestFirst bool) ([]Proposal, error) {
return p.getProposalsRaw(category, offset, limit, newestFirst, false)
}
func (p *Politeia) getProposalsRaw(category int32, offset, limit int32, newestFirst bool, skipAbandoned bool) ([]Proposal, error) {
var query storm.Query
switch category {
case ProposalCategoryAll:
if skipAbandoned {
query = p.mwRef.db.Select(
q.Not(q.Eq("Category", ProposalCategoryAbandoned)),
)
} else {
query = p.mwRef.db.Select(
q.True(),
)
}
default:
query = p.mwRef.db.Select(
q.Eq("Category", category),
)
}
if offset > 0 {
query = query.Skip(int(offset))
}
if limit > 0 {
query = query.Limit(int(limit))
}
if newestFirst {
query = query.OrderBy("PublishedAt").Reverse()
} else {
query = query.OrderBy("PublishedAt")
}
var proposals []Proposal
err := query.Find(&proposals)
if err != nil && err != storm.ErrNotFound {
return nil, fmt.Errorf("error fetching proposals: %s", err.Error())
}
return proposals, nil
}
// GetProposals returns the result of GetProposalsRaw as a JSON string
func (p *Politeia) GetProposals(category int32, offset, limit int32, newestFirst bool) (string, error) {
result, err := p.GetProposalsRaw(category, offset, limit, newestFirst)
if err != nil {
return "", err
}
if len(result) == 0 {
return "[]", nil
}
response, err := json.Marshal(result)
if err != nil {
return "", fmt.Errorf("error marshalling result: %s", err.Error())
}
return string(response), nil
}
// GetProposalRaw fetches and returns a single proposal specified by it's censorship record token
func (p *Politeia) GetProposalRaw(censorshipToken string) (*Proposal, error) {
var proposal Proposal
err := p.mwRef.db.One("Token", censorshipToken, &proposal)
if err != nil {
return nil, err
}
return &proposal, nil
}
// GetProposal returns the result of GetProposalRaw as a JSON string
func (p *Politeia) GetProposal(censorshipToken string) (string, error) {
return p.marshalResult(p.GetProposalRaw(censorshipToken))
}
// GetProposalByIDRaw fetches and returns a single proposal specified by it's ID
func (p *Politeia) GetProposalByIDRaw(proposalID int) (*Proposal, error) {
var proposal Proposal
err := p.mwRef.db.One("ID", proposalID, &proposal)
if err != nil {
return nil, err
}
return &proposal, nil
}
// GetProposalByID returns the result of GetProposalByIDRaw as a JSON string
func (p *Politeia) GetProposalByID(proposalID int) (string, error) {
return p.marshalResult(p.GetProposalByIDRaw(proposalID))
}
// Count returns the number of proposals of a specified category
func (p *Politeia) Count(category int32) (int32, error) {
var matcher q.Matcher
if category == ProposalCategoryAll {
matcher = q.True()
} else {
matcher = q.Eq("Category", category)
}
count, err := p.mwRef.db.Select(matcher).Count(&Proposal{})
if err != nil {
return 0, err
}
return int32(count), nil
}
func (p *Politeia) Overview() (*ProposalOverview, error) {
pre, err := p.Count(ProposalCategoryPre)
if err != nil {
return nil, err
}
active, err := p.Count(ProposalCategoryActive)
if err != nil {
return nil, err
}
approved, err := p.Count(ProposalCategoryApproved)
if err != nil {
return nil, err
}
rejected, err := p.Count(ProposalCategoryRejected)
if err != nil {
return nil, err
}
abandoned, err := p.Count(ProposalCategoryApproved)
if err != nil {
return nil, err
}
return &ProposalOverview{
All: pre + active + approved + rejected + abandoned,
Discussion: pre,
Voting: active,
Approved: approved,
Rejected: rejected,
Abandoned: abandoned,
}, nil
}
func (p *Politeia) ClearSavedProposals() error {
err := p.mwRef.db.Drop(&Proposal{})
if err != nil {
return translateError(err)
}
return p.mwRef.db.Init(&Proposal{})
}
func (p *Politeia) marshalResult(result interface{}, err error) (string, error) {
if err != nil {
return "", translateError(err)
}
response, err := json.Marshal(result)
if err != nil {
return "", fmt.Errorf("error marshalling result: %s", err.Error())
}
return string(response), nil
}