1
0
Fork 0
miniflux/storage/entry.go

518 lines
14 KiB
Go
Raw Normal View History

2017-11-20 00:10:04 -05:00
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
2018-08-25 00:51:50 -04:00
package storage // import "miniflux.app/storage"
2017-11-20 00:10:04 -05:00
import (
"database/sql"
2017-11-20 00:10:04 -05:00
"errors"
"fmt"
2017-11-21 18:46:59 -05:00
"time"
"miniflux.app/crypto"
2018-08-25 00:51:50 -04:00
"miniflux.app/logger"
"miniflux.app/model"
2017-11-20 00:10:04 -05:00
"github.com/lib/pq"
)
2020-09-27 19:01:06 -04:00
// CountAllEntries returns the number of entries for each status in the database.
func (s *Storage) CountAllEntries() map[string]int64 {
rows, err := s.db.Query(`SELECT status, count(*) FROM entries GROUP BY status`)
if err != nil {
return nil
}
defer rows.Close()
results := make(map[string]int64)
2021-01-18 16:22:09 -05:00
results[model.EntryStatusUnread] = 0
results[model.EntryStatusRead] = 0
results[model.EntryStatusRemoved] = 0
2020-09-27 19:01:06 -04:00
for rows.Next() {
var status string
var count int64
if err := rows.Scan(&status, &count); err != nil {
continue
}
results[status] = count
}
2021-01-18 16:22:09 -05:00
results["total"] = results[model.EntryStatusUnread] + results[model.EntryStatusRead] + results[model.EntryStatusRemoved]
2020-09-27 19:01:06 -04:00
return results
}
// CountUnreadEntries returns the number of unread entries.
func (s *Storage) CountUnreadEntries(userID int64) int {
builder := s.NewEntryQueryBuilder(userID)
builder.WithStatus(model.EntryStatusUnread)
builder.WithGloballyVisible()
n, err := builder.CountEntries()
if err != nil {
2019-10-30 01:48:07 -04:00
logger.Error(`store: unable to count unread entries for user #%d: %v`, userID, err)
return 0
}
return n
}
2017-12-28 22:20:14 -05:00
// NewEntryQueryBuilder returns a new EntryQueryBuilder
func (s *Storage) NewEntryQueryBuilder(userID int64) *EntryQueryBuilder {
return NewEntryQueryBuilder(s, userID)
2017-11-20 00:10:04 -05:00
}
// UpdateEntryContent updates entry content.
func (s *Storage) UpdateEntryContent(entry *model.Entry) error {
tx, err := s.db.Begin()
if err != nil {
return err
}
2019-10-30 01:48:07 -04:00
query := `
UPDATE
entries
SET
content=$1, reading_time=$2
2019-10-30 01:48:07 -04:00
WHERE
id=$3 AND user_id=$4
2019-10-30 01:48:07 -04:00
`
_, err = tx.Exec(query, entry.Content, entry.ReadingTime, entry.ID, entry.UserID)
if err != nil {
tx.Rollback()
2019-10-30 01:48:07 -04:00
return fmt.Errorf(`store: unable to update content of entry #%d: %v`, entry.ID, err)
}
2019-10-30 01:48:07 -04:00
query = `
UPDATE
entries
SET
document_vectors = setweight(to_tsvector(left(coalesce(title, ''), 500000)), 'A') || setweight(to_tsvector(left(coalesce(content, ''), 500000)), 'B')
2019-10-30 01:48:07 -04:00
WHERE
id=$1 AND user_id=$2
`
_, err = tx.Exec(query, entry.ID, entry.UserID)
if err != nil {
tx.Rollback()
2019-10-30 01:48:07 -04:00
return fmt.Errorf(`store: unable to update content of entry #%d: %v`, entry.ID, err)
}
return tx.Commit()
}
2017-12-24 21:04:34 -05:00
// createEntry add a new entry.
func (s *Storage) createEntry(tx *sql.Tx, entry *model.Entry) error {
2017-11-20 00:10:04 -05:00
query := `
INSERT INTO entries
(
title,
hash,
url,
comments_url,
published_at,
content,
author,
user_id,
feed_id,
reading_time,
changed_at,
document_vectors
)
2017-11-20 00:10:04 -05:00
VALUES
(
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8,
$9,
$10,
now(),
setweight(to_tsvector(left(coalesce($1, ''), 500000)), 'A') || setweight(to_tsvector(left(coalesce($6, ''), 500000)), 'B')
)
2019-10-30 01:48:07 -04:00
RETURNING
id, status
2017-11-20 00:10:04 -05:00
`
err := tx.QueryRow(
2017-11-20 00:10:04 -05:00
query,
entry.Title,
entry.Hash,
entry.URL,
2018-04-07 16:50:45 -04:00
entry.CommentsURL,
2017-11-20 00:10:04 -05:00
entry.Date,
entry.Content,
entry.Author,
entry.UserID,
entry.FeedID,
entry.ReadingTime,
).Scan(&entry.ID, &entry.Status)
2017-11-20 00:10:04 -05:00
if err != nil {
2019-10-30 01:48:07 -04:00
return fmt.Errorf(`store: unable to create entry %q (feed #%d): %v`, entry.URL, entry.FeedID, err)
2017-11-20 00:10:04 -05:00
}
for i := 0; i < len(entry.Enclosures); i++ {
entry.Enclosures[i].EntryID = entry.ID
entry.Enclosures[i].UserID = entry.UserID
err := s.createEnclosure(tx, entry.Enclosures[i])
2017-11-20 00:10:04 -05:00
if err != nil {
return err
}
}
return nil
}
// updateEntry updates an entry when a feed is refreshed.
// Note: we do not update the published date because some feeds do not contains any date,
// it default to time.Now() which could change the order of items on the history page.
func (s *Storage) updateEntry(tx *sql.Tx, entry *model.Entry) error {
2017-11-20 00:10:04 -05:00
query := `
2019-10-30 01:48:07 -04:00
UPDATE
entries
SET
title=$1,
url=$2,
comments_url=$3,
content=$4,
author=$5,
reading_time=$6,
document_vectors = setweight(to_tsvector(left(coalesce($1, ''), 500000)), 'A') || setweight(to_tsvector(left(coalesce($4, ''), 500000)), 'B')
2019-10-30 01:48:07 -04:00
WHERE
user_id=$7 AND feed_id=$8 AND hash=$9
2019-10-30 01:48:07 -04:00
RETURNING
id
2017-11-20 00:10:04 -05:00
`
err := tx.QueryRow(
2017-11-20 00:10:04 -05:00
query,
entry.Title,
entry.URL,
2018-04-07 16:50:45 -04:00
entry.CommentsURL,
2017-11-20 00:10:04 -05:00
entry.Content,
entry.Author,
entry.ReadingTime,
2017-11-20 00:10:04 -05:00
entry.UserID,
entry.FeedID,
entry.Hash,
).Scan(&entry.ID)
if err != nil {
2019-10-30 01:48:07 -04:00
return fmt.Errorf(`store: unable to update entry %q: %v`, entry.URL, err)
}
for _, enclosure := range entry.Enclosures {
enclosure.UserID = entry.UserID
enclosure.EntryID = entry.ID
}
2017-11-20 00:10:04 -05:00
return s.updateEnclosures(tx, entry.UserID, entry.ID, entry.Enclosures)
2017-11-20 00:10:04 -05:00
}
2017-12-24 21:04:34 -05:00
// entryExists checks if an entry already exists based on its hash when refreshing a feed.
func (s *Storage) entryExists(tx *sql.Tx, entry *model.Entry) bool {
var result bool
tx.QueryRow(
`SELECT true FROM entries WHERE user_id=$1 AND feed_id=$2 AND hash=$3`,
entry.UserID,
entry.FeedID,
entry.Hash,
).Scan(&result)
return result
2017-11-20 00:10:04 -05:00
}
// GetReadTime fetches the read time of an entry based on its hash, and the feed id and user id from the feed.
// It's intended to be used on entries objects created by parsing a feed as they don't contain much information.
// The feed param helps to scope the search to a specific user and feed in order to avoid hash clashes.
func (s *Storage) GetReadTime(entry *model.Entry, feed *model.Feed) int {
var result int
s.db.QueryRow(
`SELECT reading_time FROM entries WHERE user_id=$1 AND feed_id=$2 AND hash=$3`,
feed.UserID,
feed.ID,
entry.Hash,
).Scan(&result)
return result
}
// cleanupEntries deletes from the database entries marked as "removed" and not visible anymore in the feed.
func (s *Storage) cleanupEntries(feedID int64, entryHashes []string) error {
query := `
2019-10-30 01:48:07 -04:00
DELETE FROM
entries
WHERE
feed_id=$1
AND
id IN (SELECT id FROM entries WHERE feed_id=$2 AND status=$3 AND NOT (hash=ANY($4)))
`
if _, err := s.db.Exec(query, feedID, feedID, model.EntryStatusRemoved, pq.Array(entryHashes)); err != nil {
2019-10-30 01:48:07 -04:00
return fmt.Errorf(`store: unable to cleanup entries: %v`, err)
}
return nil
}
// RefreshFeedEntries updates feed entries while refreshing a feed.
func (s *Storage) RefreshFeedEntries(userID, feedID int64, entries model.Entries, updateExistingEntries bool) (err error) {
2017-11-21 19:33:36 -05:00
var entryHashes []string
2017-11-20 00:10:04 -05:00
for _, entry := range entries {
entry.UserID = userID
entry.FeedID = feedID
tx, err := s.db.Begin()
if err != nil {
return fmt.Errorf(`store: unable to start transaction: %v`, err)
}
if s.entryExists(tx, entry) {
if updateExistingEntries {
err = s.updateEntry(tx, entry)
}
2017-11-20 00:10:04 -05:00
} else {
err = s.createEntry(tx, entry)
2017-11-20 00:10:04 -05:00
}
if err != nil {
tx.Rollback()
2017-11-20 00:10:04 -05:00
return err
}
2017-11-21 19:33:36 -05:00
if err := tx.Commit(); err != nil {
return fmt.Errorf(`store: unable to commit transaction: %v`, err)
}
2017-11-21 19:33:36 -05:00
entryHashes = append(entryHashes, entry.Hash)
}
go func() {
if err := s.cleanupEntries(feedID, entryHashes); err != nil {
logger.Error(`store: feed #%d: %v`, feedID, err)
}
}()
2017-11-21 19:33:36 -05:00
return nil
}
2020-09-12 23:04:06 -04:00
// ArchiveEntries changes the status of entries to "removed" after the given number of days.
func (s *Storage) ArchiveEntries(status string, days, limit int) (int64, error) {
if days < 0 || limit <= 0 {
2020-09-12 23:04:06 -04:00
return 0, nil
}
2019-10-30 01:48:07 -04:00
query := `
UPDATE
entries
SET
2020-09-12 23:04:06 -04:00
status='removed'
2019-10-30 01:48:07 -04:00
WHERE
id=ANY(SELECT id FROM entries WHERE status=$1 AND starred is false AND share_code='' AND created_at < now () - '%d days'::interval ORDER BY created_at ASC LIMIT %d)
2019-10-30 01:48:07 -04:00
`
2020-09-12 23:04:06 -04:00
result, err := s.db.Exec(fmt.Sprintf(query, days, limit), status)
2020-09-12 23:04:06 -04:00
if err != nil {
return 0, fmt.Errorf(`store: unable to archive %s entries: %v`, status, err)
}
2020-09-12 23:04:06 -04:00
count, err := result.RowsAffected()
if err != nil {
return 0, fmt.Errorf(`store: unable to get the number of rows affected: %v`, err)
}
return count, nil
}
2017-11-21 18:46:59 -05:00
// SetEntriesStatus update the status of the given list of entries.
2017-11-20 00:10:04 -05:00
func (s *Storage) SetEntriesStatus(userID int64, entryIDs []int64, status string) error {
query := `UPDATE entries SET status=$1, changed_at=now() WHERE user_id=$2 AND id=ANY($3)`
2017-11-20 00:10:04 -05:00
result, err := s.db.Exec(query, status, userID, pq.Array(entryIDs))
if err != nil {
2019-10-30 01:48:07 -04:00
return fmt.Errorf(`store: unable to update entries statuses %v: %v`, entryIDs, err)
2017-11-20 00:10:04 -05:00
}
count, err := result.RowsAffected()
if err != nil {
2019-10-30 01:48:07 -04:00
return fmt.Errorf(`store: unable to update these entries %v: %v`, entryIDs, err)
2017-11-20 00:10:04 -05:00
}
if count == 0 {
2019-10-30 01:48:07 -04:00
return errors.New(`store: nothing has been updated`)
2017-11-21 18:46:59 -05:00
}
return nil
}
func (s *Storage) SetEntriesStatusCount(userID int64, entryIDs []int64, status string) (int, error) {
if err := s.SetEntriesStatus(userID, entryIDs, status); err != nil {
return 0, err
}
query := `
SELECT count(*)
FROM entries e
JOIN feeds f ON (f.id = e.feed_id)
JOIN categories c ON (c.id = f.category_id)
WHERE e.user_id = $1
AND e.id = ANY($2)
AND NOT f.hide_globally
AND NOT c.hide_globally
`
row := s.db.QueryRow(query, userID, pq.Array(entryIDs))
visible := 0
if err := row.Scan(&visible); err != nil {
return 0, fmt.Errorf(`store: unable to query entries visibility %v: %v`, entryIDs, err)
}
return visible, nil
}
2017-12-22 14:33:01 -05:00
// ToggleBookmark toggles entry bookmark value.
func (s *Storage) ToggleBookmark(userID int64, entryID int64) error {
query := `UPDATE entries SET starred = NOT starred, changed_at=now() WHERE user_id=$1 AND id=$2`
2017-12-24 21:04:34 -05:00
result, err := s.db.Exec(query, userID, entryID)
2017-12-22 14:33:01 -05:00
if err != nil {
2019-10-30 01:48:07 -04:00
return fmt.Errorf(`store: unable to toggle bookmark flag for entry #%d: %v`, entryID, err)
2017-12-24 21:04:34 -05:00
}
count, err := result.RowsAffected()
if err != nil {
2020-01-29 17:21:48 -05:00
return fmt.Errorf(`store: unable to toggle bookmark flag for entry #%d: %v`, entryID, err)
2017-12-24 21:04:34 -05:00
}
if count == 0 {
2019-10-30 01:48:07 -04:00
return errors.New(`store: nothing has been updated`)
2017-12-22 14:33:01 -05:00
}
return nil
}
2017-11-21 18:46:59 -05:00
// FlushHistory set all entries with the status "read" to "removed".
func (s *Storage) FlushHistory(userID int64) error {
query := `
UPDATE
entries
SET
status=$1,
changed_at=now()
WHERE
2020-07-01 23:11:36 -04:00
user_id=$2 AND status=$3 AND starred is false AND share_code=''
`
2017-11-21 18:46:59 -05:00
_, err := s.db.Exec(query, model.EntryStatusRemoved, userID, model.EntryStatusRead)
if err != nil {
2019-10-30 01:48:07 -04:00
return fmt.Errorf(`store: unable to flush history: %v`, err)
2017-11-20 00:10:04 -05:00
}
return nil
}
2018-01-04 21:11:15 -05:00
// MarkAllAsRead updates all user entries to the read status.
2018-01-04 21:11:15 -05:00
func (s *Storage) MarkAllAsRead(userID int64) error {
query := `UPDATE entries SET status=$1, changed_at=now() WHERE user_id=$2 AND status=$3`
result, err := s.db.Exec(query, model.EntryStatusRead, userID, model.EntryStatusUnread)
2018-01-04 21:11:15 -05:00
if err != nil {
2019-10-30 01:48:07 -04:00
return fmt.Errorf(`store: unable to mark all entries as read: %v`, err)
2018-01-04 21:11:15 -05:00
}
count, _ := result.RowsAffected()
logger.Debug("[Storage:MarkAllAsRead] %d items marked as read", count)
2018-01-04 21:11:15 -05:00
return nil
}
2018-01-19 21:43:27 -05:00
// MarkFeedAsRead updates all feed entries to the read status.
func (s *Storage) MarkFeedAsRead(userID, feedID int64, before time.Time) error {
query := `
2019-10-30 01:48:07 -04:00
UPDATE
entries
SET
status=$1,
changed_at=now()
2019-10-30 01:48:07 -04:00
WHERE
user_id=$2 AND feed_id=$3 AND status=$4 AND published_at < $5
`
result, err := s.db.Exec(query, model.EntryStatusRead, userID, feedID, model.EntryStatusUnread, before)
if err != nil {
2019-10-30 01:48:07 -04:00
return fmt.Errorf(`store: unable to mark feed entries as read: %v`, err)
}
count, _ := result.RowsAffected()
logger.Debug("[Storage:MarkFeedAsRead] %d items marked as read", count)
return nil
}
// MarkCategoryAsRead updates all category entries to the read status.
func (s *Storage) MarkCategoryAsRead(userID, categoryID int64, before time.Time) error {
query := `
2019-10-30 01:48:07 -04:00
UPDATE
entries
SET
status=$1,
changed_at=now()
WHERE
2019-10-30 01:48:07 -04:00
user_id=$2
AND
status=$3
AND
published_at < $4
AND
feed_id IN (SELECT id FROM feeds WHERE user_id=$2 AND category_id=$5)
`
result, err := s.db.Exec(query, model.EntryStatusRead, userID, model.EntryStatusUnread, before, categoryID)
if err != nil {
2019-10-30 01:48:07 -04:00
return fmt.Errorf(`store: unable to mark category entries as read: %v`, err)
}
count, _ := result.RowsAffected()
logger.Debug("[Storage:MarkCategoryAsRead] %d items marked as read", count)
return nil
}
2018-01-19 21:43:27 -05:00
// EntryURLExists returns true if an entry with this URL already exists.
func (s *Storage) EntryURLExists(feedID int64, entryURL string) bool {
2019-10-30 01:48:07 -04:00
var result bool
query := `SELECT true FROM entries WHERE feed_id=$1 AND url=$2`
s.db.QueryRow(query, feedID, entryURL).Scan(&result)
2019-10-30 01:48:07 -04:00
return result
2018-01-19 21:43:27 -05:00
}
// EntryShareCode returns the share code of the provided entry.
// It generates a new one if not already defined.
func (s *Storage) EntryShareCode(userID int64, entryID int64) (shareCode string, err error) {
query := `SELECT share_code FROM entries WHERE user_id=$1 AND id=$2`
err = s.db.QueryRow(query, userID, entryID).Scan(&shareCode)
if err != nil {
err = fmt.Errorf(`store: unable to get share code for entry #%d: %v`, entryID, err)
return
}
if shareCode == "" {
shareCode = crypto.GenerateRandomStringHex(20)
query = `UPDATE entries SET share_code = $1 WHERE user_id=$2 AND id=$3`
_, err = s.db.Exec(query, shareCode, userID, entryID)
if err != nil {
err = fmt.Errorf(`store: unable to set share code for entry #%d: %v`, entryID, err)
return
}
}
return
}
// UnshareEntry removes the share code for the given entry.
func (s *Storage) UnshareEntry(userID int64, entryID int64) (err error) {
query := `UPDATE entries SET share_code='' WHERE user_id=$1 AND id=$2`
_, err = s.db.Exec(query, userID, entryID)
if err != nil {
err = fmt.Errorf(`store: unable to remove share code for entry #%d: %v`, entryID, err)
}
return
}