/
poll.go
565 lines (478 loc) · 14.2 KB
/
poll.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package bundb
import (
"context"
"errors"
"slices"
"time"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/state"
"github.com/superseriousbusiness/gotosocial/internal/util"
"github.com/uptrace/bun"
)
type pollDB struct {
db *bun.DB
state *state.State
}
func (p *pollDB) GetPollByID(ctx context.Context, id string) (*gtsmodel.Poll, error) {
return p.getPoll(
ctx,
"ID",
func(poll *gtsmodel.Poll) error {
return p.db.NewSelect().
Model(poll).
Where("? = ?", bun.Ident("poll.id"), id).
Scan(ctx)
},
id,
)
}
func (p *pollDB) getPoll(ctx context.Context, lookup string, dbQuery func(*gtsmodel.Poll) error, keyParts ...any) (*gtsmodel.Poll, error) {
// Fetch poll from database cache with loader callback
poll, err := p.state.Caches.GTS.Poll.LoadOne(lookup, func() (*gtsmodel.Poll, error) {
var poll gtsmodel.Poll
// Not cached! Perform database query.
if err := dbQuery(&poll); err != nil {
return nil, err
}
// Ensure vote slice
// is non nil and set.
poll.CheckVotes()
return &poll, nil
}, keyParts...)
if err != nil {
return nil, err
}
if gtscontext.Barebones(ctx) {
// no need to fully populate.
return poll, nil
}
// Further populate the poll fields where applicable.
if err := p.PopulatePoll(ctx, poll); err != nil {
return nil, err
}
return poll, nil
}
func (p *pollDB) GetOpenPolls(ctx context.Context) ([]*gtsmodel.Poll, error) {
var pollIDs []string
// Select all polls with unset `closed_at` time.
if err := p.db.NewSelect().
Table("polls").
Column("polls.id").
Join("JOIN ? ON ? = ?", bun.Ident("statuses"), bun.Ident("polls.id"), bun.Ident("statuses.poll_id")).
Where("? = true", bun.Ident("statuses.local")).
Where("? IS NULL", bun.Ident("polls.closed_at")).
Scan(ctx, &pollIDs); err != nil {
return nil, err
}
// Preallocate a slice to contain the poll models.
polls := make([]*gtsmodel.Poll, 0, len(pollIDs))
for _, id := range pollIDs {
// Attempt to fetch poll from DB.
poll, err := p.GetPollByID(ctx, id)
if err != nil {
log.Errorf(ctx, "error getting poll %s: %v", id, err)
continue
}
// Append poll to return slice.
polls = append(polls, poll)
}
return polls, nil
}
func (p *pollDB) PopulatePoll(ctx context.Context, poll *gtsmodel.Poll) error {
var (
err error
errs gtserror.MultiError
)
if poll.Status == nil {
// Vote account is not set, fetch from database.
poll.Status, err = p.state.DB.GetStatusByID(
gtscontext.SetBarebones(ctx),
poll.StatusID,
)
if err != nil {
errs.Appendf("error populating poll status: %w", err)
}
}
return errs.Combine()
}
func (p *pollDB) PutPoll(ctx context.Context, poll *gtsmodel.Poll) error {
// Ensure vote slice
// is non nil and set.
poll.CheckVotes()
return p.state.Caches.GTS.Poll.Store(poll, func() error {
_, err := p.db.NewInsert().Model(poll).Exec(ctx)
return err
})
}
func (p *pollDB) UpdatePoll(ctx context.Context, poll *gtsmodel.Poll, cols ...string) error {
// Ensure vote slice
// is non nil and set.
poll.CheckVotes()
return p.state.Caches.GTS.Poll.Store(poll, func() error {
return p.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Update the status' "updated_at" field.
if _, err := tx.NewUpdate().
Table("statuses").
Where("? = ?", bun.Ident("id"), poll.StatusID).
SetColumn("updated_at", "?", time.Now()).
Exec(ctx); err != nil {
return err
}
// Finally, update poll
// columns in database.
_, err := tx.NewUpdate().
Model(poll).
Column(cols...).
Where("? = ?", bun.Ident("id"), poll.ID).
Exec(ctx)
return err
})
})
}
func (p *pollDB) DeletePollByID(ctx context.Context, id string) error {
// Delete poll by ID from database.
if _, err := p.db.NewDelete().
Table("polls").
Where("? = ?", bun.Ident("id"), id).
Exec(ctx); err != nil {
return err
}
// Invalidate poll by ID from cache.
p.state.Caches.GTS.Poll.Invalidate("ID", id)
p.state.Caches.GTS.PollVoteIDs.Invalidate(id)
return nil
}
func (p *pollDB) GetPollVoteByID(ctx context.Context, id string) (*gtsmodel.PollVote, error) {
return p.getPollVote(
ctx,
"ID",
func(vote *gtsmodel.PollVote) error {
return p.db.NewSelect().
Model(vote).
Where("? = ?", bun.Ident("poll_vote.id"), id).
Scan(ctx)
},
id,
)
}
func (p *pollDB) GetPollVoteBy(ctx context.Context, pollID string, accountID string) (*gtsmodel.PollVote, error) {
return p.getPollVote(
ctx,
"PollID,AccountID",
func(vote *gtsmodel.PollVote) error {
return p.db.NewSelect().
Model(vote).
Where("? = ?", bun.Ident("poll_vote.account_id"), accountID).
Where("? = ?", bun.Ident("poll_vote.poll_id"), pollID).
Scan(ctx)
},
pollID,
accountID,
)
}
func (p *pollDB) getPollVote(ctx context.Context, lookup string, dbQuery func(*gtsmodel.PollVote) error, keyParts ...any) (*gtsmodel.PollVote, error) {
// Fetch vote from database cache with loader callback
vote, err := p.state.Caches.GTS.PollVote.LoadOne(lookup, func() (*gtsmodel.PollVote, error) {
var vote gtsmodel.PollVote
// Not cached! Perform database query.
if err := dbQuery(&vote); err != nil {
return nil, err
}
return &vote, nil
}, keyParts...)
if err != nil {
return nil, err
}
if gtscontext.Barebones(ctx) {
// no need to fully populate.
return vote, nil
}
// Further populate the vote fields where applicable.
if err := p.PopulatePollVote(ctx, vote); err != nil {
return nil, err
}
return vote, nil
}
func (p *pollDB) GetPollVotes(ctx context.Context, pollID string) ([]*gtsmodel.PollVote, error) {
// Load vote IDs known for given poll ID using loader callback.
voteIDs, err := p.state.Caches.GTS.PollVoteIDs.Load(pollID, func() ([]string, error) {
var voteIDs []string
// Vote IDs not in cache, perform DB query!
q := newSelectPollVotes(p.db, pollID)
if _, err := q.Exec(ctx, &voteIDs); // nocollapse
err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, err
}
return voteIDs, nil
})
if err != nil {
return nil, err
}
// Load all votes from IDs via cache loader callbacks.
votes, err := p.state.Caches.GTS.PollVote.LoadIDs("ID",
voteIDs,
func(uncached []string) ([]*gtsmodel.PollVote, error) {
// Preallocate expected length of uncached votes.
votes := make([]*gtsmodel.PollVote, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
if err := p.db.NewSelect().
Model(&votes).
Where("? IN (?)", bun.Ident("id"), bun.In(uncached)).
Scan(ctx); err != nil {
return nil, err
}
return votes, nil
},
)
if err != nil {
return nil, err
}
// Reorder the poll votes by their
// IDs to ensure in correct order.
getID := func(v *gtsmodel.PollVote) string { return v.ID }
util.OrderBy(votes, voteIDs, getID)
if gtscontext.Barebones(ctx) {
// no need to fully populate.
return votes, nil
}
// Populate all loaded votes, removing those we fail to
// populate (removes needing so many nil checks everywhere).
votes = slices.DeleteFunc(votes, func(vote *gtsmodel.PollVote) bool {
if err := p.PopulatePollVote(ctx, vote); err != nil {
log.Errorf(ctx, "error populating vote %s: %v", vote.ID, err)
return true
}
return false
})
return votes, nil
}
func (p *pollDB) PopulatePollVote(ctx context.Context, vote *gtsmodel.PollVote) error {
var (
err error
errs gtserror.MultiError
)
if vote.Account == nil {
// Vote account is not set, fetch from database.
vote.Account, err = p.state.DB.GetAccountByID(
gtscontext.SetBarebones(ctx),
vote.AccountID,
)
if err != nil {
errs.Appendf("error populating vote account: %w", err)
}
}
if vote.Poll == nil {
// Vote poll is not set, fetch from database.
vote.Poll, err = p.GetPollByID(
gtscontext.SetBarebones(ctx),
vote.PollID,
)
if err != nil {
errs.Appendf("error populating vote poll: %w", err)
}
}
return errs.Combine()
}
func (p *pollDB) PutPollVote(ctx context.Context, vote *gtsmodel.PollVote) error {
return p.state.Caches.GTS.PollVote.Store(vote, func() error {
return p.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Try insert vote into database.
if _, err := tx.NewInsert().
Model(vote).
Exec(ctx); err != nil {
return err
}
var poll gtsmodel.Poll
// Select current poll counts from DB,
// taking minimal columns needed to
// increment/decrement votes.
if err := tx.NewSelect().
Model(&poll).
Column("options", "votes", "voters").
Where("? = ?", bun.Ident("id"), vote.PollID).
Scan(ctx); err != nil {
return err
}
// Increment poll votes for choices.
poll.IncrementVotes(vote.Choices)
// Finally, update the poll entry.
_, err := tx.NewUpdate().
Model(&poll).
Column("votes", "voters").
Where("? = ?", bun.Ident("id"), vote.PollID).
Exec(ctx)
return err
})
})
}
func (p *pollDB) DeletePollVotes(ctx context.Context, pollID string) error {
err := p.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Delete all votes in poll.
res, err := tx.NewDelete().
Table("poll_votes").
Where("? = ?", bun.Ident("poll_id"), pollID).
Exec(ctx)
if err != nil {
// irrecoverable
return err
}
ra, err := res.RowsAffected()
if err != nil {
// irrecoverable
return err
}
if ra == 0 {
// No poll votes deleted,
// nothing to update.
return nil
}
// Select current poll counts from DB,
// taking minimal columns needed to
// increment/decrement votes.
var poll gtsmodel.Poll
switch err := tx.NewSelect().
Model(&poll).
Column("options", "votes", "voters").
Where("? = ?", bun.Ident("id"), pollID).
Scan(ctx); {
case err == nil:
// no issue.
case errors.Is(err, db.ErrNoEntries):
// no votes found,
// return here.
return nil
default:
// irrecoverable.
return err
}
// Zero all counts.
poll.ResetVotes()
// Finally, update the poll entry.
_, err = tx.NewUpdate().
Model(&poll).
Column("votes", "voters").
Where("? = ?", bun.Ident("id"), pollID).
Exec(ctx)
return err
})
if err != nil {
return err
}
// Invalidate poll vote and poll entry from caches.
p.state.Caches.GTS.Poll.Invalidate("ID", pollID)
p.state.Caches.GTS.PollVote.Invalidate("PollID", pollID)
p.state.Caches.GTS.PollVoteIDs.Invalidate(pollID)
return nil
}
func (p *pollDB) DeletePollVoteBy(ctx context.Context, pollID string, accountID string) error {
err := p.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Slice should only ever be of length
// 0 or 1; it's a slice of slices only
// because we can't LIMIT deletes to 1.
var choicesSlice [][]int
// Delete vote in poll by account,
// returning the ID + choices of the vote.
if err := tx.NewDelete().
Table("poll_votes").
Where("? = ?", bun.Ident("poll_id"), pollID).
Where("? = ?", bun.Ident("account_id"), accountID).
Returning("?", bun.Ident("choices")).
Scan(ctx, &choicesSlice); err != nil {
// irrecoverable.
return err
}
if len(choicesSlice) != 1 {
// No poll votes by this
// acct on this poll.
return nil
}
// Extract the *actual* choices.
choices := choicesSlice[0]
// Select current poll counts from DB,
// taking minimal columns needed to
// increment/decrement votes.
var poll gtsmodel.Poll
switch err := tx.NewSelect().
Model(&poll).
Column("options", "votes", "voters").
Where("? = ?", bun.Ident("id"), pollID).
Scan(ctx); {
case err == nil:
// no issue.
case errors.Is(err, db.ErrNoEntries):
// no poll found,
// return here.
return nil
default:
// irrecoverable.
return err
}
// Decrement votes for choices.
poll.DecrementVotes(choices)
// Finally, update the poll entry.
_, err := tx.NewUpdate().
Model(&poll).
Column("votes", "voters").
Where("? = ?", bun.Ident("id"), pollID).
Exec(ctx)
return err
})
if err != nil {
return err
}
// Invalidate poll vote and poll entry from caches.
p.state.Caches.GTS.Poll.Invalidate("ID", pollID)
p.state.Caches.GTS.PollVote.Invalidate("PollID,AccountID", pollID, accountID)
p.state.Caches.GTS.PollVoteIDs.Invalidate(pollID)
return nil
}
func (p *pollDB) DeletePollVotesByAccountID(ctx context.Context, accountID string) error {
var pollIDs []string
// Select all polls this account
// has registered a poll vote in.
if err := p.db.NewSelect().
Table("poll_votes").
Column("poll_id").
Where("? = ?", bun.Ident("account_id"), accountID).
Scan(ctx, &pollIDs); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
for _, id := range pollIDs {
// Delete all votes by this account in each of the polls,
// this way ensures that all necessary caches are invalidated.
if err := p.DeletePollVoteBy(ctx, id, accountID); err != nil {
log.Errorf(ctx, "error deleting vote by %s in %s: %v", accountID, id, err)
}
}
return nil
}
// newSelectPollVotes returns a new select query for all rows in the poll_votes table with poll_id = pollID.
func newSelectPollVotes(db *bun.DB, pollID string) *bun.SelectQuery {
return db.NewSelect().
TableExpr("?", bun.Ident("poll_votes")).
ColumnExpr("?", bun.Ident("id")).
Where("? = ?", bun.Ident("poll_id"), pollID).
OrderExpr("? DESC", bun.Ident("id"))
}