Use ModerationLog instead of Logger
authorEgor Kislitsyn <egor@kislitsyn.com>
Sat, 26 Sep 2020 19:16:56 +0000 (23:16 +0400)
committerEgor Kislitsyn <egor@kislitsyn.com>
Wed, 7 Oct 2020 14:35:29 +0000 (18:35 +0400)
lib/pleroma/moderation_log.ex
lib/pleroma/web/admin_api/controllers/admin_api_controller.ex
test/web/admin_api/controllers/admin_api_controller_test.exs

index 47036a6f624052174543b4bec9704f49c3d4c615..be1e81467ce254f43b0338f5f74a996aaad843a8 100644 (file)
@@ -651,6 +651,16 @@ defmodule Pleroma.ModerationLog do
     "@#{actor_nickname} deleted chat message ##{subject_id}"
   end
 
+  def get_log_entry_message(%ModerationLog{
+        data: %{
+          "actor" => %{"nickname" => actor_nickname},
+          "action" => "create_backup",
+          "subject" => %{"nickname" => user_nickname}
+        }
+      }) do
+    "@#{actor_nickname} requested account backup for @#{user_nickname}"
+  end
+
   defp nicknames_to_string(nicknames) do
     nicknames
     |> Enum.map(&"@#{&1}")
index f7d2fe5b1ff2e856887b55c73dc27409d3005cb0..8b5310d8098959ae7039e8ca98c4f19fb7c4d56f 100644 (file)
@@ -686,7 +686,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
   def create_backup(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname}) do
     with %User{} = user <- User.get_by_nickname(nickname),
          {:ok, _} <- Pleroma.Backup.create(user, admin.id) do
-      Logger.info("Admin @#{admin.nickname} requested account backup for @{nickname}")
+      ModerationLog.insert_log(%{actor: admin, subject: user, action: "create_backup"})
+
       json(conn, "")
     end
   end
index a6dc4f62d3a08f5ea179963d5f59eabe42a97736..34d48c2c19d8cddfa2b5d83f38e4c719389c67d2 100644 (file)
@@ -2027,9 +2027,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
 
   describe "/api/pleroma/backups" do
     test "it creates a backup", %{conn: conn} do
-      admin = insert(:user, is_admin: true)
+      admin = %{id: admin_id, nickname: admin_nickname} = insert(:user, is_admin: true)
       token = insert(:oauth_admin_token, user: admin)
-      user = insert(:user)
+      user = %{id: user_id, nickname: user_nickname} = insert(:user)
 
       assert "" ==
                conn
@@ -2046,6 +2046,25 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
 
       assert String.contains?(email.html_body, "Admin @#{admin.nickname} requested a full backup")
       assert_email_sent(to: {user.name, user.email}, html_body: email.html_body)
+
+      log_message = "@#{admin_nickname} requested account backup for @#{user_nickname}"
+
+      assert [
+               %{
+                 data: %{
+                   "action" => "create_backup",
+                   "actor" => %{
+                     "id" => ^admin_id,
+                     "nickname" => ^admin_nickname
+                   },
+                   "message" => ^log_message,
+                   "subject" => %{
+                     "id" => ^user_id,
+                     "nickname" => ^user_nickname
+                   }
+                 }
+               }
+             ] = Pleroma.ModerationLog |> Repo.all()
     end
 
     test "it doesn't limit admins", %{conn: conn} do