miniflux/storage/entry.go

145 lines
3.6 KiB
Go
Raw Normal View History

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-11-20 06:10:04 +01:00
"github.com/miniflux/miniflux2/helper"
"github.com/miniflux/miniflux2/model"
"github.com/lib/pq"
)
2017-11-22 00:46:59 +01:00
// GetEntryQueryBuilder returns a new EntryQueryBuilder
2017-11-20 06:10:04 +01:00
func (s *Storage) GetEntryQueryBuilder(userID int64, timezone string) *EntryQueryBuilder {
return NewEntryQueryBuilder(s, userID, timezone)
}
2017-11-22 00:46:59 +01:00
// CreateEntry add a new entry.
2017-11-20 06:10:04 +01:00
func (s *Storage) CreateEntry(entry *model.Entry) error {
query := `
INSERT INTO entries
(title, hash, url, published_at, content, author, user_id, feed_id)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING id
`
err := s.db.QueryRow(
query,
entry.Title,
entry.Hash,
entry.URL,
entry.Date,
entry.Content,
entry.Author,
entry.UserID,
entry.FeedID,
).Scan(&entry.ID)
if err != nil {
return fmt.Errorf("Unable to create entry: %v", err)
}
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-11-22 00:46:59 +01:00
// UpdateEntry update an entry when a feed is refreshed.
2017-11-20 06:10:04 +01:00
func (s *Storage) UpdateEntry(entry *model.Entry) error {
query := `
UPDATE entries SET
title=$1, url=$2, published_at=$3, content=$4, author=$5
WHERE user_id=$6 AND feed_id=$7 AND hash=$8
`
_, err := s.db.Exec(
query,
entry.Title,
entry.URL,
entry.Date,
entry.Content,
entry.Author,
entry.UserID,
entry.FeedID,
entry.Hash,
)
return err
}
2017-11-22 00:46:59 +01:00
// EntryExists checks if an entry already exists based on its hash when refreshing a feed.
2017-11-20 06:10:04 +01:00
func (s *Storage) EntryExists(entry *model.Entry) bool {
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
}
2017-11-22 00:46:59 +01:00
// UpdateEntries update a list of entries while refreshing a feed.
2017-11-20 06:10:04 +01:00
func (s *Storage) UpdateEntries(userID, feedID int64, entries model.Entries) (err error) {
for _, entry := range entries {
entry.UserID = userID
entry.FeedID = feedID
if s.EntryExists(entry) {
err = s.UpdateEntry(entry)
} else {
err = s.CreateEntry(entry)
}
if err != nil {
return err
}
}
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 {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:SetEntriesStatus] userID=%d, entryIDs=%v, status=%s", userID, entryIDs, status))
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
}
// FlushHistory set all entries with the status "read" to "removed".
func (s *Storage) FlushHistory(userID int64) error {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:FlushHistory] userID=%d", userID))
query := `UPDATE entries SET status=$1 WHERE user_id=$2 AND status=$3`
_, 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
}