Add a task to re-count statuses for all users
authorEgor Kislitsyn <egor@kislitsyn.com>
Wed, 9 Oct 2019 06:11:57 +0000 (13:11 +0700)
committerEgor Kislitsyn <egor@kislitsyn.com>
Wed, 9 Oct 2019 06:11:57 +0000 (13:11 +0700)
CHANGELOG.md
lib/mix/tasks/pleroma/count_statuses.ex [new file with mode: 0644]
test/tasks/count_statuses_test.exs [new file with mode: 0644]

index 8b24db7f4b37002a15c4729e6dfaf335ebcd7912..759745dd28f0b28896b245915236f955da2dece2 100644 (file)
@@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - OAuth: support for hierarchical permissions / [Mastodon 2.4.3 OAuth permissions](https://docs.joinmastodon.org/api/permissions/)
 - Authentication: Added rate limit for password-authorized actions / login existence checks
 - Metadata Link: Atom syndication Feed
+- Mix task to re-count statuses for all users (`mix pleroma.count_statuses`)
 
 ### Changed
 - **Breaking:** Elixir >=1.8 is now required (was >= 1.7)
diff --git a/lib/mix/tasks/pleroma/count_statuses.ex b/lib/mix/tasks/pleroma/count_statuses.ex
new file mode 100644 (file)
index 0000000..e1e8195
--- /dev/null
@@ -0,0 +1,22 @@
+defmodule Mix.Tasks.Pleroma.CountStatuses do
+  @shortdoc "Re-counts statuses for all users"
+
+  use Mix.Task
+  alias Pleroma.User
+  import Ecto.Query
+
+  def run([]) do
+    Mix.Pleroma.start_pleroma()
+
+    stream =
+      User
+      |> where(local: true)
+      |> Pleroma.Repo.stream()
+
+    Pleroma.Repo.transaction(fn ->
+      Enum.each(stream, &User.update_note_count/1)
+    end)
+
+    Mix.Pleroma.shell_info("Done")
+  end
+end
diff --git a/test/tasks/count_statuses_test.exs b/test/tasks/count_statuses_test.exs
new file mode 100644 (file)
index 0000000..6035da3
--- /dev/null
@@ -0,0 +1,39 @@
+# 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.CountStatusesTest do
+  use Pleroma.DataCase
+
+  alias Pleroma.User
+  alias Pleroma.Web.CommonAPI
+
+  import ExUnit.CaptureIO, only: [capture_io: 1]
+  import Pleroma.Factory
+
+  test "counts statuses" do
+    user = insert(:user)
+    {:ok, _} = CommonAPI.post(user, %{"status" => "test"})
+    {:ok, _} = CommonAPI.post(user, %{"status" => "test2"})
+
+    user2 = insert(:user)
+    {:ok, _} = CommonAPI.post(user2, %{"status" => "test3"})
+
+    user = refresh_record(user)
+    user2 = refresh_record(user2)
+
+    assert %{info: %{note_count: 2}} = user
+    assert %{info: %{note_count: 1}} = user2
+
+    {:ok, user} = User.update_info(user, &User.Info.set_note_count(&1, 0))
+    {:ok, user2} = User.update_info(user2, &User.Info.set_note_count(&1, 0))
+
+    assert %{info: %{note_count: 0}} = user
+    assert %{info: %{note_count: 0}} = user2
+
+    assert capture_io(fn -> Mix.Tasks.Pleroma.CountStatuses.run([]) end) == "Done\n"
+
+    assert %{info: %{note_count: 2}} = refresh_record(user)
+    assert %{info: %{note_count: 1}} = refresh_record(user2)
+  end
+end