Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

70 рядки
1.6KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Mix.Tasks.Pleroma.RefreshCounterCache do
  5. @shortdoc "Refreshes counter cache"
  6. use Mix.Task
  7. alias Pleroma.Activity
  8. alias Pleroma.CounterCache
  9. alias Pleroma.Repo
  10. require Logger
  11. import Ecto.Query
  12. def run([]) do
  13. Mix.Pleroma.start_pleroma()
  14. instances =
  15. Activity
  16. |> distinct([a], true)
  17. |> select([a], fragment("split_part(?, '/', 3)", a.actor))
  18. |> Repo.all()
  19. instances
  20. |> Enum.with_index(1)
  21. |> Enum.each(fn {instance, i} ->
  22. counters = instance_counters(instance)
  23. CounterCache.set(instance, counters)
  24. Mix.Pleroma.shell_info(
  25. "[#{i}/#{length(instances)}] Setting #{instance} counters: #{inspect(counters)}"
  26. )
  27. end)
  28. Mix.Pleroma.shell_info("Done")
  29. end
  30. defp instance_counters(instance) do
  31. counters = %{"public" => 0, "unlisted" => 0, "private" => 0, "direct" => 0}
  32. Activity
  33. |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
  34. |> where([a], fragment("split_part(?, '/', 3) = ?", a.actor, ^instance))
  35. |> select(
  36. [a],
  37. {fragment(
  38. "activity_visibility(?, ?, ?)",
  39. a.actor,
  40. a.recipients,
  41. a.data
  42. ), count(a.id)}
  43. )
  44. |> group_by(
  45. [a],
  46. fragment(
  47. "activity_visibility(?, ?, ?)",
  48. a.actor,
  49. a.recipients,
  50. a.data
  51. )
  52. )
  53. |> Repo.all(timeout: :timer.minutes(30))
  54. |> Enum.reduce(counters, fn {visibility, count}, acc ->
  55. Map.put(acc, visibility, count)
  56. end)
  57. end
  58. end