category_refresh.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package ui // import "miniflux.app/v2/internal/ui"
  4. import (
  5. "log/slog"
  6. "net/http"
  7. "time"
  8. "miniflux.app/v2/internal/config"
  9. "miniflux.app/v2/internal/http/request"
  10. "miniflux.app/v2/internal/http/response"
  11. "miniflux.app/v2/internal/locale"
  12. "miniflux.app/v2/internal/ui/session"
  13. )
  14. func (h *handler) refreshCategoryEntriesPage(w http.ResponseWriter, r *http.Request) {
  15. categoryID := h.refreshCategory(w, r)
  16. response.HTMLRedirect(w, r, h.routePath("/category/%d/entries", categoryID))
  17. }
  18. func (h *handler) refreshCategoryFeedsPage(w http.ResponseWriter, r *http.Request) {
  19. categoryID := h.refreshCategory(w, r)
  20. response.HTMLRedirect(w, r, h.routePath("/category/%d/feeds", categoryID))
  21. }
  22. func (h *handler) refreshCategory(w http.ResponseWriter, r *http.Request) int64 {
  23. categoryID := request.RouteInt64Param(r, "categoryID")
  24. printer := locale.NewPrinter(request.UserLanguage(r))
  25. sess := session.New(h.store, request.SessionID(r))
  26. // Avoid accidental and excessive refreshes.
  27. if time.Since(request.LastForceRefresh(r)) < config.Opts.ForceRefreshInterval() {
  28. interval := int(config.Opts.ForceRefreshInterval().Minutes())
  29. sess.NewFlashErrorMessage(printer.Plural("alert.too_many_feeds_refresh", interval, interval))
  30. } else {
  31. userID := request.UserID(r)
  32. // We allow the end-user to force refresh all its feeds in this category
  33. // without taking into consideration the number of errors.
  34. batchBuilder := h.store.NewBatchBuilder()
  35. batchBuilder.WithoutDisabledFeeds()
  36. batchBuilder.WithUserID(userID)
  37. batchBuilder.WithCategoryID(categoryID)
  38. batchBuilder.WithLimitPerHost(config.Opts.PollingLimitPerHost())
  39. jobs, err := batchBuilder.FetchJobs()
  40. if err != nil {
  41. response.HTMLServerError(w, r, err)
  42. return 0
  43. }
  44. slog.Info(
  45. "Triggered a manual refresh of all feeds for a given category from the web ui",
  46. slog.Int64("user_id", userID),
  47. slog.Int64("category_id", categoryID),
  48. slog.Int("nb_jobs", len(jobs)),
  49. )
  50. go h.pool.Push(jobs)
  51. sess.SetLastForceRefresh()
  52. sess.NewFlashMessage(printer.Print("alert.background_feed_refresh"))
  53. }
  54. return categoryID
  55. }