diff --git a/api/api.go b/api/api.go index ceab2697..ff2126d2 100644 --- a/api/api.go +++ b/api/api.go @@ -43,6 +43,7 @@ func Serve(router *mux.Router, store *storage.Storage, pool *worker.Pool) { sr.HandleFunc("/categories/{categoryID}", handler.removeCategory).Methods(http.MethodDelete) sr.HandleFunc("/categories/{categoryID}/mark-all-as-read", handler.markCategoryAsRead).Methods(http.MethodPut) sr.HandleFunc("/categories/{categoryID}/feeds", handler.getCategoryFeeds).Methods(http.MethodGet) + sr.HandleFunc("/categories/{categoryID}/refresh", handler.refreshCategory).Methods(http.MethodPut) sr.HandleFunc("/categories/{categoryID}/entries", handler.getCategoryEntries).Methods(http.MethodGet) sr.HandleFunc("/categories/{categoryID}/entries/{entryID}", handler.getCategoryEntry).Methods(http.MethodGet) sr.HandleFunc("/discover", handler.discoverSubscriptions).Methods(http.MethodPost) diff --git a/api/category.go b/api/category.go index 9a351581..64cc036b 100644 --- a/api/category.go +++ b/api/category.go @@ -123,3 +123,20 @@ func (h *handler) removeCategory(w http.ResponseWriter, r *http.Request) { json.NoContent(w, r) } + +func (h *handler) refreshCategory(w http.ResponseWriter, r *http.Request) { + userID := request.UserID(r) + categoryID := request.RouteInt64Param(r, "categoryID") + + jobs, err := h.store.NewCategoryBatch(userID, categoryID, h.store.CountFeeds(userID)) + if err != nil { + json.ServerError(w, r, err) + return + } + + go func() { + h.pool.Push(jobs) + }() + + json.NoContent(w, r) +} diff --git a/client/client.go b/client/client.go index 218e7899..e1276d06 100644 --- a/client/client.go +++ b/client/client.go @@ -248,6 +248,12 @@ func (c *Client) DeleteCategory(categoryID int64) error { return c.request.Delete(fmt.Sprintf("/v1/categories/%d", categoryID)) } +// RefreshCategory refreshes a category. +func (c *Client) RefreshCategory(categoryID int64) error { + _, err := c.request.Put(fmt.Sprintf("/v1/categories/%d/refresh", categoryID), nil) + return err +} + // Feeds gets all feeds. func (c *Client) Feeds() (Feeds, error) { body, err := c.request.Get("/v1/feeds") diff --git a/storage/job.go b/storage/job.go index 447b87e7..57e289ed 100644 --- a/storage/job.go +++ b/storage/job.go @@ -45,6 +45,23 @@ func (s *Storage) NewUserBatch(userID int64, batchSize int) (jobs model.JobList, return s.fetchBatchRows(fmt.Sprintf(query, batchSize), userID) } +// NewCategoryBatch returns a series of jobs but only for a given category. +func (s *Storage) NewCategoryBatch(userID int64, categoryID int64, batchSize int) (jobs model.JobList, err error) { + // We do not take the error counter into consideration when the given + // user refresh manually all his feeds to force a refresh. + query := ` + SELECT + id, + user_id + FROM + feeds + WHERE + user_id=$1 AND category_id=$2 AND disabled is false + ORDER BY next_check_at ASC LIMIT %d + ` + return s.fetchBatchRows(fmt.Sprintf(query, batchSize), userID, categoryID) +} + func (s *Storage) fetchBatchRows(query string, args ...interface{}) (jobs model.JobList, err error) { rows, err := s.db.Query(query, args...) if err != nil { diff --git a/template/templates/views/category_entries.html b/template/templates/views/category_entries.html index 7785ee67..b221370b 100644 --- a/template/templates/views/category_entries.html +++ b/template/templates/views/category_entries.html @@ -36,6 +36,9 @@
  • {{ icon "feeds" }}{{ t "menu.feeds" }}
  • +
  • + {{ icon "refresh" }}{{ t "menu.refresh_all_feeds" }} +
  • diff --git a/template/templates/views/category_feeds.html b/template/templates/views/category_feeds.html index 26d4d2a7..8149949d 100644 --- a/template/templates/views/category_feeds.html +++ b/template/templates/views/category_feeds.html @@ -22,6 +22,9 @@ data-url="{{ route "removeCategory" "categoryID" .category.ID }}">{{ icon "delete" }}{{ t "action.remove" }} {{ end }} +
  • + {{ icon "refresh" }}{{ t "menu.refresh_all_feeds" }} +
  • diff --git a/ui/category_refresh.go b/ui/category_refresh.go new file mode 100644 index 00000000..0b4689f9 --- /dev/null +++ b/ui/category_refresh.go @@ -0,0 +1,40 @@ +// Copyright 2018 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 ui // import "miniflux.app/ui" + +import ( + "net/http" + + "miniflux.app/http/request" + "miniflux.app/http/response/html" + "miniflux.app/http/route" +) + +func (h *handler) refreshCategoryEntriesPage(w http.ResponseWriter, r *http.Request) { + categoryID := h.refreshCategory(w, r) + html.Redirect(w, r, route.Path(h.router, "categoryEntries", "categoryID", categoryID)) +} + +func (h *handler) refreshCategoryFeedsPage(w http.ResponseWriter, r *http.Request) { + categoryID := h.refreshCategory(w, r) + html.Redirect(w, r, route.Path(h.router, "categoryFeeds", "categoryID", categoryID)) +} + +func (h *handler) refreshCategory(w http.ResponseWriter, r *http.Request) int64 { + userID := request.UserID(r) + categoryID := request.RouteInt64Param(r, "categoryID") + + jobs, err := h.store.NewCategoryBatch(userID, categoryID, h.store.CountFeeds(userID)) + if err != nil { + html.ServerError(w, r, err) + return 0 + } + + go func() { + h.pool.Push(jobs) + }() + + return categoryID +} diff --git a/ui/ui.go b/ui/ui.go index 21ab45ba..3cac810e 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -83,7 +83,9 @@ func Serve(router *mux.Router, store *storage.Storage, pool *worker.Pool) { uiRouter.HandleFunc("/category/create", handler.showCreateCategoryPage).Name("createCategory").Methods(http.MethodGet) uiRouter.HandleFunc("/category/save", handler.saveCategory).Name("saveCategory").Methods(http.MethodPost) uiRouter.HandleFunc("/category/{categoryID}/feeds", handler.showCategoryFeedsPage).Name("categoryFeeds").Methods(http.MethodGet) + uiRouter.HandleFunc("/category/{categoryID}/feeds/refresh", handler.refreshCategoryFeedsPage).Name("refreshCategoryFeedsPage").Methods(http.MethodGet) uiRouter.HandleFunc("/category/{categoryID}/entries", handler.showCategoryEntriesPage).Name("categoryEntries").Methods(http.MethodGet) + uiRouter.HandleFunc("/category/{categoryID}/entries/refresh", handler.refreshCategoryEntriesPage).Name("refreshCategoryEntriesPage").Methods(http.MethodGet) uiRouter.HandleFunc("/category/{categoryID}/entries/all", handler.showCategoryEntriesAllPage).Name("categoryEntriesAll").Methods(http.MethodGet) uiRouter.HandleFunc("/category/{categoryID}/edit", handler.showEditCategoryPage).Name("editCategory").Methods(http.MethodGet) uiRouter.HandleFunc("/category/{categoryID}/update", handler.updateCategory).Name("updateCategory").Methods(http.MethodPost)