8000 [pull] main from miniflux:main by pull[bot] · Pull Request #284 · suhaoGe/miniflux · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion internal/api/category_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ func (h *handler) getCategoriesHandler(w http.ResponseWriter, r *http.Request) {
includeCounts := request.QueryStringParam(r, "counts", "false")

if includeCounts == "true" {
categories, err = h.store.CategoriesWithFeedCount(request.UserID(r))
user, userErr := h.store.UserByID(request.UserID(r))
if userErr != nil {
response.JSONServerError(w, r, userErr)
return
}
categories, err = h.store.CategoriesWithFeedCount(user.ID, user.CategoriesSortingOrder)
} else {
categories, err = h.store.Categories(request.UserID(r))
}
Expand Down
10 changes: 9 additions & 1 deletion internal/api/user_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package api // import "miniflux.app/v2/internal/api"
import (
json_parser "encoding/json"
"errors"
"log/slog"
"net/http"

"miniflux.app/v2/internal/http/request"
Expand Down Expand Up @@ -242,6 +243,13 @@ func (h *handler) removeUserHandler(w http.ResponseWriter, r *http.Request) {
return
}

h.store.RemoveUserAsync(user.ID)
go func() {
if err := h.store.RemoveUser(user.ID); err != nil {
slog.Error("Unable to delete user",
slog.Int64("user_id", user.ID),
slog.Any("error", err),
)
}
}()
response.NoContent(w, r)
}
10 changes: 10 additions & 0 deletions internal/database/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -1486,4 +1486,14 @@ var migrations = [...]func(tx *sql.Tx) error{
`)
return err
},
func(tx *sql.Tx) (err error) {
_, err = tx.Exec(`
DELETE FROM integrations WHERE user_id NOT IN (SELECT id FROM users);

ALTER TABLE integrations
ADD CONSTRAINT integrations_user_id_fkey
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
`)
return err
},
}
11 changes: 3 additions & 8 deletions internal/storage/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,8 @@ func (s *Storage) Categories(userID int64) (model.Categories, error) {
return categories, nil
}

// CategoriesWithFeedCount returns all categories with the number of feeds.
func (s *Storage) CategoriesWithFeedCount(userID int64) (model.Categories, error) {
user, err := s.UserByID(userID)
if err != nil {
return nil, err
}

// CategoriesWithFeedCount returns all categories with the number of feeds, sorted according to sortOrder.
func (s *Storage) CategoriesWithFeedCount(userID int64, sortOrder string) (model.Categories, error) {
query := `
SELECT
c.id,
Expand All @@ -132,7 +127,7 @@ func (s *Storage) CategoriesWithFeedCount(userID int64) (model.Categories, error
user_id=$2
`

if user.CategoriesSortingOrder == "alphabetical" {
if sortOrder == "alphabetical" {
query += `
ORDER BY
c.title ASC
Expand Down
36 changes: 19 additions & 17 deletions internal/storage/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,27 +445,29 @@ func (s *Storage) SetEntriesStatus(userID int64, entryIDs []int64, status string
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
}

// SetEntriesStatusAndCountVisible updates the status of the given entries and returns how many are visible in global views.
func (s *Storage) SetEntriesStatusAndCountVisible(userID int64, entryIDs []int64, status string) (int, error) {
query := `
WITH updated AS (
UPDATE entries
SET
status=$1,
changed_at=now()
WHERE
user_id=$2 AND
id=ANY($3)
RETURNING feed_id
)
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
FROM updated u
JOIN feeds f ON (f.id = u.feed_id)
JOIN categories c ON (c.id = f.category_id)
WHERE 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)
var visible int
if err := s.db.QueryRow(query, status, userID, pq.Array(entryIDs)).Scan(&visible); err != nil {
return 0, fmt.Errorf(`store: unable to update entries status %v: %v`, entryIDs, err)
}

return visible, nil
}

Expand Down
51 changes: 13 additions & 38 deletions internal/storage/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"database/sql"
"errors"
"fmt"
"log/slog"
"sort"
"time"

Expand Down Expand Up @@ -52,31 +51,31 @@ func (s *Storage) CheckedAt(userID, feedID int64) (time.Time, error) {
return result, nil
}

// CategoryFeedExists returns true if the given feed exists that belongs to the given category.
// CategoryFeedExists returns true if the given feed exists and belongs to the given category.
func (s *Storage) CategoryFeedExists(userID, categoryID, feedID int64) bool {
var result bool
query := `SELECT true FROM feeds WHERE user_id=$1 AND category_id=$2 AND id=$3 LIMIT 1`
s.db.QueryRow(query, userID, categoryID, feedID).Scan(&result)
return result
}

// FeedURLExists checks if feed URL already exists.
// FeedURLExists returns true if the given feed URL already exists for the user.
func (s *Storage) FeedURLExists(userID int64, feedURL string) bool {
var result bool
query := `SELECT true FROM feeds WHERE user_id=$1 AND feed_url=$2 LIMIT 1`
s.db.QueryRow(query, userID, feedURL).Scan(&result)
return result
}

// AnotherFeedURLExists checks if the user a duplicated feed.
// AnotherFeedURLExists returns true if another feed with the same URL exists for the user.
func (s *Storage) AnotherFeedURLExists(userID, feedID int64, feedURL string) bool {
var result bool
query := `SELECT true FROM feeds WHERE id <> $1 AND user_id=$2 AND feed_url=$3 LIMIT 1`
s.db.QueryRow(query, feedID, userID, feedURL).Scan(&result)
return result
}

// CountAllFeeds returns the number of feeds in the database.
// CountAllFeeds returns the number of feeds keyed by enabled, disabled, and total.
func (s *Storage) CountAllFeeds() (map[string]int64, error) {
rows, err := s.db.Query(`SELECT disabled, count(*) FROM feeds GROUP BY disabled`)
if err != nil {
Expand Down Expand Up @@ -141,7 +140,7 @@ func (s *Storage) CountAllFeedsWithErrors() (int, error) {
return result, nil
}

// Feeds returns all feeds that belongs to the given user.
// Feeds returns all feeds that belong to the given user.
func (s *Storage) Feeds(userID int64) (model.Feeds, error) {
builder := NewFeedQueryBuilder(s, userID)
builder.WithSorting(model.DefaultFeedSorting, model.DefaultFeedSortingDirection)
Expand All @@ -157,23 +156,23 @@ func getFeedsSorted(builder *feedQueryBuilder) (model.Feeds, error) {
return result, err
}

// FeedsWithCounters returns all feeds of the given user with counters of read and unread entries.
// FeedsWithCounters returns all feeds of the given user with read and unread entry counters.
func (s *Storage) FeedsWithCounters(userID int64) (model.Feeds, error) {
builder := NewFeedQueryBuilder(s, userID)
builder.WithCounters()
builder.WithSorting(model.DefaultFeedSorting, model.DefaultFeedSortingDirection)
return getFeedsSorted(builder)
}

// FetchCounters returns read and unread count.
// FetchCounters returns the per-feed read and unread entry counts for the given user.
func (s *Storage) FetchCounters(userID int64) (model.FeedCounters, error) {
builder := NewFeedQueryBuilder(s, userID)
builder.WithCounters()
reads, unreads, err := builder.fetchFeedCounter()
return model.FeedCounters{ReadCounters: reads, UnreadCounters: unreads}, err
}

// FeedsByCategoryWithCounters returns all feeds of the given user/category with counters of read and unread entries.
// FeedsByCategoryWithCounters returns all feeds in the given category for the given user with read and unread entry counters.
func (s *Storage) FeedsByCategoryWithCounters(userID, cat E870 egoryID int64) (model.Feeds, error) {
builder := NewFeedQueryBuilder(s, userID)
builder.WithCategoryID(categoryID)
Expand Down Expand Up @@ -214,7 +213,7 @@ func (s *Storage) WeeklyFeedEntryCount(userID, feedID int64) (int, error) {
return weeklyCount, nil
}

// FeedByID returns a feed by the ID.
// FeedByID returns the feed with the given ID.
func (s *Storage) FeedByID(userID, feedID int64) (*model.Feed, error) {
builder := NewFeedQueryBuilder(s, userID)
builder.WithFeedID(feedID)
Expand Down Expand Up @@ -442,7 +441,7 @@ func (s *Storage) UpdateFeed(feed *model.Feed) (err error) {
return nil
}

// UpdateFeedError updates feed errors.
// UpdateFeedError persists the parsing error fields for the given feed.
func (s *Storage) UpdateFeedError(feed *model.Feed) (err error) {
query := `
UPDATE
Expand Down Expand Up @@ -471,45 +470,21 @@ func (s *Storage) UpdateFeedError(feed *model.Feed) (err error) {
return nil
}

// RemoveFeed removes a feed and all entries.
// This operation can takes time if the feed has lot of entries.
// RemoveFeed removes the given feed along with its entries and enclosures.
func (s *Storage) RemoveFeed(userID, feedID int64) error {
rows, err := s.db.Query(`SELECT id FROM entries WHERE user_id=$1 AND feed_id=$2`, userID, feedID)
if err != nil {
return fmt.Errorf(`store: unable to get user feed entries: %v`, err)
}
defer rows.Close()

for rows.Next() {
var entryID int64
if err := rows.Scan(&entryID); err != nil {
return fmt.Errorf(`store: unable to read user feed entry ID: %v`, err)
}

slog.Debug("Deleting entry",
slog.Int64("user_id", userID),
slog.Int64("feed_id", feedID),
slog.Int64("entry_id", entryID),
)

if _, err := s.db.Exec(`DELETE FROM entries WHERE id=$1 AND user_id=$2`, entryID, userID); err != nil {
return fmt.Errorf(`store: unable to delete user feed entries #%d: %v`, entryID, err)
}
}

if _, err := s.db.Exec(`DELETE FROM feeds WHERE id=$1 AND user_id=$2`, feedID, userID); err != nil {
return fmt.Errorf(`store: unable to delete feed #%d: %v`, feedID, err)
}

return nil
}

// ResetFeedErrors removes all feed errors.
// ResetFeedErrors clears the parsing error fields for all feeds.
func (s *Storage) ResetFeedErrors() error {
_, err := s.db.Exec(`UPDATE feeds SET parsing_error_count=0, parsing_error_msg=''`)
return err
}

// ResetNextCheckAt schedules all feeds to be checked immediately.
func (s *Storage) ResetNextCheckAt() error {
_, err := s.db.Exec(`UPDATE feeds SET next_check_at=now()`)
return err
Expand Down
Loading
Loading
0