2020-01-09 12:18:55 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-01 04:11:47 -07:00
|
|
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
2020-01-09 12:18:55 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Mix.Tasks.Pleroma.RefreshCounterCache do
|
|
|
|
@shortdoc "Refreshes counter cache"
|
|
|
|
|
|
|
|
use Mix.Task
|
|
|
|
|
|
|
|
alias Pleroma.Activity
|
|
|
|
alias Pleroma.CounterCache
|
|
|
|
alias Pleroma.Repo
|
|
|
|
|
|
|
|
require Logger
|
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
def run([]) do
|
|
|
|
Mix.Pleroma.start_pleroma()
|
|
|
|
|
2020-05-09 02:43:31 -06:00
|
|
|
instances =
|
|
|
|
Activity
|
|
|
|
|> distinct([a], true)
|
|
|
|
|> select([a], fragment("split_part(?, '/', 3)", a.actor))
|
|
|
|
|> Repo.all()
|
|
|
|
|
|
|
|
instances
|
|
|
|
|> Enum.with_index(1)
|
|
|
|
|> Enum.each(fn {instance, i} ->
|
2020-05-08 16:20:50 -06:00
|
|
|
counters = instance_counters(instance)
|
|
|
|
CounterCache.set(instance, counters)
|
2020-05-09 02:43:31 -06:00
|
|
|
|
|
|
|
Mix.Pleroma.shell_info(
|
|
|
|
"[#{i}/#{length(instances)}] Setting #{instance} counters: #{inspect(counters)}"
|
|
|
|
)
|
2020-01-09 12:18:55 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
Mix.Pleroma.shell_info("Done")
|
|
|
|
end
|
|
|
|
|
2020-05-08 16:20:50 -06:00
|
|
|
defp instance_counters(instance) do
|
|
|
|
counters = %{"public" => 0, "unlisted" => 0, "private" => 0, "direct" => 0}
|
|
|
|
|
2020-01-09 12:18:55 -07:00
|
|
|
Activity
|
2020-05-08 16:20:50 -06:00
|
|
|
|> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
|
2020-05-09 04:13:26 -06:00
|
|
|
|> where([a], fragment("split_part(?, '/', 3) = ?", a.actor, ^instance))
|
2020-05-08 16:20:50 -06:00
|
|
|
|> select(
|
|
|
|
[a],
|
|
|
|
{fragment(
|
|
|
|
"activity_visibility(?, ?, ?)",
|
|
|
|
a.actor,
|
|
|
|
a.recipients,
|
|
|
|
a.data
|
|
|
|
), count(a.id)}
|
|
|
|
)
|
|
|
|
|> group_by(
|
2020-01-09 12:18:55 -07:00
|
|
|
[a],
|
|
|
|
fragment(
|
2020-05-08 16:20:50 -06:00
|
|
|
"activity_visibility(?, ?, ?)",
|
2020-01-09 12:18:55 -07:00
|
|
|
a.actor,
|
|
|
|
a.recipients,
|
2020-05-08 16:20:50 -06:00
|
|
|
a.data
|
2020-01-09 12:18:55 -07:00
|
|
|
)
|
|
|
|
)
|
2020-05-08 16:20:50 -06:00
|
|
|
|> Repo.all(timeout: :timer.minutes(30))
|
|
|
|
|> Enum.reduce(counters, fn {visibility, count}, acc ->
|
|
|
|
Map.put(acc, visibility, count)
|
|
|
|
end)
|
2020-01-09 12:18:55 -07:00
|
|
|
end
|
|
|
|
end
|