1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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()
22 |> distinct([a], true)
23 |> select([a], fragment("split_part(?, '/', 3)", a.actor))
28 |> Enum.each(fn {instance, i} ->
29 counters = instance_counters(instance)
30 CounterCache.set(instance, counters)
32 Mix.Pleroma.shell_info(
33 "[#{i}/#{length(instances)}] Setting #{instance} counters: #{inspect(counters)}"
37 Mix.Pleroma.shell_info("Done")
40 defp instance_counters(instance) do
41 counters = %{"public" => 0, "unlisted" => 0, "private" => 0, "direct" => 0}
44 |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
45 |> where([a], fragment("split_part(?, '/', 3) = ?", a.actor, ^instance))
49 "activity_visibility(?, ?, ?)",
58 "activity_visibility(?, ?, ?)",
64 |> Repo.all(timeout: :timer.minutes(30))
65 |> Enum.reduce(counters, fn {visibility, count}, acc ->
66 Map.put(acc, visibility, count)