1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-01 17:38:37 +00:00

add option to hide categories from the global unread list

This commit is contained in:
pennae 2021-06-03 02:39:47 +02:00 committed by fguillot
parent 571d7bf17c
commit 0bcfc81b1f
24 changed files with 109 additions and 30 deletions

View file

@ -40,8 +40,8 @@ func (s *Storage) CategoryIDExists(userID, categoryID int64) bool {
func (s *Storage) Category(userID, categoryID int64) (*model.Category, error) {
var category model.Category
query := `SELECT id, user_id, title FROM categories WHERE user_id=$1 AND id=$2`
err := s.db.QueryRow(query, userID, categoryID).Scan(&category.ID, &category.UserID, &category.Title)
query := `SELECT id, user_id, title, hide_globally FROM categories WHERE user_id=$1 AND id=$2`
err := s.db.QueryRow(query, userID, categoryID).Scan(&category.ID, &category.UserID, &category.Title, &category.HideGlobally)
switch {
case err == sql.ErrNoRows:
@ -55,10 +55,10 @@ func (s *Storage) Category(userID, categoryID int64) (*model.Category, error) {
// FirstCategory returns the first category for the given user.
func (s *Storage) FirstCategory(userID int64) (*model.Category, error) {
query := `SELECT id, user_id, title FROM categories WHERE user_id=$1 ORDER BY title ASC LIMIT 1`
query := `SELECT id, user_id, title, hide_globally FROM categories WHERE user_id=$1 ORDER BY title ASC LIMIT 1`
var category model.Category
err := s.db.QueryRow(query, userID).Scan(&category.ID, &category.UserID, &category.Title)
err := s.db.QueryRow(query, userID).Scan(&category.ID, &category.UserID, &category.Title, &category.HideGlobally)
switch {
case err == sql.ErrNoRows:
@ -74,8 +74,8 @@ func (s *Storage) FirstCategory(userID int64) (*model.Category, error) {
func (s *Storage) CategoryByTitle(userID int64, title string) (*model.Category, error) {
var category model.Category
query := `SELECT id, user_id, title FROM categories WHERE user_id=$1 AND title=$2`
err := s.db.QueryRow(query, userID, title).Scan(&category.ID, &category.UserID, &category.Title)
query := `SELECT id, user_id, title, hide_globally FROM categories WHERE user_id=$1 AND title=$2`
err := s.db.QueryRow(query, userID, title).Scan(&category.ID, &category.UserID, &category.Title, &category.HideGlobally)
switch {
case err == sql.ErrNoRows:
@ -89,7 +89,7 @@ func (s *Storage) CategoryByTitle(userID int64, title string) (*model.Category,
// Categories returns all categories that belongs to the given user.
func (s *Storage) Categories(userID int64) (model.Categories, error) {
query := `SELECT id, user_id, title FROM categories WHERE user_id=$1 ORDER BY title ASC`
query := `SELECT id, user_id, title, hide_globally FROM categories WHERE user_id=$1 ORDER BY title ASC`
rows, err := s.db.Query(query, userID)
if err != nil {
return nil, fmt.Errorf(`store: unable to fetch categories: %v`, err)
@ -99,7 +99,7 @@ func (s *Storage) Categories(userID int64) (model.Categories, error) {
categories := make(model.Categories, 0)
for rows.Next() {
var category model.Category
if err := rows.Scan(&category.ID, &category.UserID, &category.Title); err != nil {
if err := rows.Scan(&category.ID, &category.UserID, &category.Title, &category.HideGlobally); err != nil {
return nil, fmt.Errorf(`store: unable to fetch category row: %v`, err)
}
@ -116,6 +116,7 @@ func (s *Storage) CategoriesWithFeedCount(userID int64) (model.Categories, error
c.id,
c.user_id,
c.title,
c.hide_globally,
(SELECT count(*) FROM feeds WHERE feeds.category_id=c.id) AS count,
(SELECT count(*)
FROM feeds
@ -136,7 +137,7 @@ func (s *Storage) CategoriesWithFeedCount(userID int64) (model.Categories, error
categories := make(model.Categories, 0)
for rows.Next() {
var category model.Category
if err := rows.Scan(&category.ID, &category.UserID, &category.Title, &category.FeedCount, &category.TotalUnread); err != nil {
if err := rows.Scan(&category.ID, &category.UserID, &category.Title, &category.HideGlobally, &category.FeedCount, &category.TotalUnread); err != nil {
return nil, fmt.Errorf(`store: unable to fetch category row: %v`, err)
}
@ -179,10 +180,11 @@ func (s *Storage) CreateCategory(userID int64, request *model.CategoryRequest) (
// UpdateCategory updates an existing category.
func (s *Storage) UpdateCategory(category *model.Category) error {
query := `UPDATE categories SET title=$1 WHERE id=$2 AND user_id=$3`
query := `UPDATE categories SET title=$1, hide_globally = $2 WHERE id=$3 AND user_id=$4`
_, err := s.db.Exec(
query,
category.Title,
category.HideGlobally,
category.ID,
category.UserID,
)

View file

@ -49,6 +49,7 @@ func (s *Storage) CountAllEntries() map[string]int64 {
func (s *Storage) CountUnreadEntries(userID int64) int {
builder := s.NewEntryQueryBuilder(userID)
builder.WithStatus(model.EntryStatusUnread)
builder.WithGloballyVisible()
n, err := builder.CountEntries()
if err != nil {
@ -346,6 +347,27 @@ 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
}
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 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
}
// 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`

View file

@ -181,9 +181,20 @@ func (e *EntryQueryBuilder) WithOffset(offset int) *EntryQueryBuilder {
return e
}
func (e *EntryQueryBuilder) WithGloballyVisible() *EntryQueryBuilder {
e.conditions = append(e.conditions, "not c.hide_globally")
return e
}
// CountEntries count the number of entries that match the condition.
func (e *EntryQueryBuilder) CountEntries() (count int, err error) {
query := `SELECT count(*) FROM entries e LEFT JOIN feeds f ON f.id=e.feed_id WHERE %s`
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 %s
`
condition := e.buildCondition()
err = e.store.db.QueryRow(fmt.Sprintf(query, condition), e.args...).Scan(&count)