From f6028f38634c548d13232fc9b86a9428e5ec8c81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Fri, 26 Oct 2018 19:49:49 -0700 Subject: [PATCH] Improve Fever middleware and handle groupID=0 --- fever/fever.go | 10 +++++++++- middleware/fever.go | 11 +++++++++-- storage/entry.go | 5 ++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/fever/fever.go b/fever/fever.go index cb658743..4a89f45a 100644 --- a/fever/fever.go +++ b/fever/fever.go @@ -573,7 +573,15 @@ func (c *Controller) handleWriteGroups(w http.ResponseWriter, r *http.Request) { } go func() { - if err := c.store.MarkCategoryAsRead(userID, groupID, before); err != nil { + var err error + + if groupID == 0 { + err = c.store.MarkAllAsRead(userID) + } else { + err = c.store.MarkCategoryAsRead(userID, groupID, before) + } + + if err != nil { logger.Error("[Fever] MarkCategoryAsRead failed: %v", err) } }() diff --git a/middleware/fever.go b/middleware/fever.go index e4dc1d42..7b96677f 100644 --- a/middleware/fever.go +++ b/middleware/fever.go @@ -13,21 +13,28 @@ import ( "miniflux.app/logger" ) +var feverAuthFailureResponse = map[string]int{"api_version": 3, "auth": 0} + // FeverAuth handles Fever API authentication. func (m *Middleware) FeverAuth(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { apiKey := r.FormValue("api_key") + if apiKey == "" { + logger.Info("[Middleware:Fever] No API key provided") + json.OK(w, r, feverAuthFailureResponse) + return + } user, err := m.store.UserByFeverToken(apiKey) if err != nil { logger.Error("[Middleware:Fever] %v", err) - json.OK(w, r, map[string]int{"api_version": 3, "auth": 0}) + json.OK(w, r, feverAuthFailureResponse) return } if user == nil { logger.Info("[Middleware:Fever] No user found with this API key") - json.OK(w, r, map[string]int{"api_version": 3, "auth": 0}) + json.OK(w, r, feverAuthFailureResponse) return } diff --git a/storage/entry.go b/storage/entry.go index 0405deb8..7617bb6a 100644 --- a/storage/entry.go +++ b/storage/entry.go @@ -261,11 +261,14 @@ 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) + result, 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) } + count, _ := result.RowsAffected() + logger.Debug("[Storage:MarkAllAsRead] %d items marked as read", count) + return nil }