diff options
Diffstat (limited to 'models/item/item.go')
| -rw-r--r-- | models/item/item.go | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/models/item/item.go b/models/item/item.go index 3722e90..f960c65 100644 --- a/models/item/item.go +++ b/models/item/item.go @@ -57,8 +57,8 @@ func (i *Item) Print() { func (i *Item) Create() error { res, err := models.DB.Exec(`INSERT INTO - item(title, url, description, publish_date, feed_id) - VALUES(?, ?, ?, ?, ?)`, i.Title, i.Url, i.Description, i.PublishDate, i.FeedId) + item(title, url, description, publish_date, feed_id, read_state, starred) + VALUES(?, ?, ?, ?, ?, ?, ?)`, i.Title, i.Url, i.Description, i.PublishDate, i.FeedId, i.ReadState, i.Starred) if err != nil { vlog.Printf("Error on item.Create\n%v\n%v\n", i.Url, err) return err @@ -230,6 +230,32 @@ func Filter(max_id int64, feed_id int64, category string, unread_only bool, star return items, nil } +// Purge deletes items older than the specified number of days. +// By default it only deletes read items. +// If allItems is true, it also deletes unread items. +// Starred items are NEVER deleted. +func Purge(days int, allItems bool) (int64, error) { + query := `DELETE FROM item WHERE datetime(publish_date) < datetime('now', ?) AND starred == 0` + if !allItems { + query = query + ` AND read_state == 1` + } + vlog.Printf("Purge query: %s with param %s\n", query, fmt.Sprintf("-%d days", days)) + res, err := models.DB.Exec(query, fmt.Sprintf("-%d days", days)) + if err != nil { + return 0, err + } + affected, _ := res.RowsAffected() + vlog.Printf("Purge affected rows: %d\n", affected) + + // Cleanup FTS table - SQLite FTS4 doesn't automatically cleanup content table rows + // if we are using "content=item" (which we are). + // Actually we have triggers, so it should be fine. + // But VACUUM is good to reclaim space. + // _, _ = models.DB.Exec("VACUUM") + + return affected, nil +} + func (i *Item) CleanHeaderImage() { // TODO: blacklist of bad imgs if i.HeaderImage == "https://s0.wp.com/i/blank.jpg" { |
