2017-11-20 06:10:04 +01: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.
|
|
|
|
|
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-11-22 00:46:59 +01:00
|
|
|
"time"
|
|
|
|
|
2017-12-16 03:55:57 +01:00
|
|
|
"github.com/miniflux/miniflux/logger"
|
2017-12-13 06:48:13 +01:00
|
|
|
"github.com/miniflux/miniflux/model"
|
2018-01-03 04:15:08 +01:00
|
|
|
"github.com/miniflux/miniflux/timer"
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
"github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
// CountUnreadEntries returns the number of unread entries.
|
|
|
|
func (s *Storage) CountUnreadEntries(userID int64) int {
|
|
|
|
builder := s.NewEntryQueryBuilder(userID)
|
|
|
|
builder.WithStatus(model.EntryStatusUnread)
|
|
|
|
|
|
|
|
n, err := builder.CountEntries()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("unable to count unread entries: %v", err)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2017-12-29 04:20:14 +01:00
|
|
|
// NewEntryQueryBuilder returns a new EntryQueryBuilder
|
|
|
|
func (s *Storage) NewEntryQueryBuilder(userID int64) *EntryQueryBuilder {
|
|
|
|
return NewEntryQueryBuilder(s, userID)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-12-25 03:04:34 +01:00
|
|
|
// createEntry add a new entry.
|
|
|
|
func (s *Storage) createEntry(entry *model.Entry) error {
|
2017-11-20 06:10:04 +01:00
|
|
|
query := `
|
|
|
|
INSERT INTO entries
|
2018-04-07 22:50:45 +02:00
|
|
|
(title, hash, url, comments_url, published_at, content, author, user_id, feed_id)
|
2017-11-20 06:10:04 +01:00
|
|
|
VALUES
|
2018-04-07 22:50:45 +02:00
|
|
|
($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
2017-11-20 06:10:04 +01:00
|
|
|
RETURNING id
|
|
|
|
`
|
|
|
|
err := s.db.QueryRow(
|
|
|
|
query,
|
|
|
|
entry.Title,
|
|
|
|
entry.Hash,
|
|
|
|
entry.URL,
|
2018-04-07 22:50:45 +02:00
|
|
|
entry.CommentsURL,
|
2017-11-20 06:10:04 +01:00
|
|
|
entry.Date,
|
|
|
|
entry.Content,
|
|
|
|
entry.Author,
|
|
|
|
entry.UserID,
|
|
|
|
entry.FeedID,
|
|
|
|
).Scan(&entry.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
2017-11-22 01:08:43 +01:00
|
|
|
return fmt.Errorf("unable to create entry: %v", err)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
entry.Status = "unread"
|
|
|
|
for i := 0; i < len(entry.Enclosures); i++ {
|
|
|
|
entry.Enclosures[i].EntryID = entry.ID
|
|
|
|
entry.Enclosures[i].UserID = entry.UserID
|
|
|
|
err := s.CreateEnclosure(entry.Enclosures[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-11 04:01:38 +01:00
|
|
|
// UpdateEntryContent updates entry content.
|
|
|
|
func (s *Storage) UpdateEntryContent(entry *model.Entry) error {
|
|
|
|
query := `
|
|
|
|
UPDATE entries SET
|
|
|
|
content=$1
|
|
|
|
WHERE user_id=$2 AND id=$3
|
|
|
|
`
|
|
|
|
|
|
|
|
_, err := s.db.Exec(
|
|
|
|
query,
|
|
|
|
entry.Content,
|
|
|
|
entry.UserID,
|
|
|
|
entry.ID,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-30 06:07:55 +01:00
|
|
|
// 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.
|
2017-12-25 03:04:34 +01:00
|
|
|
func (s *Storage) updateEntry(entry *model.Entry) error {
|
2017-11-20 06:10:04 +01:00
|
|
|
query := `
|
|
|
|
UPDATE entries SET
|
2018-04-07 22:50:45 +02:00
|
|
|
title=$1, url=$2, comments_url=$3, content=$4, author=$5
|
|
|
|
WHERE user_id=$6 AND feed_id=$7 AND hash=$8
|
2017-11-22 01:08:43 +01:00
|
|
|
RETURNING id
|
2017-11-20 06:10:04 +01:00
|
|
|
`
|
2017-11-22 01:08:43 +01:00
|
|
|
err := s.db.QueryRow(
|
2017-11-20 06:10:04 +01:00
|
|
|
query,
|
|
|
|
entry.Title,
|
|
|
|
entry.URL,
|
2018-04-07 22:50:45 +02:00
|
|
|
entry.CommentsURL,
|
2017-11-20 06:10:04 +01:00
|
|
|
entry.Content,
|
|
|
|
entry.Author,
|
|
|
|
entry.UserID,
|
|
|
|
entry.FeedID,
|
|
|
|
entry.Hash,
|
2017-11-22 01:08:43 +01:00
|
|
|
).Scan(&entry.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, enclosure := range entry.Enclosures {
|
|
|
|
enclosure.UserID = entry.UserID
|
|
|
|
enclosure.EntryID = entry.ID
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2017-11-22 01:08:43 +01:00
|
|
|
return s.UpdateEnclosures(entry.Enclosures)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-12-25 03:04:34 +01:00
|
|
|
// entryExists checks if an entry already exists based on its hash when refreshing a feed.
|
|
|
|
func (s *Storage) entryExists(entry *model.Entry) bool {
|
2017-11-20 06:10:04 +01:00
|
|
|
var result int
|
|
|
|
query := `SELECT count(*) as c FROM entries WHERE user_id=$1 AND feed_id=$2 AND hash=$3`
|
|
|
|
s.db.QueryRow(query, entry.UserID, entry.FeedID, entry.Hash).Scan(&result)
|
|
|
|
return result >= 1
|
|
|
|
}
|
|
|
|
|
2018-01-20 23:04:19 +01:00
|
|
|
// UpdateEntries updates a list of entries while refreshing a feed.
|
|
|
|
func (s *Storage) UpdateEntries(userID, feedID int64, entries model.Entries, updateExistingEntries bool) (err error) {
|
2017-11-22 01:33:36 +01:00
|
|
|
var entryHashes []string
|
2017-11-20 06:10:04 +01:00
|
|
|
for _, entry := range entries {
|
|
|
|
entry.UserID = userID
|
|
|
|
entry.FeedID = feedID
|
|
|
|
|
2017-12-25 03:04:34 +01:00
|
|
|
if s.entryExists(entry) {
|
2018-01-20 23:04:19 +01:00
|
|
|
if updateExistingEntries {
|
|
|
|
err = s.updateEntry(entry)
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
} else {
|
2017-12-25 03:04:34 +01:00
|
|
|
err = s.createEntry(entry)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-22 01:33:36 +01:00
|
|
|
|
|
|
|
entryHashes = append(entryHashes, entry.Hash)
|
|
|
|
}
|
|
|
|
|
2017-12-25 03:04:34 +01:00
|
|
|
if err := s.cleanupEntries(feedID, entryHashes); err != nil {
|
2017-12-16 03:55:57 +01:00
|
|
|
logger.Error("[Storage:CleanupEntries] %v", err)
|
2017-11-22 01:33:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-25 03:04:34 +01:00
|
|
|
// 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 {
|
2017-11-22 01:33:36 +01:00
|
|
|
query := `
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("unable to cleanup entries: %v", err)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-22 00:46:59 +01:00
|
|
|
// SetEntriesStatus update the status of the given list of entries.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (s *Storage) SetEntriesStatus(userID int64, entryIDs []int64, status string) error {
|
2018-01-03 04:15:08 +01:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:SetEntriesStatus] userID=%d, entryIDs=%v, status=%s", userID, entryIDs, status))
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
query := `UPDATE entries SET status=$1 WHERE user_id=$2 AND id=ANY($3)`
|
|
|
|
result, err := s.db.Exec(query, status, userID, pq.Array(entryIDs))
|
|
|
|
if err != nil {
|
2017-11-22 00:46:59 +01:00
|
|
|
return fmt.Errorf("unable to update entries status: %v", err)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
count, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
2017-11-22 00:46:59 +01:00
|
|
|
return fmt.Errorf("unable to update these entries: %v", err)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if count == 0 {
|
2017-11-22 00:46:59 +01:00
|
|
|
return errors.New("nothing has been updated")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-22 20:33:01 +01:00
|
|
|
// ToggleBookmark toggles entry bookmark value.
|
|
|
|
func (s *Storage) ToggleBookmark(userID int64, entryID int64) error {
|
2018-01-03 04:15:08 +01:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:ToggleBookmark] userID=%d, entryID=%d", userID, entryID))
|
2017-12-22 20:33:01 +01:00
|
|
|
|
|
|
|
query := `UPDATE entries SET starred = NOT starred WHERE user_id=$1 AND id=$2`
|
2017-12-25 03:04:34 +01:00
|
|
|
result, err := s.db.Exec(query, userID, entryID)
|
2017-12-22 20:33:01 +01:00
|
|
|
if err != nil {
|
2017-12-25 03:04:34 +01:00
|
|
|
return fmt.Errorf("unable to toggle bookmark flag: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to toogle bookmark flag: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if count == 0 {
|
|
|
|
return errors.New("nothing has been updated")
|
2017-12-22 20:33:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-22 00:46:59 +01:00
|
|
|
// FlushHistory set all entries with the status "read" to "removed".
|
|
|
|
func (s *Storage) FlushHistory(userID int64) error {
|
2018-01-03 04:15:08 +01:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:FlushHistory] userID=%d", userID))
|
2017-11-22 00:46:59 +01:00
|
|
|
|
2017-12-22 20:33:01 +01:00
|
|
|
query := `UPDATE entries SET status=$1 WHERE user_id=$2 AND status=$3 AND starred='f'`
|
2017-11-22 00:46:59 +01:00
|
|
|
_, err := s.db.Exec(query, model.EntryStatusRemoved, userID, model.EntryStatusRead)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to flush history: %v", err)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-05 03:11:15 +01:00
|
|
|
|
|
|
|
// MarkAllAsRead set all entries with the status "unread" to "read".
|
|
|
|
func (s *Storage) MarkAllAsRead(userID int64) error {
|
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:MarkAllAsRead] userID=%d", userID))
|
|
|
|
|
|
|
|
query := `UPDATE entries SET status=$1 WHERE user_id=$2 AND status=$3`
|
|
|
|
_, err := s.db.Exec(query, model.EntryStatusRead, userID, model.EntryStatusUnread)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to mark all entries as read: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-20 03:43:27 +01:00
|
|
|
|
|
|
|
// EntryURLExists returns true if an entry with this URL already exists.
|
|
|
|
func (s *Storage) EntryURLExists(userID int64, entryURL string) bool {
|
|
|
|
var result int
|
|
|
|
query := `SELECT count(*) as c FROM entries WHERE user_id=$1 AND url=$2`
|
|
|
|
s.db.QueryRow(query, userID, entryURL).Scan(&result)
|
|
|
|
return result >= 1
|
|
|
|
}
|