prune_objects can prune orphaned activities
[akkoma] / lib / mix / tasks / pleroma / refresh_counter_cache.ex
index 280201befcfd0ad0fbeabe6bac4aa51afaeeb8ba..66eed86570d7e0a0ea1f6c38a8d1fbf34710934d 100644 (file)
@@ -1,5 +1,5 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Mix.Tasks.Pleroma.RefreshCounterCache do
@@ -17,14 +17,21 @@ defmodule Mix.Tasks.Pleroma.RefreshCounterCache do
   def run([]) do
     Mix.Pleroma.start_pleroma()
 
-    Activity
-    |> distinct([a], true)
-    |> select([a], fragment("split_part(?, '/', 3)", a.actor))
-    |> Repo.all()
-    |> Enum.each(fn instance ->
+    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} ->
       counters = instance_counters(instance)
       CounterCache.set(instance, counters)
-      Mix.Pleroma.shell_info("Setting #{instance} counters: #{inspect(counters)}")
+
+      Mix.Pleroma.shell_info(
+        "[#{i}/#{length(instances)}] Setting #{instance} counters: #{inspect(counters)}"
+      )
     end)
 
     Mix.Pleroma.shell_info("Done")
@@ -35,7 +42,7 @@ defmodule Mix.Tasks.Pleroma.RefreshCounterCache do
 
     Activity
     |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
-    |> where([a], like(a.actor, ^"%#{instance}%"))
+    |> where([a], fragment("split_part(?, '/', 3) = ?", a.actor, ^instance))
     |> select(
       [a],
       {fragment(