category_refresh.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/http/request"
  9. "miniflux.app/v2/internal/http/response/html"
  10. "miniflux.app/v2/internal/http/route"
  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. html.Redirect(w, r, route.Path(h.router, "categoryEntries", "categoryID", categoryID))
  17. }
  18. func (h *handler) refreshCategoryFeedsPage(w http.ResponseWriter, r *http.Request) {
  19. categoryID := h.refreshCategory(w, r)
  20. html.Redirect(w, r, route.Path(h.router, "categoryFeeds", "categoryID", categoryID))
  21. }
  22. func (h *handler) refreshCategory(w http.ResponseWriter, r *http.Request) int64 {
  23. userID := request.UserID(r)
  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.Now().UTC().Unix()-request.LastForceRefresh(r) < 1800 {
  29. sess.NewFlashErrorMessage(printer.Printf("alert.too_many_feeds_refresh"))
  30. } else {
  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. jobs, err := batchBuilder.FetchJobs()
  38. if err != nil {
  39. html.ServerError(w, r, err)
  40. return 0
  41. }
  42. slog.Info(
  43. "Triggered a manual refresh of all feeds for a given category from the web ui",
  44. slog.Int64("user_id", userID),
  45. slog.Int64("category_id", categoryID),
  46. slog.Int("nb_jobs", len(jobs)),
  47. )
  48. go h.pool.Push(jobs)
  49. sess.SetLastForceRefresh()
  50. sess.NewFlashMessage(printer.Printf("alert.background_feed_refresh"))
  51. }
  52. return categoryID
  53. }