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/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. userID := request.UserID(r)
  25. categoryID := request.RouteInt64Param(r, "categoryID")
  26. printer := locale.NewPrinter(request.UserLanguage(r))
  27. sess := session.New(h.store, request.SessionID(r))
  28. // Avoid accidental and excessive refreshes.
  29. if time.Now().UTC().Unix()-request.LastForceRefresh(r) < int64(config.Opts.ForceRefreshInterval())*60 {
  30. time := config.Opts.ForceRefreshInterval()
  31. sess.NewFlashErrorMessage(printer.Plural("alert.too_many_feeds_refresh", time, time))
  32. } else {
  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. jobs, err := batchBuilder.FetchJobs()
  40. if err != nil {
  41. html.ServerError(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. }