1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.RefreshCounterCache do
6 @shortdoc "Refreshes counter cache"
10 alias Pleroma.Activity
11 alias Pleroma.CounterCache
18 Mix.Pleroma.start_pleroma()
21 |> distinct([a], true)
22 |> select([a], fragment("split_part(?, '/', 3)", a.actor))
24 |> Enum.each(fn instance ->
25 counters = instance_counters(instance)
26 CounterCache.set(instance, counters)
27 Mix.Pleroma.shell_info("Setting #{instance} counters: #{inspect(counters)}")
30 Mix.Pleroma.shell_info("Done")
33 defp instance_counters(instance) do
34 counters = %{"public" => 0, "unlisted" => 0, "private" => 0, "direct" => 0}
37 |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
38 |> where([a], like(a.actor, ^"%#{instance}%"))
42 "activity_visibility(?, ?, ?)",
51 "activity_visibility(?, ?, ?)",
57 |> Repo.all(timeout: :timer.minutes(30))
58 |> Enum.reduce(counters, fn {visibility, count}, acc ->
59 Map.put(acc, visibility, count)