-
Notifications
You must be signed in to change notification settings - Fork 244
/
persistence.go
275 lines (231 loc) · 6.94 KB
/
persistence.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package verification
import (
"database/sql"
"errors"
"time"
)
var (
ErrVerificationRequestNotFound = errors.New("verification request not found")
)
type Persistence struct {
db *sql.DB
}
func NewPersistence(db *sql.DB) *Persistence {
return &Persistence{
db: db,
}
}
type RequestStatus int
const (
RequestStatusUNKNOWN RequestStatus = iota
RequestStatusPENDING
RequestStatusACCEPTED
RequestStatusDECLINED
RequestStatusCANCELED
RequestStatusTRUSTED
)
type TrustStatus int
const (
TrustStatusUNKNOWN TrustStatus = iota
TrustStatusTRUSTED
TrustStatusUNTRUSTWORTHY
)
type Request struct {
From string `json:"from"`
To string `json:"to"`
Challenge string `json:"challenge"`
Response string `json:"response"`
RequestedAt uint64 `json:"requested_at"`
RequestStatus RequestStatus `json:"verification_status"`
RepliedAt uint64 `json:"replied_at"`
}
func (p *Persistence) GetVerificationRequests() ([]Request, error) {
rows, err := p.db.Query("SELECT from_user, to_user, challenge, response, requested_at, verification_status, replied_at FROM verification_requests")
if err != nil {
return nil, err
}
defer rows.Close()
var result []Request
for rows.Next() {
var vr Request
err = rows.Scan(&vr.From, &vr.To, &vr.Challenge, &vr.Response, &vr.RequestedAt, &vr.RepliedAt, &vr.RequestStatus)
if err != nil {
return nil, err
}
result = append(result, vr)
}
return result, nil
}
func (p *Persistence) GetVerificationRequestFrom(contactID string) (*Request, error) {
var vr Request
err := p.db.QueryRow(`SELECT from_user, to_user, challenge, response, requested_at, verification_status, replied_at FROM verification_requests WHERE from_user = ?`, contactID).Scan(
&vr.From,
&vr.To,
&vr.Challenge,
&vr.Response,
&vr.RequestedAt,
&vr.RequestStatus,
&vr.RepliedAt,
)
switch err {
case sql.ErrNoRows:
return nil, nil
case nil:
return &vr, nil
default:
return nil, err
}
}
func (p *Persistence) GetReceivedVerificationRequests(myPublicKey string) ([]*Request, error) {
response := make([]*Request, 0)
query := `SELECT from_user, to_user, challenge, response, requested_at, verification_status, replied_at FROM verification_requests WHERE to_user = ?`
rows, err := p.db.Query(query, myPublicKey)
if err == sql.ErrNoRows {
return nil, nil
} else if err != nil {
return nil, err
}
for rows.Next() {
var vr Request
err := rows.Scan(
&vr.From,
&vr.To,
&vr.Challenge,
&vr.Response,
&vr.RequestedAt,
&vr.RequestStatus,
&vr.RepliedAt,
)
if err != nil {
return nil, err
}
response = append(response, &vr)
}
return response, nil
}
func (p *Persistence) GetVerificationRequestSentTo(contactID string) (*Request, error) {
var vr Request
err := p.db.QueryRow(`SELECT from_user, to_user, challenge, response, requested_at, verification_status, replied_at FROM verification_requests WHERE to_user = ?`, contactID).Scan(
&vr.From,
&vr.To,
&vr.Challenge,
&vr.Response,
&vr.RequestedAt,
&vr.RequestStatus,
&vr.RepliedAt,
)
switch err {
case sql.ErrNoRows:
return nil, nil
case nil:
return &vr, nil
default:
return nil, err
}
}
func (p *Persistence) SaveVerificationRequest(vr *Request) error {
if vr == nil {
return errors.New("invalid verification request provided")
}
_, err := p.db.Exec(`INSERT INTO verification_requests (from_user, to_user, challenge, response, requested_at, verification_status, replied_at) VALUES (?, ?, ?, ?, ?, ?, ?)`, vr.From, vr.To, vr.Challenge, vr.Response, vr.RequestedAt, vr.RequestStatus, vr.RepliedAt)
return err
}
func (p *Persistence) AcceptContactVerificationRequest(contactID string, response string) error {
result, err := p.db.Exec("UPDATE verification_requests SET response = ?, replied_at = ?, verification_status = ? WHERE from_user = ?", response, time.Now().Unix(), RequestStatusACCEPTED, contactID)
if err != nil {
return err
}
numRows, err := result.RowsAffected()
if err != nil {
return err
}
if numRows == 0 {
return ErrVerificationRequestNotFound
}
return nil
}
func (p *Persistence) DeclineContactVerificationRequest(contactID string) error {
result, err := p.db.Exec("UPDATE verification_requests SET response = '', replied_at = ?, verification_status = ? WHERE from_user = ?", time.Now().Unix(), RequestStatusDECLINED, contactID)
if err != nil {
return err
}
numRows, err := result.RowsAffected()
if err != nil {
return err
}
if numRows == 0 {
return ErrVerificationRequestNotFound
}
return nil
}
func (p *Persistence) UpsertVerificationRequest(request *Request) (shouldSync bool, err error) {
var dbRequestedAt uint64
var dbRepliedAt uint64
err = p.db.QueryRow(`SELECT requested_at, replied_at FROM verification_requests WHERE from_user = ? AND to_user = ?`, request.From, request.To).Scan(&dbRequestedAt, &dbRepliedAt)
if err == sql.ErrNoRows {
return true, p.SaveVerificationRequest(request)
}
if err == nil && ((dbRequestedAt < request.RequestedAt) || (dbRepliedAt < request.RepliedAt && dbRepliedAt != 0)) {
_, err := p.db.Exec("UPDATE verification_requests SET challenge = ?, response = ?, requested_at = ?, replied_at = ?, verification_status = ? WHERE from_user = ? AND to_user = ?",
request.Challenge,
request.Response,
request.RequestedAt,
request.RepliedAt,
request.RequestStatus,
request.From,
request.To)
if err == nil {
shouldSync = true
}
return shouldSync, err
}
return false, err
}
func (p *Persistence) SetTrustStatus(contactID string, trust TrustStatus, updatedAt uint64) error {
_, err := p.db.Exec(`INSERT INTO trusted_users (id, trust_status, updated_at) VALUES (?, ?, ?)`, contactID, trust, updatedAt)
return err
}
func (p *Persistence) UpsertTrustStatus(contactID string, trust TrustStatus, updatedAt uint64) (shouldSync bool, err error) {
var t uint64
err = p.db.QueryRow(`SELECT updated_at FROM trusted_users WHERE id = ?`, contactID).Scan(&t)
if err == sql.ErrNoRows {
return true, p.SetTrustStatus(contactID, trust, updatedAt)
}
if err == nil && updatedAt > t {
_, err := p.db.Exec("UPDATE trusted_users SET trust_status = ?, updated_at = ? WHERE id = ?", trust, updatedAt, contactID)
if err != nil {
return true, err
}
}
return false, err
}
func (p *Persistence) GetTrustStatus(contactID string) (TrustStatus, error) {
var t TrustStatus
err := p.db.QueryRow(`SELECT trust_status FROM trusted_users WHERE id = ?`, contactID).Scan(&t)
switch err {
case sql.ErrNoRows:
return TrustStatusUNKNOWN, nil
case nil:
return t, nil
default:
return TrustStatusUNKNOWN, err
}
}
func (p *Persistence) GetAllTrustStatus() (map[string]TrustStatus, error) {
result := make(map[string]TrustStatus)
rows, err := p.db.Query("SELECT id, trust_status FROM trusted_users")
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var id string
var ts TrustStatus
err = rows.Scan(&id, &ts)
if err != nil {
return nil, err
}
result[id] = ts
}
return result, nil
}