Merge branch 'static-accept-missing' into 'develop'
[akkoma] / lib / mix / tasks / pleroma / refresh_counter_cache.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.RefreshCounterCache do
6 @shortdoc "Refreshes counter cache"
7
8 use Mix.Task
9
10 alias Pleroma.Activity
11 alias Pleroma.CounterCache
12 alias Pleroma.Repo
13
14 require Logger
15 import Ecto.Query
16
17 def run([]) do
18 Mix.Pleroma.start_pleroma()
19
20 ["public", "unlisted", "private", "direct"]
21 |> Enum.each(fn visibility ->
22 count = status_visibility_count_query(visibility)
23 name = "status_visibility_#{visibility}"
24 CounterCache.set(name, count)
25 Mix.Pleroma.shell_info("Set #{name} to #{count}")
26 end)
27
28 Mix.Pleroma.shell_info("Done")
29 end
30
31 defp status_visibility_count_query(visibility) do
32 Activity
33 |> where(
34 [a],
35 fragment(
36 "activity_visibility(?, ?, ?) = ?",
37 a.actor,
38 a.recipients,
39 a.data,
40 ^visibility
41 )
42 )
43 |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
44 |> Repo.aggregate(:count, :id, timeout: :timer.minutes(30))
45 end
46 end