/
article.go
731 lines (568 loc) · 17.1 KB
/
article.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
package sql
import (
"database/sql"
"fmt"
"strings"
"text/template"
"time"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"github.com/urandom/readeef/content"
"github.com/urandom/readeef/content/repo/sql/db"
"github.com/urandom/readeef/log"
"github.com/urandom/readeef/pool"
)
var (
getArticlesUserlessTemplate *template.Template
getArticlesTemplate *template.Template
getArticleIDsTemplate *template.Template
articleCountTemplate *template.Template
readStateInsertTemplate *template.Template
readStateDeleteTemplate *template.Template
favoriteStateInsertTemplate *template.Template
favoriteStateDeleteTemplate *template.Template
)
type articleRepo struct {
db *db.DB
log log.Log
}
type getArticlesData struct {
Columns string
Join string
Where string
Order string
Limit string
}
const (
userLogin = "user_login"
beforeID = "before_id"
afterID = "after_id"
beforeDate = "before_date"
afterDate = "after_date"
beforeScore = "before_score"
afterScore = "after_score"
idPrefix = "id"
feedIDPRefix = "feed_id"
limit = "limit"
offset = "offset"
filterURLPrefix = "filterURL"
filterTitlePrefix = "filterTitle"
filterIDPrefix = "filterID"
)
// ForUser returns all user articles restricted by the QueryOptions
func (r articleRepo) ForUser(user content.User, opts ...content.QueryOpt) ([]content.Article, error) {
articles := []content.Article{}
if err := user.Validate(); err != nil {
return articles, errors.WithMessage(err, "validating user")
}
o := content.QueryOptions{}
o.Apply(opts)
r.log.Infof("Getting articles for user %s", user)
articles, err := getArticles(user.Login, r.db, r.log, o)
if err != nil {
err = errors.Wrapf(err, "getting articles for user %s", user)
}
return articles, err
}
func (r articleRepo) All(opts ...content.QueryOpt) ([]content.Article, error) {
o := content.QueryOptions{}
o.Apply(opts)
r.log.Infof("Getting all articles")
var err error
if getArticlesUserlessTemplate == nil {
getArticlesUserlessTemplate, err = template.New("userless-articles").
Parse(r.db.SQL().Article.GetUserlessTemplate)
if err != nil {
return []content.Article{}, errors.Wrap(err, "generating get-articles-userless template")
}
}
renderData := getArticlesData{}
if o.IncludeScores {
renderData.Columns += ", asco.score"
}
var args map[string]interface{}
renderData.Join, renderData.Where, renderData.Order, renderData.Limit, args = constructSQLQueryOptions("", o, r.db)
buf := pool.Buffer.Get()
defer pool.Buffer.Put(buf)
if err = getArticlesUserlessTemplate.Execute(buf, renderData); err != nil {
return []content.Article{}, errors.Wrap(err, "executing get-articles-userless template")
}
sql := buf.String()
var articles []content.Article
r.log.Debugf("Articles SQL:\n%s\nArgs:%v\n", sql, args)
if err = r.db.WithNamedStmt(sql, nil, func(stmt *sqlx.NamedStmt) error {
return stmt.Select(&articles, args)
}); err != nil {
err = errors.Wrap(err, "getting articles")
}
return articles, err
}
func (r articleRepo) Count(user content.User, opts ...content.QueryOpt) (int64, error) {
if err := user.Validate(); err != nil {
return 0, errors.WithMessage(err, "validating user")
}
o := content.QueryOptions{}
o.Apply(opts)
r.log.Infof("Getting articles count")
s := r.db.SQL()
var err error
if articleCountTemplate == nil {
articleCountTemplate, err = template.New("article-count-sql").
Parse(s.Article.CountTemplate)
if err != nil {
return 0, errors.Wrap(err, "generating article-count template")
}
}
var login content.Login
var renderData getArticlesData
if user.Login != "" {
renderData.Join += s.Article.CountUserFeedsJoin
login = user.Login
}
if o.FavoriteOnly {
renderData.Join += s.Article.StateFavoriteJoin
}
if o.ReadOnly || o.UnreadOnly {
renderData.Join += s.Article.StateUnreadJoin
}
o.IncludeScores = false
o.UnreadFirst = false
join, where, _, _, args := constructSQLQueryOptions(login, o, r.db)
renderData.Join += join
renderData.Where = where
buf := pool.Buffer.Get()
defer pool.Buffer.Put(buf)
if err := articleCountTemplate.Execute(buf, renderData); err != nil {
return 0, errors.Wrap(err, "executing article-count template")
}
var count int64
r.log.Debugf("Article count SQL:\n%s\nArgs:%v\n", buf.String(), args)
if err = r.db.WithNamedStmt(buf.String(), nil, func(stmt *sqlx.NamedStmt) error {
return stmt.Get(&count, args)
}); err != nil {
return 0, errors.Wrap(err, "getting article count")
}
return count, nil
}
func (r articleRepo) IDs(user content.User, opts ...content.QueryOpt) ([]content.ArticleID, error) {
if err := user.Validate(); err != nil {
return []content.ArticleID{}, errors.WithMessage(err, "validating user")
}
o := content.QueryOptions{}
o.Apply(opts)
r.log.Infof("Getting article ids")
s := r.db.SQL()
var err error
if getArticleIDsTemplate == nil {
getArticleIDsTemplate, err = template.New("article-ids-sql").
Parse(s.Article.GetIDsTemplate)
if err != nil {
return []content.ArticleID{}, errors.Wrap(err, "generating article-ids template")
}
}
var login content.Login
var renderData getArticlesData
if user.Login != "" {
renderData.Join += s.Article.CountUserFeedsJoin
login = user.Login
}
if o.FavoriteOnly {
renderData.Join += s.Article.StateFavoriteJoin
}
if o.ReadOnly || o.UnreadOnly || o.UnreadFirst {
renderData.Join += s.Article.StateUnreadJoin
}
if o.UnreadFirst {
renderData.Columns += ", " + s.Article.StateReadColumn
}
o.IncludeScores = false
join, where, order, limit, args := constructSQLQueryOptions(login, o, r.db)
renderData.Join += join
renderData.Where = where
renderData.Order = order
renderData.Limit = limit
buf := pool.Buffer.Get()
defer pool.Buffer.Put(buf)
if err := getArticleIDsTemplate.Execute(buf, renderData); err != nil {
return []content.ArticleID{}, errors.Wrap(err, "executing article-count template")
}
var ids []content.ArticleID
r.log.Debugf("Article ids SQL:\n%s\nArgs:%v\n", buf.String(), args)
if err = r.db.WithNamedStmt(buf.String(), nil, func(stmt *sqlx.NamedStmt) error {
return stmt.Select(&ids, args)
}); err != nil {
return []content.ArticleID{}, errors.Wrap(err, "getting article ids")
}
return ids, nil
}
func (r articleRepo) Read(
state bool,
user content.User,
opts ...content.QueryOpt,
) error {
return articleStateSet(readState, state, user, r.db, r.log, opts)
}
func (r articleRepo) Favor(
state bool,
user content.User,
opts ...content.QueryOpt,
) error {
return articleStateSet(favoriteState, state, user, r.db, r.log, opts)
}
type staleArgs struct {
InsertDate time.Time `db:"insert_date"`
}
func (r articleRepo) RemoveStaleUnreadRecords() error {
r.log.Infof("Removing stale unread article records")
if err := r.db.WithNamedStmt(
r.db.SQL().Article.DeleteStaleUnreadRecords,
nil,
func(stmt *sqlx.NamedStmt) error {
_, err := stmt.Exec(staleArgs{time.Now().AddDate(0, -1, 0)})
return err
},
); err != nil {
return errors.Wrap(err, "removing stale unread article records")
}
return nil
}
func getArticles(login content.Login, dbo *db.DB, log log.Log, opts content.QueryOptions) ([]content.Article, error) {
var err error
if getArticlesTemplate == nil {
getArticlesTemplate, err = template.New("get-articles-sql").
Parse(dbo.SQL().Article.GetTemplate)
if err != nil {
return []content.Article{}, errors.Wrap(err, "generating get-articles template")
}
}
renderData := getArticlesData{}
var args map[string]interface{}
renderData.Join, renderData.Where, renderData.Order, renderData.Limit, args = constructSQLQueryOptions(login, opts, dbo)
if opts.IncludeScores {
renderData.Columns += ", asco.score"
}
buf := pool.Buffer.Get()
defer pool.Buffer.Put(buf)
if err := getArticlesTemplate.Execute(buf, renderData); err != nil {
return []content.Article{}, errors.Wrap(err, "executing get-articles template")
}
sql := buf.String()
var articles []content.Article
log.Debugf("Articles SQL:\n%s\nArgs:%v\n", sql, args)
if err := dbo.WithNamedStmt(sql, nil, func(stmt *sqlx.NamedStmt) error {
return stmt.Select(&articles, args)
}); err != nil {
return []content.Article{}, errors.Wrap(err, "getting articles")
}
return articles, nil
}
type stateType int
const (
readState stateType = iota
favoriteState stateType = iota
)
func articleStateSet(
stateType stateType,
state bool,
user content.User,
db *db.DB,
log log.Log,
opts []content.QueryOpt,
) error {
if err := instantiateStateTemplates(db.SQL()); err != nil {
return errors.WithMessage(err, "instantiating article state templates")
}
if err := user.Validate(); err != nil {
return errors.WithMessage(err, "validating user")
}
o := content.QueryOptions{}
o.Apply(opts)
var tmpl *template.Template
switch stateType {
case readState:
log.Infof("Setting articles read state")
if state {
tmpl = readStateDeleteTemplate
} else {
tmpl = readStateInsertTemplate
}
case favoriteState:
log.Infof("Setting articles favorite state")
if state {
tmpl = favoriteStateInsertTemplate
} else {
tmpl = favoriteStateDeleteTemplate
}
}
s := db.SQL()
renderData := getArticlesData{}
var args map[string]interface{}
renderData.Join, renderData.Where, _, _, args = constructSQLQueryOptions(user.Login, o, db)
if o.FavoriteOnly {
renderData.Join += s.Article.StateFavoriteJoin
}
if o.ReadOnly || o.UnreadOnly {
renderData.Join += s.Article.StateUnreadJoin
}
buf := pool.Buffer.Get()
defer pool.Buffer.Put(buf)
if err := tmpl.Execute(buf, renderData); err != nil {
return errors.Wrap(err, "executing article state template")
}
log.Debugf("Articles state SQL:\n%s\nArgs:%v\n", buf.String(), args)
if err := db.WithNamedStmt(buf.String(), nil, func(stmt *sqlx.NamedStmt) error {
_, err := stmt.Exec(args)
return err
}); err != nil {
return errors.Wrap(err, "executing article state statement")
}
return nil
}
func constructSQLQueryOptions(
login content.Login,
opts content.QueryOptions,
db *db.DB,
) (string, string, string, string, map[string]interface{}) {
args := map[string]interface{}{}
hasUser := login != ""
var join string
s := db.SQL()
if opts.IncludeScores || opts.BeforeScore > 0 || opts.AfterScore > 0 {
join += s.Article.GetScoreJoin
}
if hasUser {
args[userLogin] = login
if opts.UntaggedOnly {
join += s.Article.GetUntaggedJoin
}
}
whereSlice := []string{}
if hasUser {
if opts.UnreadOnly {
whereSlice = append(whereSlice, "au.article_id IS NOT NULL")
} else if opts.ReadOnly {
whereSlice = append(whereSlice, "au.article_id IS NULL")
}
if opts.UntaggedOnly {
whereSlice = append(whereSlice, "uft.feed_id IS NULL")
}
if opts.FavoriteOnly {
whereSlice = append(whereSlice, "af.article_id IS NOT NULL")
}
}
if clause := createRowValueClause(opts.BeforeID, opts.BeforeDate, opts.BeforeScore, "before", args); clause != "" {
whereSlice = append(whereSlice, clause)
}
if clause := createRowValueClause(opts.AfterID, opts.AfterDate, opts.AfterScore, "after", args); clause != "" {
whereSlice = append(whereSlice, clause)
}
if len(opts.IDs) > 0 {
whereSlice = append(whereSlice, db.WhereMultipleORs("a.id", idPrefix, len(opts.IDs), true))
for i := range opts.IDs {
args[fmt.Sprintf("%s%d", idPrefix, i)] = opts.IDs[i]
}
}
if opts.IncludeScores {
whereSlice = append(whereSlice, "asco.score > 0")
}
feedIDset := make(map[content.FeedID]struct{})
if len(opts.FeedIDs) > 0 {
whereSlice = append(whereSlice, db.WhereMultipleORs("a.feed_id", feedIDPRefix, len(opts.FeedIDs), true))
for i := range opts.FeedIDs {
args[fmt.Sprintf("%s%d", feedIDPRefix, i)] = opts.FeedIDs[i]
feedIDset[opts.FeedIDs[i]] = struct{}{}
}
}
for i, f := range opts.Filters {
if !f.Valid() {
continue
}
parts := make([]string, 0, 3)
if f.URLTerm != "" {
sign := "LIKE"
if f.InverseURL {
sign = "NOT LIKE"
}
args[fmt.Sprintf("%s%d", filterURLPrefix, i)] = fmt.Sprintf("%%%s%%", f.URLTerm)
parts = append(parts,
fmt.Sprintf("LOWER(a.link) %s :%s%d", sign, filterURLPrefix, i),
)
}
if f.TitleTerm != "" {
sign := "LIKE"
if f.InverseTitle {
sign = "NOT LIKE"
}
args[fmt.Sprintf("%s%d", filterTitlePrefix, i)] = fmt.Sprintf("%%%s%%", f.TitleTerm)
parts = append(parts,
fmt.Sprintf("LOWER(a.title) %s :%s%d", sign, filterTitlePrefix, i),
)
}
ids := f.FeedIDs
if len(opts.FeedIDs) > 0 && len(ids) > 0 {
ids = make([]content.FeedID, 0, len(opts.FeedIDs))
for _, id := range f.FeedIDs {
if _, ok := feedIDset[id]; ok {
ids = append(ids, id)
}
}
if len(ids) == 0 {
continue
}
}
if len(ids) > 0 {
parts = append(parts,
db.WhereMultipleORs(
"a.feed_id",
fmt.Sprintf("%s%dx", filterIDPrefix, i),
len(ids),
!f.InverseFeeds,
),
)
for j := range ids {
args[fmt.Sprintf("%s%dx%d", filterIDPrefix, i, j)] = ids[j]
}
}
whereSlice = append(whereSlice, "NOT ("+strings.Join(parts, " AND ")+")")
}
var where string
if len(whereSlice) > 0 {
where = "WHERE " + strings.Join(whereSlice, " AND ")
}
sortingField := opts.SortField
sortingOrder := opts.SortOrder
fields := []string{}
if opts.IncludeScores && opts.HighScoredFirst {
field := "asco.score"
if sortingOrder == content.DescendingOrder {
field += " DESC"
}
fields = append(fields, field)
}
if hasUser {
if opts.UnreadFirst {
fields = append(fields, "read")
}
}
switch sortingField {
case content.SortByID:
fields = append(fields, "a.id")
case content.SortByDate:
fields = append(fields, "a.date")
}
var order string
if len(fields) > 0 {
order = " ORDER BY " + strings.Join(fields, ", ")
if sortingOrder == content.DescendingOrder {
order += " DESC"
}
}
var paging string
if opts.Limit > 0 {
if opts.Offset > 0 {
paging = " LIMIT :limit OFFSET :offset"
args[limit] = opts.Limit
args[offset] = opts.Offset
} else {
paging = " LIMIT :limit"
args[limit] = opts.Limit
}
}
return join, where, order, paging, args
}
func updateArticle(a content.Article, tx *sqlx.Tx, db *db.DB, log log.Log) (content.Article, error) {
if err := a.Validate(); err != nil && a.ID != 0 {
return content.Article{}, errors.WithMessage(err, "validating article")
}
log.Infof("Updating article %s\n", a)
s := db.SQL()
db.WithNamedStmt(s.Article.Update, tx, func(stmt *sqlx.NamedStmt) error {
res, err := stmt.Exec(a)
if err != nil {
return errors.Wrap(err, "executing article update statement")
}
if num, err := res.RowsAffected(); err != nil && err == sql.ErrNoRows || num == 0 {
log.Infof("Creating article %s\n", a)
id, err := db.CreateWithID(tx, s.Article.Create, a)
if err != nil {
return errors.Wrap(err, "creating article")
}
a.ID = content.ArticleID(id)
a.IsNew = true
}
return nil
})
return a, nil
}
func instantiateStateTemplates(s db.SqlStmts) error {
var err error
if readStateInsertTemplate == nil {
readStateInsertTemplate, err = template.New("read-state-insert-sql").
Parse(s.Article.ReadStateInsertTemplate)
if err != nil {
return errors.Wrap(err, "generating read-state-insert template")
}
}
if readStateDeleteTemplate == nil {
readStateDeleteTemplate, err = template.New("read-state-delete-sql").
Parse(s.Article.ReadStateDeleteTemplate)
if err != nil {
return errors.Wrap(err, "generating read-state-delete template")
}
}
if favoriteStateInsertTemplate == nil {
favoriteStateInsertTemplate, err = template.New("favorite-state-insert-sql").
Parse(s.Article.FavoriteStateInsertTemplate)
if err != nil {
return errors.Wrap(err, "generating favorite-state-insert template")
}
}
if favoriteStateDeleteTemplate == nil {
favoriteStateDeleteTemplate, err = template.New("favorite-state-delete-sql").
Parse(s.Article.FavoriteStateDeleteTemplate)
if err != nil {
return errors.Wrap(err, "generating favorite-state-delete template")
}
}
return nil
}
func createRowValueClause(id content.ArticleID, date time.Time, score int64, prefix string, args map[string]interface{}) string {
op := "<"
if prefix == "after" {
op = ">"
}
if id > 0 && !date.IsZero() && score > 0 {
args[prefix+"_id"] = id
args[prefix+"_date"] = date
args[prefix+"_score"] = score
return fmt.Sprintf("(a.id, a.date, asco.score) %s (:%s_id, :%s_date, :%s_score)", op, prefix, prefix, prefix)
}
if id > 0 && !date.IsZero() {
args[prefix+"_id"] = id
args[prefix+"_date"] = date
return fmt.Sprintf("(a.id, a.date) %s (:%s_id, :%s_date)", op, prefix, prefix)
}
if id > 0 && score > 0 {
args[prefix+"_id"] = id
args[prefix+"_score"] = score
return fmt.Sprintf("(a.id.date, asco.score) %s (:%s_id, :%s_score)", op, prefix, prefix)
}
if !date.IsZero() && score > 0 {
args[prefix+"_date"] = date
args[prefix+"_score"] = score
return fmt.Sprintf("(a.date, asco.score) %s (:%s_date, :%s_score)", op, prefix, prefix)
}
if id > 0 {
args[prefix+"_id"] = id
return fmt.Sprintf("a.id %s :%s_id", op, prefix)
}
if !date.IsZero() {
args[prefix+"_date"] = date
return fmt.Sprintf("a.date %s :%s_date", op, prefix)
}
if score > 0 {
args[prefix+"_score"] = score
return fmt.Sprintf("asco.score %s :%s_score", op, prefix)
}
return ""
}