Ver código fonte

Add category feeds refresh

Davide Masserut 3 anos atrás
pai
commit
ce35b46fee

+ 1 - 0
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)

+ 17 - 0
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)
+}

+ 6 - 0
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")

+ 17 - 0
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 {

+ 3 - 0
template/templates/views/category_entries.html

@@ -36,6 +36,9 @@
         <li>
             <a href="{{ route "categoryFeeds" "categoryID" .category.ID }}">{{ icon "feeds" }}{{ t "menu.feeds" }}</a>
         </li>
+        <li>
+            <a href="{{ route "refreshCategoryEntriesPage" "categoryID" .category.ID }}">{{ icon "refresh" }}{{ t "menu.refresh_all_feeds" }}</a>
+        </li>
     </ul>
 </section>
 

+ 3 - 0
template/templates/views/category_feeds.html

@@ -22,6 +22,9 @@
                 data-url="{{ route "removeCategory" "categoryID" .category.ID }}">{{ icon "delete" }}{{ t "action.remove" }}</a>
         </li>
         {{ end }}
+        <li>
+            <a href="{{ route "refreshCategoryFeedsPage" "categoryID" .category.ID }}">{{ icon "refresh" }}{{ t "menu.refresh_all_feeds" }}</a>
+        </li>
     </ul>
 </section>
 

+ 40 - 0
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
+}

+ 2 - 0
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)