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