1
0
Fork 0

Improve Fever middleware and handle groupID=0

This commit is contained in:
Frédéric Guillot 2018-10-26 19:49:49 -07:00
parent 92c98bd986
commit f6028f3863
3 changed files with 22 additions and 4 deletions

View file

@ -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)
}
}()

View file

@ -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
}

View file

@ -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
}