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.
|
|
|
|
|
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-11-22 01:36:00 -05:00
|
|
|
"time"
|
|
|
|
|
2017-12-13 00:48:13 -05:00
|
|
|
"github.com/miniflux/miniflux/model"
|
2018-01-02 22:15:08 -05:00
|
|
|
"github.com/miniflux/miniflux/timer"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// FeedExists checks if the given feed exists.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (s *Storage) FeedExists(userID, feedID int64) bool {
|
2018-01-02 22:15:08 -05:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:FeedExists] userID=%d, feedID=%d", userID, feedID))
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
var result int
|
|
|
|
query := `SELECT count(*) as c FROM feeds WHERE user_id=$1 AND id=$2`
|
|
|
|
s.db.QueryRow(query, userID, feedID).Scan(&result)
|
|
|
|
return result >= 1
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// FeedURLExists checks if feed URL already exists.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (s *Storage) FeedURLExists(userID int64, feedURL string) bool {
|
2018-01-02 22:15:08 -05:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:FeedURLExists] userID=%d, feedURL=%s", userID, feedURL))
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
var result int
|
|
|
|
query := `SELECT count(*) as c FROM feeds WHERE user_id=$1 AND feed_url=$2`
|
|
|
|
s.db.QueryRow(query, userID, feedURL).Scan(&result)
|
|
|
|
return result >= 1
|
|
|
|
}
|
|
|
|
|
2017-11-22 01:36:00 -05:00
|
|
|
// CountFeeds returns the number of feeds that belongs to the given user.
|
|
|
|
func (s *Storage) CountFeeds(userID int64) int {
|
|
|
|
var result int
|
|
|
|
err := s.db.QueryRow(`SELECT count(*) FROM feeds WHERE user_id=$1`, userID).Scan(&result)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// Feeds returns all feeds of the given user.
|
|
|
|
func (s *Storage) Feeds(userID int64) (model.Feeds, error) {
|
2018-01-02 22:15:08 -05:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:Feeds] userID=%d", userID))
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
feeds := make(model.Feeds, 0)
|
|
|
|
query := `SELECT
|
|
|
|
f.id, f.feed_url, f.site_url, f.title, f.etag_header, f.last_modified_header,
|
2017-12-28 22:20:14 -05:00
|
|
|
f.user_id, f.checked_at at time zone u.timezone,
|
|
|
|
f.parsing_error_count, f.parsing_error_msg,
|
2017-12-12 22:19:36 -05:00
|
|
|
f.scraper_rules, f.rewrite_rules, f.crawler,
|
2017-11-20 00:10:04 -05:00
|
|
|
f.category_id, c.title as category_title,
|
|
|
|
fi.icon_id
|
|
|
|
FROM feeds f
|
|
|
|
LEFT JOIN categories c ON c.id=f.category_id
|
|
|
|
LEFT JOIN feed_icons fi ON fi.feed_id=f.id
|
2017-12-28 22:20:14 -05:00
|
|
|
LEFT JOIN users u ON u.id=f.user_id
|
2017-11-20 00:10:04 -05:00
|
|
|
WHERE f.user_id=$1
|
2017-12-26 23:42:43 -05:00
|
|
|
ORDER BY f.parsing_error_count DESC, f.title ASC`
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
rows, err := s.db.Query(query, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to fetch feeds: %v", err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var feed model.Feed
|
2017-12-24 21:04:34 -05:00
|
|
|
var iconID interface{}
|
2017-11-20 00:10:04 -05:00
|
|
|
feed.Category = &model.Category{UserID: userID}
|
|
|
|
|
|
|
|
err := rows.Scan(
|
|
|
|
&feed.ID,
|
|
|
|
&feed.FeedURL,
|
|
|
|
&feed.SiteURL,
|
|
|
|
&feed.Title,
|
|
|
|
&feed.EtagHeader,
|
|
|
|
&feed.LastModifiedHeader,
|
|
|
|
&feed.UserID,
|
|
|
|
&feed.CheckedAt,
|
|
|
|
&feed.ParsingErrorCount,
|
2017-12-24 21:04:34 -05:00
|
|
|
&feed.ParsingErrorMsg,
|
2017-12-10 23:51:04 -05:00
|
|
|
&feed.ScraperRules,
|
2017-12-12 01:16:32 -05:00
|
|
|
&feed.RewriteRules,
|
2017-12-12 22:19:36 -05:00
|
|
|
&feed.Crawler,
|
2017-11-20 00:10:04 -05:00
|
|
|
&feed.Category.ID,
|
|
|
|
&feed.Category.Title,
|
|
|
|
&iconID,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2017-12-24 21:04:34 -05:00
|
|
|
return nil, fmt.Errorf("unable to fetch feeds row: %v", err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-12-24 21:04:34 -05:00
|
|
|
if iconID != nil {
|
|
|
|
feed.Icon = &model.FeedIcon{FeedID: feed.ID, IconID: iconID.(int64)}
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
feeds = append(feeds, &feed)
|
|
|
|
}
|
|
|
|
|
|
|
|
return feeds, nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// FeedByID returns a feed by the ID.
|
|
|
|
func (s *Storage) FeedByID(userID, feedID int64) (*model.Feed, error) {
|
2018-01-02 22:15:08 -05:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:FeedByID] feedID=%d", feedID))
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
var feed model.Feed
|
2017-12-24 21:04:34 -05:00
|
|
|
var iconID interface{}
|
2017-11-20 00:10:04 -05:00
|
|
|
feed.Category = &model.Category{UserID: userID}
|
|
|
|
|
|
|
|
query := `
|
|
|
|
SELECT
|
|
|
|
f.id, f.feed_url, f.site_url, f.title, f.etag_header, f.last_modified_header,
|
2017-12-28 22:20:14 -05:00
|
|
|
f.user_id, f.checked_at at time zone u.timezone,
|
|
|
|
f.parsing_error_count, f.parsing_error_msg,
|
2017-12-12 22:19:36 -05:00
|
|
|
f.scraper_rules, f.rewrite_rules, f.crawler,
|
2017-12-24 21:04:34 -05:00
|
|
|
f.category_id, c.title as category_title,
|
|
|
|
fi.icon_id
|
2017-11-20 00:10:04 -05:00
|
|
|
FROM feeds f
|
|
|
|
LEFT JOIN categories c ON c.id=f.category_id
|
2017-12-24 21:04:34 -05:00
|
|
|
LEFT JOIN feed_icons fi ON fi.feed_id=f.id
|
2017-12-28 22:20:14 -05:00
|
|
|
LEFT JOIN users u ON u.id=f.user_id
|
2017-11-20 00:10:04 -05:00
|
|
|
WHERE f.user_id=$1 AND f.id=$2`
|
|
|
|
|
|
|
|
err := s.db.QueryRow(query, userID, feedID).Scan(
|
|
|
|
&feed.ID,
|
|
|
|
&feed.FeedURL,
|
|
|
|
&feed.SiteURL,
|
|
|
|
&feed.Title,
|
|
|
|
&feed.EtagHeader,
|
|
|
|
&feed.LastModifiedHeader,
|
|
|
|
&feed.UserID,
|
|
|
|
&feed.CheckedAt,
|
|
|
|
&feed.ParsingErrorCount,
|
|
|
|
&feed.ParsingErrorMsg,
|
2017-12-10 23:51:04 -05:00
|
|
|
&feed.ScraperRules,
|
2017-12-12 01:16:32 -05:00
|
|
|
&feed.RewriteRules,
|
2017-12-12 22:19:36 -05:00
|
|
|
&feed.Crawler,
|
2017-11-20 00:10:04 -05:00
|
|
|
&feed.Category.ID,
|
|
|
|
&feed.Category.Title,
|
2017-12-24 21:04:34 -05:00
|
|
|
&iconID,
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
|
|
|
return nil, nil
|
|
|
|
case err != nil:
|
2017-11-25 01:29:20 -05:00
|
|
|
return nil, fmt.Errorf("unable to fetch feed: %v", err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-12-24 21:04:34 -05:00
|
|
|
if iconID != nil {
|
|
|
|
feed.Icon = &model.FeedIcon{FeedID: feed.ID, IconID: iconID.(int64)}
|
|
|
|
}
|
|
|
|
|
2017-11-20 00:10:04 -05:00
|
|
|
return &feed, nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// CreateFeed creates a new feed.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (s *Storage) CreateFeed(feed *model.Feed) error {
|
2018-01-02 22:15:08 -05:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:CreateFeed] feedURL=%s", feed.FeedURL))
|
2017-11-20 00:10:04 -05:00
|
|
|
sql := `
|
|
|
|
INSERT INTO feeds
|
2017-12-12 22:19:36 -05:00
|
|
|
(feed_url, site_url, title, category_id, user_id, etag_header, last_modified_header, crawler)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
2017-11-20 00:10:04 -05:00
|
|
|
RETURNING id
|
|
|
|
`
|
|
|
|
|
|
|
|
err := s.db.QueryRow(
|
|
|
|
sql,
|
|
|
|
feed.FeedURL,
|
|
|
|
feed.SiteURL,
|
|
|
|
feed.Title,
|
|
|
|
feed.Category.ID,
|
|
|
|
feed.UserID,
|
|
|
|
feed.EtagHeader,
|
|
|
|
feed.LastModifiedHeader,
|
2017-12-12 22:19:36 -05:00
|
|
|
feed.Crawler,
|
2017-11-20 00:10:04 -05:00
|
|
|
).Scan(&feed.ID)
|
|
|
|
if err != nil {
|
2017-11-25 01:29:20 -05:00
|
|
|
return fmt.Errorf("unable to create feed: %v", err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(feed.Entries); i++ {
|
|
|
|
feed.Entries[i].FeedID = feed.ID
|
|
|
|
feed.Entries[i].UserID = feed.UserID
|
2017-12-24 21:04:34 -05:00
|
|
|
err := s.createEntry(feed.Entries[i])
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// UpdateFeed updates an existing feed.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (s *Storage) UpdateFeed(feed *model.Feed) (err error) {
|
2018-01-02 22:15:08 -05:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:UpdateFeed] feedURL=%s", feed.FeedURL))
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
query := `UPDATE feeds SET
|
|
|
|
feed_url=$1, site_url=$2, title=$3, category_id=$4, etag_header=$5, last_modified_header=$6, checked_at=$7,
|
2017-12-12 22:19:36 -05:00
|
|
|
parsing_error_msg=$8, parsing_error_count=$9, scraper_rules=$10, rewrite_rules=$11, crawler=$12
|
|
|
|
WHERE id=$13 AND user_id=$14`
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
_, err = s.db.Exec(query,
|
|
|
|
feed.FeedURL,
|
|
|
|
feed.SiteURL,
|
|
|
|
feed.Title,
|
|
|
|
feed.Category.ID,
|
|
|
|
feed.EtagHeader,
|
|
|
|
feed.LastModifiedHeader,
|
|
|
|
feed.CheckedAt,
|
|
|
|
feed.ParsingErrorMsg,
|
|
|
|
feed.ParsingErrorCount,
|
2017-12-10 23:51:04 -05:00
|
|
|
feed.ScraperRules,
|
2017-12-12 01:16:32 -05:00
|
|
|
feed.RewriteRules,
|
2017-12-12 22:19:36 -05:00
|
|
|
feed.Crawler,
|
2017-11-20 00:10:04 -05:00
|
|
|
feed.ID,
|
|
|
|
feed.UserID,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to update feed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// RemoveFeed removes a feed.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (s *Storage) RemoveFeed(userID, feedID int64) error {
|
2018-01-02 22:15:08 -05:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:RemoveFeed] userID=%d, feedID=%d", userID, feedID))
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
result, err := s.db.Exec("DELETE FROM feeds WHERE id = $1 AND user_id = $2", feedID, userID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to remove this feed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to remove this feed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if count == 0 {
|
|
|
|
return errors.New("no feed has been removed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|