1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.ModerationLog do
9 alias Pleroma.ModerationLog
15 @type t :: %__MODULE__{}
16 @type log_subject :: Activity.t() | User.t() | list(User.t())
17 @type log_params :: %{
18 required(:actor) => User.t(),
19 required(:action) => String.t(),
20 optional(:subject) => log_subject(),
21 optional(:subject_actor) => User.t(),
22 optional(:subject_id) => String.t(),
23 optional(:subjects) => list(User.t()),
24 optional(:permission) => String.t(),
25 optional(:text) => String.t(),
26 optional(:sensitive) => String.t(),
27 optional(:visibility) => String.t(),
28 optional(:followed) => User.t(),
29 optional(:follower) => User.t(),
30 optional(:nicknames) => list(String.t()),
31 optional(:tags) => list(String.t()),
32 optional(:target) => String.t()
35 schema "moderation_log" do
41 def get_all(params) do
44 |> maybe_filter_by_date(params)
45 |> maybe_filter_by_user(params)
46 |> maybe_filter_by_search(params)
48 query_with_pagination = base_query |> paginate_query(params)
51 items: Repo.all(query_with_pagination),
52 count: Repo.aggregate(base_query, :count, :id)
56 defp maybe_filter_by_date(query, %{start_date: nil, end_date: nil}), do: query
58 defp maybe_filter_by_date(query, %{start_date: start_date, end_date: nil}) do
60 where: q.inserted_at >= ^parse_datetime(start_date)
64 defp maybe_filter_by_date(query, %{start_date: nil, end_date: end_date}) do
66 where: q.inserted_at <= ^parse_datetime(end_date)
70 defp maybe_filter_by_date(query, %{start_date: start_date, end_date: end_date}) do
72 where: q.inserted_at >= ^parse_datetime(start_date),
73 where: q.inserted_at <= ^parse_datetime(end_date)
77 defp maybe_filter_by_user(query, %{user_id: nil}), do: query
79 defp maybe_filter_by_user(query, %{user_id: user_id}) do
81 where: fragment("(?)->'actor'->>'id' = ?", q.data, ^user_id)
85 defp maybe_filter_by_search(query, %{search: search}) when is_nil(search) or search == "",
88 defp maybe_filter_by_search(query, %{search: search}) do
90 where: fragment("(?)->>'message' ILIKE ?", q.data, ^"%#{search}%")
94 defp paginate_query(query, %{page: page, page_size: page_size}) do
97 offset: ^((page - 1) * page_size)
101 defp get_all_query do
102 from(q in __MODULE__,
103 order_by: [desc: q.inserted_at]
107 defp parse_datetime(datetime) do
108 {:ok, parsed_datetime, _} = DateTime.from_iso8601(datetime)
113 defp prepare_log_data(%{actor: actor, action: action} = attrs) do
115 "actor" => user_to_map(actor),
119 |> Pleroma.Maps.put_if_present("subject_actor", user_to_map(attrs[:subject_actor]))
122 defp prepare_log_data(attrs), do: attrs
124 @spec insert_log(log_params()) :: {:ok, ModerationLog} | {:error, any}
125 def insert_log(%{actor: %User{}, subject: subjects, permission: permission} = attrs) do
129 |> Map.merge(%{"subject" => user_to_map(subjects), "permission" => permission})
131 insert_log_entry_with_message(%ModerationLog{data: data})
134 def insert_log(%{actor: %User{}, action: action, subject: %Activity{} = subject} = attrs)
135 when action in ["report_note_delete", "report_update", "report_note"] do
139 |> Pleroma.Maps.put_if_present("text", attrs[:text])
140 |> Map.merge(%{"subject" => report_to_map(subject)})
142 insert_log_entry_with_message(%ModerationLog{data: data})
149 subject: %Activity{} = subject,
150 sensitive: sensitive,
151 visibility: visibility
154 when action == "status_update" do
159 "subject" => status_to_map(subject),
160 "sensitive" => sensitive,
161 "visibility" => visibility
164 insert_log_entry_with_message(%ModerationLog{data: data})
167 def insert_log(%{actor: %User{}, action: action, subject_id: subject_id} = attrs)
168 when action == "status_delete" do
172 |> Map.merge(%{"subject_id" => subject_id})
174 insert_log_entry_with_message(%ModerationLog{data: data})
177 def insert_log(%{actor: %User{}, subject: subject, action: _action} = attrs) do
181 |> Map.merge(%{"subject" => user_to_map(subject)})
183 insert_log_entry_with_message(%ModerationLog{data: data})
186 def insert_log(%{actor: %User{}, subjects: subjects, action: _action} = attrs) do
190 |> Map.merge(%{"subjects" => user_to_map(subjects)})
192 insert_log_entry_with_message(%ModerationLog{data: data})
198 followed: %User{} = followed,
199 follower: %User{} = follower,
203 when action in ["unfollow", "follow"] do
207 |> Map.merge(%{"followed" => user_to_map(followed), "follower" => user_to_map(follower)})
209 insert_log_entry_with_message(%ModerationLog{data: data})
213 actor: %User{} = actor,
214 nicknames: nicknames,
220 "actor" => user_to_map(actor),
221 "nicknames" => nicknames,
227 |> insert_log_entry_with_message()
230 def insert_log(%{actor: %User{}, action: action, target: target} = attrs)
231 when action in ["relay_follow", "relay_unfollow"] do
235 |> Map.merge(%{"target" => target})
237 insert_log_entry_with_message(%ModerationLog{data: data})
240 def insert_log(%{actor: %User{} = actor, action: "chat_message_delete", subject_id: subject_id}) do
243 "actor" => %{"nickname" => actor.nickname},
244 "action" => "chat_message_delete",
245 "subject_id" => subject_id
248 |> insert_log_entry_with_message()
251 @spec insert_log_entry_with_message(ModerationLog) :: {:ok, ModerationLog} | {:error, any}
252 defp insert_log_entry_with_message(entry) do
253 entry.data["message"]
254 |> put_in(get_log_entry_message(entry))
258 defp user_to_map(users) when is_list(users) do
259 Enum.map(users, &user_to_map/1)
262 defp user_to_map(%User{} = user) do
264 |> Map.take([:id, :nickname])
265 |> Map.new(fn {k, v} -> {Atom.to_string(k), v} end)
266 |> Map.put("type", "user")
269 defp user_to_map(_), do: nil
271 defp report_to_map(%Activity{} = report) do
272 %{"type" => "report", "id" => report.id, "state" => report.data["state"]}
275 defp status_to_map(%Activity{} = status) do
276 %{"type" => "status", "id" => status.id}
279 @spec get_log_entry_message(ModerationLog.t()) :: String.t()
280 def get_log_entry_message(%ModerationLog{
282 "actor" => %{"nickname" => actor_nickname},
284 "followed" => %{"nickname" => followed_nickname},
285 "follower" => %{"nickname" => follower_nickname}
288 "@#{actor_nickname} made @#{follower_nickname} #{action} @#{followed_nickname}"
291 def get_log_entry_message(%ModerationLog{
293 "actor" => %{"nickname" => actor_nickname},
294 "action" => "delete",
295 "subject" => subjects
298 "@#{actor_nickname} deleted users: #{users_to_nicknames_string(subjects)}"
301 def get_log_entry_message(%ModerationLog{
303 "actor" => %{"nickname" => actor_nickname},
304 "action" => "create",
305 "subjects" => subjects
308 "@#{actor_nickname} created users: #{users_to_nicknames_string(subjects)}"
311 def get_log_entry_message(%ModerationLog{
313 "actor" => %{"nickname" => actor_nickname},
314 "action" => "activate",
318 "@#{actor_nickname} activated users: #{users_to_nicknames_string(users)}"
321 def get_log_entry_message(%ModerationLog{
323 "actor" => %{"nickname" => actor_nickname},
324 "action" => "deactivate",
328 "@#{actor_nickname} deactivated users: #{users_to_nicknames_string(users)}"
331 def get_log_entry_message(%ModerationLog{
333 "actor" => %{"nickname" => actor_nickname},
334 "action" => "approve",
338 "@#{actor_nickname} approved users: #{users_to_nicknames_string(users)}"
341 def get_log_entry_message(%ModerationLog{
343 "actor" => %{"nickname" => actor_nickname},
344 "action" => "add_suggestion",
348 "@#{actor_nickname} added suggested users: #{users_to_nicknames_string(users)}"
351 def get_log_entry_message(%ModerationLog{
353 "actor" => %{"nickname" => actor_nickname},
354 "action" => "remove_suggestion",
358 "@#{actor_nickname} removed suggested users: #{users_to_nicknames_string(users)}"
361 def get_log_entry_message(%ModerationLog{
363 "actor" => %{"nickname" => actor_nickname},
364 "nicknames" => nicknames,
369 tags_string = tags |> Enum.join(", ")
371 "@#{actor_nickname} added tags: #{tags_string} to users: #{nicknames_to_string(nicknames)}"
374 def get_log_entry_message(%ModerationLog{
376 "actor" => %{"nickname" => actor_nickname},
377 "nicknames" => nicknames,
382 tags_string = tags |> Enum.join(", ")
384 "@#{actor_nickname} removed tags: #{tags_string} from users: #{nicknames_to_string(nicknames)}"
387 def get_log_entry_message(%ModerationLog{
389 "actor" => %{"nickname" => actor_nickname},
392 "permission" => permission
395 "@#{actor_nickname} made #{users_to_nicknames_string(users)} #{permission}"
398 def get_log_entry_message(%ModerationLog{
400 "actor" => %{"nickname" => actor_nickname},
401 "action" => "revoke",
403 "permission" => permission
406 "@#{actor_nickname} revoked #{permission} role from #{users_to_nicknames_string(users)}"
409 def get_log_entry_message(%ModerationLog{
411 "actor" => %{"nickname" => actor_nickname},
412 "action" => "relay_follow",
416 "@#{actor_nickname} followed relay: #{target}"
419 def get_log_entry_message(%ModerationLog{
421 "actor" => %{"nickname" => actor_nickname},
422 "action" => "relay_unfollow",
426 "@#{actor_nickname} unfollowed relay: #{target}"
429 def get_log_entry_message(
432 "actor" => %{"nickname" => actor_nickname},
433 "action" => "report_update",
434 "subject" => %{"id" => subject_id, "state" => state, "type" => "report"}
438 "@#{actor_nickname} updated report ##{subject_id}" <>
439 subject_actor_nickname(log, " (on user ", ")") <>
440 " with '#{state}' state"
443 def get_log_entry_message(
446 "actor" => %{"nickname" => actor_nickname},
447 "action" => "report_note",
448 "subject" => %{"id" => subject_id, "type" => "report"},
453 "@#{actor_nickname} added note '#{text}' to report ##{subject_id}" <>
454 subject_actor_nickname(log, " on user ")
457 def get_log_entry_message(
460 "actor" => %{"nickname" => actor_nickname},
461 "action" => "report_note_delete",
462 "subject" => %{"id" => subject_id, "type" => "report"},
467 "@#{actor_nickname} deleted note '#{text}' from report ##{subject_id}" <>
468 subject_actor_nickname(log, " on user ")
471 def get_log_entry_message(%ModerationLog{
473 "actor" => %{"nickname" => actor_nickname},
474 "action" => "status_update",
475 "subject" => %{"id" => subject_id, "type" => "status"},
477 "visibility" => visibility
480 "@#{actor_nickname} updated status ##{subject_id}, set visibility: '#{visibility}'"
483 def get_log_entry_message(%ModerationLog{
485 "actor" => %{"nickname" => actor_nickname},
486 "action" => "status_update",
487 "subject" => %{"id" => subject_id, "type" => "status"},
488 "sensitive" => sensitive,
492 "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}'"
495 def get_log_entry_message(%ModerationLog{
497 "actor" => %{"nickname" => actor_nickname},
498 "action" => "status_update",
499 "subject" => %{"id" => subject_id, "type" => "status"},
500 "sensitive" => sensitive,
501 "visibility" => visibility
504 "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}', visibility: '#{visibility}'"
507 def get_log_entry_message(%ModerationLog{
509 "actor" => %{"nickname" => actor_nickname},
510 "action" => "status_delete",
511 "subject_id" => subject_id
514 "@#{actor_nickname} deleted status ##{subject_id}"
517 def get_log_entry_message(%ModerationLog{
519 "actor" => %{"nickname" => actor_nickname},
520 "action" => "force_password_reset",
521 "subject" => subjects
524 "@#{actor_nickname} forced password reset for users: #{users_to_nicknames_string(subjects)}"
527 def get_log_entry_message(%ModerationLog{
529 "actor" => %{"nickname" => actor_nickname},
530 "action" => "confirm_email",
531 "subject" => subjects
534 "@#{actor_nickname} confirmed email for users: #{users_to_nicknames_string(subjects)}"
537 def get_log_entry_message(%ModerationLog{
539 "actor" => %{"nickname" => actor_nickname},
540 "action" => "resend_confirmation_email",
541 "subject" => subjects
544 "@#{actor_nickname} re-sent confirmation email for users: #{users_to_nicknames_string(subjects)}"
547 def get_log_entry_message(%ModerationLog{
549 "actor" => %{"nickname" => actor_nickname},
550 "action" => "updated_users",
551 "subject" => subjects
554 "@#{actor_nickname} updated users: #{users_to_nicknames_string(subjects)}"
557 def get_log_entry_message(%ModerationLog{
559 "actor" => %{"nickname" => actor_nickname},
560 "action" => "chat_message_delete",
561 "subject_id" => subject_id
564 "@#{actor_nickname} deleted chat message ##{subject_id}"
567 def get_log_entry_message(%ModerationLog{
569 "actor" => %{"nickname" => actor_nickname},
570 "action" => "create_backup",
571 "subject" => %{"nickname" => user_nickname}
574 "@#{actor_nickname} requested account backup for @#{user_nickname}"
577 defp nicknames_to_string(nicknames) do
579 |> Enum.map(&"@#{&1}")
583 defp users_to_nicknames_string(users) do
585 |> Enum.map(&"@#{&1["nickname"]}")
589 defp subject_actor_nickname(%ModerationLog{data: data}, prefix_msg, postfix_msg \\ "") do
591 %{"subject_actor" => %{"nickname" => subject_actor}} ->
592 [prefix_msg, "@#{subject_actor}", postfix_msg]
593 |> Enum.reject(&(&1 == ""))