diff options
author | Maksim Pechnikov <parallel588@gmail.com> | 2020-02-25 07:15:33 +0300 |
---|---|---|
committer | Maksim Pechnikov <parallel588@gmail.com> | 2020-02-25 07:22:56 +0300 |
commit | 10f452ad1feae9a882b6dc4cd35e09adb7e78208 (patch) | |
tree | 62083f08fbe99cd38002d1f6896cd2fd79675320 /lib/mix/tasks/pleroma/refresh_counter_cache.ex | |
parent | 28701c08ad9219219a32f31b2ed9dcb83f92cd59 (diff) | |
parent | 035c2c1415ed46abb268cf85c141384416a799e2 (diff) | |
download | pleroma-10f452ad1feae9a882b6dc4cd35e09adb7e78208.tar.gz |
Merge branch 'develop' into issue/1276
Diffstat (limited to 'lib/mix/tasks/pleroma/refresh_counter_cache.ex')
-rw-r--r-- | lib/mix/tasks/pleroma/refresh_counter_cache.ex | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/mix/tasks/pleroma/refresh_counter_cache.ex b/lib/mix/tasks/pleroma/refresh_counter_cache.ex new file mode 100644 index 000000000..bc2571efd --- /dev/null +++ b/lib/mix/tasks/pleroma/refresh_counter_cache.ex @@ -0,0 +1,46 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> +# 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() + + ["public", "unlisted", "private", "direct"] + |> Enum.each(fn visibility -> + count = status_visibility_count_query(visibility) + name = "status_visibility_#{visibility}" + CounterCache.set(name, count) + Mix.Pleroma.shell_info("Set #{name} to #{count}") + end) + + Mix.Pleroma.shell_info("Done") + end + + defp status_visibility_count_query(visibility) do + Activity + |> where( + [a], + fragment( + "activity_visibility(?, ?, ?) = ?", + a.actor, + a.recipients, + a.data, + ^visibility + ) + ) + |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data)) + |> Repo.aggregate(:count, :id, timeout: :timer.minutes(30)) + end +end |