Use ModerationLog instead of Logger

This commit is contained in:
Egor Kislitsyn 2020-09-26 23:16:56 +04:00
parent 8545d533dd
commit bc3db72403
No known key found for this signature in database
GPG key ID: 1B49CB15B71E7805
3 changed files with 33 additions and 3 deletions

View file

@ -651,6 +651,16 @@ def get_log_entry_message(%ModerationLog{
"@#{actor_nickname} deleted chat message ##{subject_id}" "@#{actor_nickname} deleted chat message ##{subject_id}"
end 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 defp nicknames_to_string(nicknames) do
nicknames nicknames
|> Enum.map(&"@#{&1}") |> Enum.map(&"@#{&1}")

View file

@ -686,7 +686,8 @@ def stats(conn, params) do
def create_backup(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname}) do def create_backup(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname}) do
with %User{} = user <- User.get_by_nickname(nickname), with %User{} = user <- User.get_by_nickname(nickname),
{:ok, _} <- Pleroma.Backup.create(user, admin.id) do {: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, "") json(conn, "")
end end
end end

View file

@ -2027,9 +2027,9 @@ test "by instance", %{conn: conn} do
describe "/api/pleroma/backups" do describe "/api/pleroma/backups" do
test "it creates a backup", %{conn: conn} 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) token = insert(:oauth_admin_token, user: admin)
user = insert(:user) user = %{id: user_id, nickname: user_nickname} = insert(:user)
assert "" == assert "" ==
conn conn
@ -2046,6 +2046,25 @@ test "it creates a backup", %{conn: conn} do
assert String.contains?(email.html_body, "Admin @#{admin.nickname} requested a full backup") 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) 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 end
test "it doesn't limit admins", %{conn: conn} do test "it doesn't limit admins", %{conn: conn} do