akkoma/lib/pleroma/moderation_log.ex

680 lines
19 KiB
Elixir
Raw Normal View History

2020-10-12 11:00:50 -06:00
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
2019-08-25 13:39:37 -06:00
defmodule Pleroma.ModerationLog do
use Ecto.Schema
alias Pleroma.Activity
alias Pleroma.ModerationLog
alias Pleroma.Repo
alias Pleroma.User
import Ecto.Query
schema "moderation_log" do
field(:data, :map)
timestamps()
end
2019-08-27 11:48:16 -06:00
def get_all(params) do
2019-09-01 12:23:30 -06:00
base_query =
get_all_query()
|> maybe_filter_by_date(params)
|> maybe_filter_by_user(params)
|> maybe_filter_by_search(params)
query_with_pagination = base_query |> paginate_query(params)
%{
items: Repo.all(query_with_pagination),
count: Repo.aggregate(base_query, :count, :id)
}
2019-08-27 11:48:16 -06:00
end
defp maybe_filter_by_date(query, %{start_date: nil, end_date: nil}), do: query
defp maybe_filter_by_date(query, %{start_date: start_date, end_date: nil}) do
from(q in query,
where: q.inserted_at >= ^parse_datetime(start_date)
)
end
defp maybe_filter_by_date(query, %{start_date: nil, end_date: end_date}) do
from(q in query,
where: q.inserted_at <= ^parse_datetime(end_date)
)
end
defp maybe_filter_by_date(query, %{start_date: start_date, end_date: end_date}) do
from(q in query,
where: q.inserted_at >= ^parse_datetime(start_date),
where: q.inserted_at <= ^parse_datetime(end_date)
)
end
2019-08-30 15:57:15 -06:00
defp maybe_filter_by_user(query, %{user_id: nil}), do: query
defp maybe_filter_by_user(query, %{user_id: user_id}) do
from(q in query,
where: fragment("(?)->'actor'->>'id' = ?", q.data, ^user_id)
)
end
defp maybe_filter_by_search(query, %{search: search}) when is_nil(search) or search == "",
do: query
defp maybe_filter_by_search(query, %{search: search}) do
from(q in query,
where: fragment("(?)->>'message' ILIKE ?", q.data, ^"%#{search}%")
)
end
2019-09-01 12:23:30 -06:00
defp paginate_query(query, %{page: page, page_size: page_size}) do
from(q in query,
2019-08-25 13:39:37 -06:00
limit: ^page_size,
offset: ^((page - 1) * page_size)
)
2019-08-27 11:48:16 -06:00
end
2019-09-01 12:23:30 -06:00
defp get_all_query do
from(q in __MODULE__,
order_by: [desc: q.inserted_at]
)
end
2019-08-27 11:48:16 -06:00
defp parse_datetime(datetime) do
{:ok, parsed_datetime, _} = DateTime.from_iso8601(datetime)
parsed_datetime
2019-08-25 13:39:37 -06:00
end
@spec insert_log(%{actor: User, subject: [User], action: String.t(), permission: String.t()}) ::
2019-08-30 15:57:15 -06:00
{:ok, ModerationLog} | {:error, any}
2019-08-25 13:39:37 -06:00
def insert_log(%{
actor: %User{} = actor,
subject: subjects,
2019-08-25 13:39:37 -06:00
action: action,
permission: permission
}) do
2019-08-30 15:57:15 -06:00
%ModerationLog{
2019-08-25 13:39:37 -06:00
data: %{
2019-08-30 15:57:15 -06:00
"actor" => user_to_map(actor),
"subject" => user_to_map(subjects),
2019-08-30 15:57:15 -06:00
"action" => action,
"permission" => permission,
"message" => ""
2019-08-25 13:39:37 -06:00
}
2019-08-30 15:57:15 -06:00
}
|> insert_log_entry_with_message()
2019-08-25 13:39:37 -06:00
end
2019-08-30 15:57:15 -06:00
@spec insert_log(%{actor: User, subject: User, action: String.t()}) ::
{:ok, ModerationLog} | {:error, any}
2019-08-25 13:39:37 -06:00
def insert_log(%{
actor: %User{} = actor,
action: "report_update",
subject: %Activity{data: %{"type" => "Flag"}} = subject
}) do
2019-08-30 15:57:15 -06:00
%ModerationLog{
2019-08-25 13:39:37 -06:00
data: %{
2019-08-30 15:57:15 -06:00
"actor" => user_to_map(actor),
"action" => "report_update",
"subject" => report_to_map(subject),
"message" => ""
2019-08-25 13:39:37 -06:00
}
2019-08-30 15:57:15 -06:00
}
|> insert_log_entry_with_message()
2019-08-25 13:39:37 -06:00
end
2019-08-30 15:57:15 -06:00
@spec insert_log(%{actor: User, subject: Activity, action: String.t(), text: String.t()}) ::
{:ok, ModerationLog} | {:error, any}
2019-08-25 13:39:37 -06:00
def insert_log(%{
actor: %User{} = actor,
2019-12-08 01:27:23 -07:00
action: "report_note",
2019-08-25 13:39:37 -06:00
subject: %Activity{} = subject,
text: text
}) do
2019-08-30 15:57:15 -06:00
%ModerationLog{
2019-08-25 13:39:37 -06:00
data: %{
2019-08-30 15:57:15 -06:00
"actor" => user_to_map(actor),
2019-12-08 01:27:23 -07:00
"action" => "report_note",
2019-08-30 15:57:15 -06:00
"subject" => report_to_map(subject),
2019-12-08 01:27:23 -07:00
"text" => text
}
}
|> insert_log_entry_with_message()
end
@spec insert_log(%{actor: User, subject: Activity, action: String.t(), text: String.t()}) ::
{:ok, ModerationLog} | {:error, any}
def insert_log(%{
actor: %User{} = actor,
action: "report_note_delete",
subject: %Activity{} = subject,
text: text
}) do
%ModerationLog{
data: %{
"actor" => user_to_map(actor),
"action" => "report_note_delete",
"subject" => report_to_map(subject),
"text" => text
2019-08-25 13:39:37 -06:00
}
2019-08-30 15:57:15 -06:00
}
|> insert_log_entry_with_message()
2019-08-25 13:39:37 -06:00
end
2019-08-30 15:57:15 -06:00
@spec insert_log(%{
actor: User,
subject: Activity,
action: String.t(),
sensitive: String.t(),
visibility: String.t()
}) :: {:ok, ModerationLog} | {:error, any}
2019-08-25 13:39:37 -06:00
def insert_log(%{
actor: %User{} = actor,
action: "status_update",
subject: %Activity{} = subject,
sensitive: sensitive,
visibility: visibility
}) do
2019-08-30 15:57:15 -06:00
%ModerationLog{
2019-08-25 13:39:37 -06:00
data: %{
2019-08-30 15:57:15 -06:00
"actor" => user_to_map(actor),
"action" => "status_update",
"subject" => status_to_map(subject),
"sensitive" => sensitive,
"visibility" => visibility,
"message" => ""
2019-08-25 13:39:37 -06:00
}
2019-08-30 15:57:15 -06:00
}
|> insert_log_entry_with_message()
2019-08-25 13:39:37 -06:00
end
2019-08-30 15:57:15 -06:00
@spec insert_log(%{actor: User, action: String.t(), subject_id: String.t()}) ::
{:ok, ModerationLog} | {:error, any}
2019-08-25 13:39:37 -06:00
def insert_log(%{
actor: %User{} = actor,
action: "status_delete",
subject_id: subject_id
}) do
2019-08-30 15:57:15 -06:00
%ModerationLog{
2019-08-25 13:39:37 -06:00
data: %{
2019-08-30 15:57:15 -06:00
"actor" => user_to_map(actor),
"action" => "status_delete",
"subject_id" => subject_id,
"message" => ""
2019-08-25 13:39:37 -06:00
}
2019-08-30 15:57:15 -06:00
}
|> insert_log_entry_with_message()
2019-08-25 13:39:37 -06:00
end
@spec insert_log(%{actor: User, subject: User, action: String.t()}) ::
{:ok, ModerationLog} | {:error, any}
def insert_log(%{actor: %User{} = actor, subject: subject, action: action}) do
2019-08-30 15:57:15 -06:00
%ModerationLog{
2019-08-25 13:39:37 -06:00
data: %{
2019-08-30 15:57:15 -06:00
"actor" => user_to_map(actor),
"action" => action,
"subject" => user_to_map(subject),
"message" => ""
2019-08-25 13:39:37 -06:00
}
2019-08-30 15:57:15 -06:00
}
|> insert_log_entry_with_message()
2019-08-25 13:39:37 -06:00
end
@spec insert_log(%{actor: User, subjects: [User], action: String.t()}) ::
{:ok, ModerationLog} | {:error, any}
def insert_log(%{actor: %User{} = actor, subjects: subjects, action: action}) do
subjects = Enum.map(subjects, &user_to_map/1)
2019-08-30 15:57:15 -06:00
%ModerationLog{
2019-08-25 13:39:37 -06:00
data: %{
2019-08-30 15:57:15 -06:00
"actor" => user_to_map(actor),
"action" => action,
"subjects" => subjects,
"message" => ""
2019-08-25 13:39:37 -06:00
}
2019-08-30 15:57:15 -06:00
}
|> insert_log_entry_with_message()
2019-08-25 13:39:37 -06:00
end
2019-08-30 15:57:15 -06:00
@spec insert_log(%{actor: User, action: String.t(), followed: User, follower: User}) ::
{:ok, ModerationLog} | {:error, any}
2019-08-25 13:39:37 -06:00
def insert_log(%{
actor: %User{} = actor,
followed: %User{} = followed,
follower: %User{} = follower,
action: "follow"
}) do
2019-08-30 15:57:15 -06:00
%ModerationLog{
2019-08-25 13:39:37 -06:00
data: %{
2019-08-30 15:57:15 -06:00
"actor" => user_to_map(actor),
"action" => "follow",
"followed" => user_to_map(followed),
"follower" => user_to_map(follower),
"message" => ""
2019-08-25 13:39:37 -06:00
}
2019-08-30 15:57:15 -06:00
}
|> insert_log_entry_with_message()
2019-08-25 13:39:37 -06:00
end
2019-08-30 15:57:15 -06:00
@spec insert_log(%{actor: User, action: String.t(), followed: User, follower: User}) ::
{:ok, ModerationLog} | {:error, any}
2019-08-25 13:39:37 -06:00
def insert_log(%{
actor: %User{} = actor,
followed: %User{} = followed,
follower: %User{} = follower,
action: "unfollow"
}) do
2019-08-30 15:57:15 -06:00
%ModerationLog{
2019-08-25 13:39:37 -06:00
data: %{
2019-08-30 15:57:15 -06:00
"actor" => user_to_map(actor),
"action" => "unfollow",
"followed" => user_to_map(followed),
"follower" => user_to_map(follower),
"message" => ""
2019-08-25 13:39:37 -06:00
}
2019-08-30 15:57:15 -06:00
}
|> insert_log_entry_with_message()
2019-08-25 13:39:37 -06:00
end
2019-08-30 16:09:48 -06:00
@spec insert_log(%{
actor: User,
action: String.t(),
nicknames: [String.t()],
tags: [String.t()]
}) :: {:ok, ModerationLog} | {:error, any}
2019-08-25 13:39:37 -06:00
def insert_log(%{
actor: %User{} = actor,
nicknames: nicknames,
tags: tags,
action: action
}) do
2019-08-30 15:57:15 -06:00
%ModerationLog{
2019-08-25 13:39:37 -06:00
data: %{
2019-08-30 15:57:15 -06:00
"actor" => user_to_map(actor),
"nicknames" => nicknames,
"tags" => tags,
"action" => action,
"message" => ""
2019-08-25 13:39:37 -06:00
}
2019-08-30 15:57:15 -06:00
}
|> insert_log_entry_with_message()
2019-08-25 13:39:37 -06:00
end
2019-08-30 15:57:15 -06:00
@spec insert_log(%{actor: User, action: String.t(), target: String.t()}) ::
{:ok, ModerationLog} | {:error, any}
2019-08-25 13:39:37 -06:00
def insert_log(%{
actor: %User{} = actor,
action: action,
target: target
})
when action in ["relay_follow", "relay_unfollow"] do
2019-08-30 15:57:15 -06:00
%ModerationLog{
2019-08-25 13:39:37 -06:00
data: %{
2019-08-30 15:57:15 -06:00
"actor" => user_to_map(actor),
"action" => action,
"target" => target,
"message" => ""
2019-08-25 13:39:37 -06:00
}
2019-08-30 15:57:15 -06:00
}
|> insert_log_entry_with_message()
end
2020-08-31 18:56:05 -06:00
@spec insert_log(%{actor: User, action: String.t(), subject_id: String.t()}) ::
{:ok, ModerationLog} | {:error, any}
def insert_log(%{actor: %User{} = actor, action: "chat_message_delete", subject_id: subject_id}) do
%ModerationLog{
data: %{
"actor" => %{"nickname" => actor.nickname},
"action" => "chat_message_delete",
"subject_id" => subject_id
}
}
|> insert_log_entry_with_message()
end
2019-08-30 15:57:15 -06:00
@spec insert_log_entry_with_message(ModerationLog) :: {:ok, ModerationLog} | {:error, any}
defp insert_log_entry_with_message(entry) do
entry.data["message"]
|> put_in(get_log_entry_message(entry))
|> Repo.insert()
2019-08-25 13:39:37 -06:00
end
defp user_to_map(users) when is_list(users) do
users |> Enum.map(&user_to_map/1)
end
2019-08-25 13:39:37 -06:00
defp user_to_map(%User{} = user) do
user
|> Map.from_struct()
|> Map.take([:id, :nickname])
2019-08-30 15:57:15 -06:00
|> Map.new(fn {k, v} -> {Atom.to_string(k), v} end)
|> Map.put("type", "user")
2019-08-25 13:39:37 -06:00
end
defp report_to_map(%Activity{} = report) do
%{
2019-08-30 15:57:15 -06:00
"type" => "report",
"id" => report.id,
"state" => report.data["state"]
2019-08-25 13:39:37 -06:00
}
end
defp status_to_map(%Activity{} = status) do
%{
2019-08-30 15:57:15 -06:00
"type" => "status",
"id" => status.id
2019-08-25 13:39:37 -06:00
}
end
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => action,
"followed" => %{"nickname" => followed_nickname},
"follower" => %{"nickname" => follower_nickname}
}
}) do
"@#{actor_nickname} made @#{follower_nickname} #{action} @#{followed_nickname}"
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "delete",
"subject" => subjects
2019-08-25 13:39:37 -06:00
}
}) do
"@#{actor_nickname} deleted users: #{users_to_nicknames_string(subjects)}"
2019-08-25 13:39:37 -06:00
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "create",
"subjects" => subjects
}
}) do
"@#{actor_nickname} created users: #{users_to_nicknames_string(subjects)}"
2019-08-25 13:39:37 -06:00
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "activate",
"subject" => users
2019-08-25 13:39:37 -06:00
}
}) do
"@#{actor_nickname} activated users: #{users_to_nicknames_string(users)}"
2019-08-25 13:39:37 -06:00
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "deactivate",
"subject" => users
2019-08-25 13:39:37 -06:00
}
}) do
"@#{actor_nickname} deactivated users: #{users_to_nicknames_string(users)}"
2019-08-25 13:39:37 -06:00
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "approve",
"subject" => users
}
}) do
"@#{actor_nickname} approved users: #{users_to_nicknames_string(users)}"
end
2019-08-25 13:39:37 -06:00
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"nicknames" => nicknames,
"tags" => tags,
"action" => "tag"
}
}) do
tags_string = tags |> Enum.join(", ")
"@#{actor_nickname} added tags: #{tags_string} to users: #{nicknames_to_string(nicknames)}"
2019-08-25 13:39:37 -06:00
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"nicknames" => nicknames,
"tags" => tags,
"action" => "untag"
}
}) do
tags_string = tags |> Enum.join(", ")
"@#{actor_nickname} removed tags: #{tags_string} from users: #{nicknames_to_string(nicknames)}"
2019-08-25 13:39:37 -06:00
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "grant",
"subject" => users,
2019-08-25 13:39:37 -06:00
"permission" => permission
}
}) do
"@#{actor_nickname} made #{users_to_nicknames_string(users)} #{permission}"
2019-08-25 13:39:37 -06:00
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "revoke",
"subject" => users,
2019-08-25 13:39:37 -06:00
"permission" => permission
}
}) do
"@#{actor_nickname} revoked #{permission} role from #{users_to_nicknames_string(users)}"
2019-08-25 13:39:37 -06:00
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "relay_follow",
"target" => target
}
}) do
"@#{actor_nickname} followed relay: #{target}"
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "relay_unfollow",
"target" => target
}
}) do
"@#{actor_nickname} unfollowed relay: #{target}"
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "report_update",
"subject" => %{"id" => subject_id, "state" => state, "type" => "report"}
}
}) do
"@#{actor_nickname} updated report ##{subject_id} with '#{state}' state"
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
2019-12-08 01:27:23 -07:00
"action" => "report_note",
"subject" => %{"id" => subject_id, "type" => "report"},
"text" => text
}
}) do
"@#{actor_nickname} added note '#{text}' to report ##{subject_id}"
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "report_note_delete",
2019-08-25 13:39:37 -06:00
"subject" => %{"id" => subject_id, "type" => "report"},
"text" => text
}
}) do
2019-12-08 01:27:23 -07:00
"@#{actor_nickname} deleted note '#{text}' from report ##{subject_id}"
2019-08-25 13:39:37 -06:00
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "status_update",
"subject" => %{"id" => subject_id, "type" => "status"},
"sensitive" => nil,
"visibility" => visibility
}
}) do
"@#{actor_nickname} updated status ##{subject_id}, set visibility: '#{visibility}'"
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "status_update",
"subject" => %{"id" => subject_id, "type" => "status"},
"sensitive" => sensitive,
"visibility" => nil
}
}) do
"@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}'"
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "status_update",
"subject" => %{"id" => subject_id, "type" => "status"},
"sensitive" => sensitive,
"visibility" => visibility
}
}) do
"@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}', visibility: '#{
visibility
}'"
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "status_delete",
"subject_id" => subject_id
}
}) do
"@#{actor_nickname} deleted status ##{subject_id}"
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "force_password_reset",
"subject" => subjects
}
}) do
"@#{actor_nickname} forced password reset for users: #{users_to_nicknames_string(subjects)}"
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "confirm_email",
"subject" => subjects
}
}) do
"@#{actor_nickname} confirmed email for users: #{users_to_nicknames_string(subjects)}"
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "resend_confirmation_email",
"subject" => subjects
}
}) do
"@#{actor_nickname} re-sent confirmation email for users: #{
users_to_nicknames_string(subjects)
}"
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "updated_users",
"subject" => subjects
}
}) do
"@#{actor_nickname} updated users: #{users_to_nicknames_string(subjects)}"
end
2020-08-31 18:56:05 -06:00
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "chat_message_delete",
"subject_id" => subject_id
}
}) do
"@#{actor_nickname} deleted chat message ##{subject_id}"
end
2020-09-26 13:16:56 -06:00
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}")
|> Enum.join(", ")
end
defp users_to_nicknames_string(users) do
users
|> Enum.map(&"@#{&1["nickname"]}")
|> Enum.join(", ")
end
2019-08-25 13:39:37 -06:00
end