category_refresh.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. )
  13. func (h *handler) refreshCategoryEntriesPage(w http.ResponseWriter, r *http.Request) {
  14. categoryID := h.refreshCategory(w, r)
  15. response.HTMLRedirect(w, r, h.routePath("/category/%d/entries", categoryID))
  16. }
  17. func (h *handler) refreshCategoryFeedsPage(w http.ResponseWriter, r *http.Request) {
  18. categoryID := h.refreshCategory(w, r)
  19. response.HTMLRedirect(w, r, h.routePath("/category/%d/feeds", categoryID))
  20. }
  21. func (h *handler) refreshCategory(w http.ResponseWriter, r *http.Request) int64 {
  22. categoryID := request.RouteInt64Param(r, "categoryID")
  23. sess := request.WebSession(r)
  24. printer := locale.NewPrinter(sess.Language())
  25. // Avoid accidental and excessive refreshes.
  26. if time.Since(sess.LastForceRefresh()) < config.Opts.ForceRefreshInterval() {
  27. interval := int(config.Opts.ForceRefreshInterval().Minutes())
  28. sess.SetErrorMessage(printer.Plural("alert.too_many_feeds_refresh", interval, interval))
  29. } else {
  30. userID := request.UserID(r)
  31. // We allow the end-user to force refresh all its feeds in this category
  32. // without taking into consideration the number of errors.
  33. batchBuilder := h.store.NewBatchBuilder()
  34. batchBuilder.WithoutDisabledFeeds()
  35. batchBuilder.WithUserID(userID)
  36. batchBuilder.WithCategoryID(categoryID)
  37. batchBuilder.WithLimitPerHost(config.Opts.PollingLimitPerHost())
  38. jobs, err := batchBuilder.FetchJobs()
  39. if err != nil {
  40. response.HTMLServerError(w, r, err)
  41. return 0
  42. }
  43. slog.Info(
  44. "Triggered a manual refresh of all feeds for a given category from the web ui",
  45. slog.Int64("user_id", userID),
  46. slog.Int64("category_id", categoryID),
  47. slog.Int("nb_jobs", len(jobs)),
  48. )
  49. go h.pool.Push(jobs)
  50. sess.MarkForceRefreshed()
  51. sess.SetSuccessMessage(printer.Print("alert.background_feed_refresh"))
  52. }
  53. return categoryID
  54. }