category_refresh.go 2.2 KB

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