-
Notifications
You must be signed in to change notification settings - Fork 0
/
items.go
426 lines (354 loc) · 10 KB
/
items.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
package database
import (
"errors"
"strings"
"time"
"github.com/awused/aw-rss/internal/structs"
log "github.com/sirupsen/logrus"
)
// MutateItem applies `fn` to one item from the DB and returns it
func (d *Database) MutateItem(
id int64,
fn func(*structs.Item) structs.EntityUpdate) (*structs.Item, error) {
d.lock.Lock()
defer d.lock.Unlock()
if err := d.checkClosed(); err != nil {
log.Error(err)
return nil, err
}
tx, err := d.db.Begin()
if err != nil {
log.Error(err)
return nil, err
}
defer tx.Rollback()
it, err := getItem(tx, id)
if err != nil {
log.Error(err)
return nil, err
}
update := fn(it)
if update.Noop() {
err = tx.Commit()
return it, err
}
err = updateEntity(tx, update)
if err != nil {
log.Error(err)
return nil, err
}
newIt, err := getItem(tx, id)
if err != nil {
log.Error(err)
return nil, err
}
err = tx.Commit()
if err != nil {
log.Error(err)
return nil, err
}
return newIt, nil
}
func getItem(dot dbOrTx, id int64) (*structs.Item, error) {
sql := entityGetSQL("items", structs.ItemSelectColumns)
return structs.ScanItem(dot.QueryRow(sql, id))
}
func getItems(dot dbOrTx, ids []int64) ([]*structs.Item, error) {
sql := entityBatchGetSQL("items", structs.ItemSelectColumns, len(ids))
// Ugly
binds := make([]interface{}, len(ids), len(ids))
for i, v := range ids {
binds[i] = v
}
rows, err := dot.Query(sql, binds...)
if err != nil {
return nil, err
}
return structs.ScanItems(rows)
}
// InsertItems inserts new items into the database if they aren't present.
// The order of the items matters, as item IDs are used to break timestamp ties
// when sorting on the frontend.
func (d *Database) InsertItems(items []*structs.Item) error {
if len(items) == 0 {
log.Trace("InsertItems() called with empty list")
return nil
}
d.lock.Lock()
defer d.lock.Unlock()
if err := d.checkClosed(); err != nil {
log.Error(err)
return err
}
insertColumns := structs.ItemInsertColumns
insertPlaceholders := structs.ItemInsertPlaceholders
if d.conf.Dedupe {
// Allow duplicates within the same feed
// The unhandled corner case is where multiple feeds have 'legitimate'
// duplicates, but I don't think it's worth the effort to handle.
insertColumns += ", read"
insertPlaceholders += `,
(SELECT EXISTS (SELECT url FROM items WHERE url = ? AND feedid != ?))`
}
sql := insertSQL("items", insertColumns, insertPlaceholders)
binds := []interface{}{}
for _, i := range items {
// TODO -- ON CONFLICT UPDATE if we want to handle updates
log.Tracef("Attempting to insert [%s]", i)
insertValues := i.InsertValues()
if d.conf.Dedupe {
insertValues = append(insertValues, i.URL(), i.FeedID())
}
binds = append(binds, insertValues...)
}
log.Debugf("Inserting %d potentially new items", len(items))
tx, err := d.db.Begin()
if err != nil {
log.Error(err)
return err
}
defer tx.Rollback()
_, err = tx.Exec(strings.Repeat(sql, len(items)), binds...)
if err != nil {
log.Error(err)
return err
}
err = tx.Commit()
if err != nil {
log.Error(err)
return err
}
return nil
}
// GetItemsRequest is a request for items matching some constraints
// Items from disabled feeds will not be fetched unless they're specified in
// FeedIDs
// If both FeedIDs and CategoryID are absent, items from every enabled feed
// will be fetched
type GetItemsRequest struct {
// ID of the category, if any
CategoryID *int64 `json:"categoryId"`
// IDs of the Feeds, if any
FeedIDs []int64 `json:"feedIds"`
// Include feeds specified by FeedIDs in the response
// Does not work for categories
IncludeFeeds bool `json:"includeFeeds"`
// If true fetch all unread items
Unread bool `json:"unread"`
// Note that they will be filtered by timestamp, but ordered by ID.
ReadBeforeCount int `json:"readBeforeCount"`
// Fetch _at least_ ReadBeforeCount items before this timestamp (exclusive)
// Guaranteed that all existing read items between ReadBefore and the minimum
// timestamp in the response are fetched.
ReadBefore *time.Time `json:"readBefore"`
// Fetch _all_ read items after this timestamp (inclusive)
// This is used when backfilling on the frontend, but only in the rare
// case where a category is open and a feed is added to it or re-enabled.
ReadAfter *time.Time `json:"readAfter"`
}
// GetItemsResponse is used to fulfill the GetItemsRequest
type GetItemsResponse struct {
Items []*structs.Item `json:"items"`
Feeds []*structs.Feed `json:"feeds,omitempty"`
// Don't include the categories, if any
// The frontend either has it or will on refresh
}
// GetItems returns the Items needed to fulfill the GetItemsRequest
func (d *Database) GetItems(
req GetItemsRequest) (*GetItemsResponse, error) {
var err error
resp := GetItemsResponse{
Items: []*structs.Item{}}
if !req.Unread && req.ReadAfter == nil && req.ReadBefore == nil {
log.Info("GetItems() called with empty request")
return &resp, nil
}
if len(req.FeedIDs) != 0 && req.CategoryID != nil {
return nil, errors.New("Can't call GetItems for both feeds and a category")
}
if req.Unread && len(req.FeedIDs) == 0 {
return nil, errors.New("Can't request unread except by feeds")
}
if (req.ReadAfter != nil) && (req.ReadBefore != nil) {
return nil, errors.New("Must not specify both ReadBefore and ReadAfter")
}
if req.ReadAfter != nil {
*req.ReadAfter = req.ReadAfter.UTC().Truncate(time.Second)
}
if req.ReadBefore != nil {
*req.ReadBefore = req.ReadBefore.UTC().Truncate(time.Second)
}
d.lock.RLock()
defer d.lock.RUnlock()
if err := d.checkClosed(); err != nil {
log.Error(err)
return nil, err
}
if !req.IncludeFeeds {
resp.Items, err = getItemsFor(d.db, req)
if err != nil {
log.Error(err)
return nil, err
}
return &resp, nil
}
tx, err := d.db.Begin()
if err != nil {
log.Error(err)
return nil, err
}
defer tx.Rollback()
resp.Items, err = getItemsFor(tx, req)
if err != nil {
log.Error(err)
return nil, err
}
if req.IncludeFeeds && req.FeedIDs != nil {
resp.Feeds, err = getFeeds(tx, req.FeedIDs)
if err != nil {
log.Error(err)
tx.Rollback()
return nil, err
}
}
err = tx.Commit()
if err != nil {
log.Error(err)
return nil, err
}
return &resp, nil
}
func getItemsFor(dot dbOrTx, req GetItemsRequest) ([]*structs.Item, error) {
selectSQL := `SELECT ` + structs.ItemSelectColumns
sql := `
FROM
feeds CROSS JOIN items on items.feedid = feeds.id
WHERE `
binds := []interface{}{}
if req.CategoryID != nil {
sql += ` feeds.categoryid = ? `
binds = append(binds, req.CategoryID)
} else if len(req.FeedIDs) != 0 {
placeholders := make([]string, len(req.FeedIDs), len(req.FeedIDs))
for i, v := range req.FeedIDs {
placeholders[i] = "?"
binds = append(binds, v)
}
sql += `
feeds.id IN (` + strings.Join(placeholders, ", ") + `) `
} else {
sql += ` feeds.disabled = 0 `
}
if req.ReadAfter != nil && req.ReadBefore != nil {
// Pretty sure this is unnecessary
return nil, errors.New("unimplemented")
} else if req.ReadAfter != nil {
where := "items.read = 1 AND items.timestamp >= ?"
binds = append(binds, req.ReadAfter)
if req.Unread {
where = "((" + where + ") OR items.read = 0)"
}
sql += ` AND ` + where + ` `
} else if req.ReadBefore != nil {
count := req.ReadBeforeCount
if count <= 0 {
count = 100
}
// Timestamps are truncacted to the second for consistency, so it's
// entirely possible to have many at the same timestamp, especially
// from feeds that do not provide timestamps.
// Ensure there are no gaps by fetching 'count' items, getting the
// timestamp, then using that.
// This _can_ be done in one sql statement, but it's unwieldly
timestampSQL := `
SELECT MIN(timestamp)
FROM (SELECT items.timestamp ` + sql + `
AND items.read = 1 AND items.timestamp < ?
ORDER BY items.timestamp DESC
LIMIT ?)`
timestampBinds := append(binds, req.ReadBefore, count)
row := dot.QueryRow(timestampSQL, timestampBinds...)
var b []uint8
err := row.Scan(&b)
if err != nil {
log.Error(err)
// Couldn't get a minimum timestamp -> there are no read items
// TODO -- If we need to handle ReadBefore and Unread, change this
return []*structs.Item{}, nil
}
minTimestamp := string(b)
where := "items.read = 1 AND items.timestamp < ? AND items.timestamp >= ?"
binds = append(binds, req.ReadBefore, minTimestamp)
if req.Unread {
// This shouldn't be necessary
return nil, errors.New("unimplemented")
}
sql += ` AND ` + where
} else {
sql += ` AND items.read = 0 `
}
sql += ` ORDER BY items.id ASC;`
rows, err := dot.Query(selectSQL+sql, binds...)
if err != nil {
return nil, err
}
return structs.ScanItems(rows)
}
// MarkItemsReadByFeed marks all items up to maxID as read for the feed
func (d *Database) MarkItemsReadByFeed(feedID int64, maxID int64) (
[]*structs.Item, error) {
d.lock.Lock()
defer d.lock.Unlock()
if err := d.checkClosed(); err != nil {
log.Error(err)
return nil, err
}
tx, err := d.db.Begin()
if err != nil {
log.Error(err)
return nil, err
}
defer tx.Rollback()
// It's not really optimal to read the full items here, but it's safer
// to use the existing item mutation functions
selectSQL := `
SELECT ` + structs.ItemSelectColumns + `
FROM items
WHERE feedid = ? AND read = 0 AND id <= ?;`
rows, err := tx.Query(selectSQL, feedID, maxID)
if err != nil {
log.Error(err)
return nil, err
}
items, err := structs.ScanItems(rows)
if err != nil {
log.Error(err)
return nil, err
}
updateSQL := []string{}
updateBinds := []interface{}{}
ids := []int64{}
for _, it := range items {
ids = append(ids, it.ID())
update := structs.ItemSetRead(true)(it)
sql, binds := update.Get()
updateSQL = append(updateSQL, sql)
updateBinds = append(updateBinds, binds...)
}
_, err = tx.Exec(strings.Join(updateSQL, "\n"), updateBinds...)
if err != nil {
log.Error(err)
return nil, err
}
updatedItems, err := getItems(tx, ids)
if err != nil {
log.Error(err)
return nil, err
}
err = tx.Commit()
if err != nil {
log.Error(err)
return nil, err
}
return updatedItems, nil
}