1 defmodule Pleroma.ModerationLog do
5 alias Pleroma.ModerationLog
11 schema "moderation_log" do
17 def get_all(params) do
20 |> maybe_filter_by_date(params)
21 |> maybe_filter_by_user(params)
22 |> maybe_filter_by_search(params)
24 query_with_pagination = base_query |> paginate_query(params)
27 items: Repo.all(query_with_pagination),
28 count: Repo.aggregate(base_query, :count, :id)
32 defp maybe_filter_by_date(query, %{start_date: nil, end_date: nil}), do: query
34 defp maybe_filter_by_date(query, %{start_date: start_date, end_date: nil}) do
36 where: q.inserted_at >= ^parse_datetime(start_date)
40 defp maybe_filter_by_date(query, %{start_date: nil, end_date: end_date}) do
42 where: q.inserted_at <= ^parse_datetime(end_date)
46 defp maybe_filter_by_date(query, %{start_date: start_date, end_date: end_date}) do
48 where: q.inserted_at >= ^parse_datetime(start_date),
49 where: q.inserted_at <= ^parse_datetime(end_date)
53 defp maybe_filter_by_user(query, %{user_id: nil}), do: query
55 defp maybe_filter_by_user(query, %{user_id: user_id}) do
57 where: fragment("(?)->'actor'->>'id' = ?", q.data, ^user_id)
61 defp maybe_filter_by_search(query, %{search: search}) when is_nil(search) or search == "",
64 defp maybe_filter_by_search(query, %{search: search}) do
66 where: fragment("(?)->>'message' ILIKE ?", q.data, ^"%#{search}%")
70 defp paginate_query(query, %{page: page, page_size: page_size}) do
73 offset: ^((page - 1) * page_size)
79 order_by: [desc: q.inserted_at]
83 defp parse_datetime(datetime) do
84 {:ok, parsed_datetime, _} = DateTime.from_iso8601(datetime)
89 @spec insert_log(%{actor: User, subject: [User], action: String.t(), permission: String.t()}) ::
90 {:ok, ModerationLog} | {:error, any}
92 actor: %User{} = actor,
95 permission: permission
99 "actor" => user_to_map(actor),
100 "subject" => user_to_map(subjects),
102 "permission" => permission,
106 |> insert_log_entry_with_message()
109 @spec insert_log(%{actor: User, subject: User, action: String.t()}) ::
110 {:ok, ModerationLog} | {:error, any}
112 actor: %User{} = actor,
113 action: "report_update",
114 subject: %Activity{data: %{"type" => "Flag"}} = subject
118 "actor" => user_to_map(actor),
119 "action" => "report_update",
120 "subject" => report_to_map(subject),
124 |> insert_log_entry_with_message()
127 @spec insert_log(%{actor: User, subject: Activity, action: String.t(), text: String.t()}) ::
128 {:ok, ModerationLog} | {:error, any}
130 actor: %User{} = actor,
131 action: "report_note",
132 subject: %Activity{} = subject,
137 "actor" => user_to_map(actor),
138 "action" => "report_note",
139 "subject" => report_to_map(subject),
143 |> insert_log_entry_with_message()
146 @spec insert_log(%{actor: User, subject: Activity, action: String.t(), text: String.t()}) ::
147 {:ok, ModerationLog} | {:error, any}
149 actor: %User{} = actor,
150 action: "report_note_delete",
151 subject: %Activity{} = subject,
156 "actor" => user_to_map(actor),
157 "action" => "report_note_delete",
158 "subject" => report_to_map(subject),
162 |> insert_log_entry_with_message()
169 sensitive: String.t(),
170 visibility: String.t()
171 }) :: {:ok, ModerationLog} | {:error, any}
173 actor: %User{} = actor,
174 action: "status_update",
175 subject: %Activity{} = subject,
176 sensitive: sensitive,
177 visibility: visibility
181 "actor" => user_to_map(actor),
182 "action" => "status_update",
183 "subject" => status_to_map(subject),
184 "sensitive" => sensitive,
185 "visibility" => visibility,
189 |> insert_log_entry_with_message()
192 @spec insert_log(%{actor: User, action: String.t(), subject_id: String.t()}) ::
193 {:ok, ModerationLog} | {:error, any}
195 actor: %User{} = actor,
196 action: "status_delete",
197 subject_id: subject_id
201 "actor" => user_to_map(actor),
202 "action" => "status_delete",
203 "subject_id" => subject_id,
207 |> insert_log_entry_with_message()
210 @spec insert_log(%{actor: User, subject: User, action: String.t()}) ::
211 {:ok, ModerationLog} | {:error, any}
212 def insert_log(%{actor: %User{} = actor, subject: subject, action: action}) do
215 "actor" => user_to_map(actor),
217 "subject" => user_to_map(subject),
221 |> insert_log_entry_with_message()
224 @spec insert_log(%{actor: User, subjects: [User], action: String.t()}) ::
225 {:ok, ModerationLog} | {:error, any}
226 def insert_log(%{actor: %User{} = actor, subjects: subjects, action: action}) do
227 subjects = Enum.map(subjects, &user_to_map/1)
231 "actor" => user_to_map(actor),
233 "subjects" => subjects,
237 |> insert_log_entry_with_message()
240 @spec insert_log(%{actor: User, action: String.t(), followed: User, follower: User}) ::
241 {:ok, ModerationLog} | {:error, any}
243 actor: %User{} = actor,
244 followed: %User{} = followed,
245 follower: %User{} = follower,
250 "actor" => user_to_map(actor),
251 "action" => "follow",
252 "followed" => user_to_map(followed),
253 "follower" => user_to_map(follower),
257 |> insert_log_entry_with_message()
260 @spec insert_log(%{actor: User, action: String.t(), followed: User, follower: User}) ::
261 {:ok, ModerationLog} | {:error, any}
263 actor: %User{} = actor,
264 followed: %User{} = followed,
265 follower: %User{} = follower,
270 "actor" => user_to_map(actor),
271 "action" => "unfollow",
272 "followed" => user_to_map(followed),
273 "follower" => user_to_map(follower),
277 |> insert_log_entry_with_message()
283 nicknames: [String.t()],
285 }) :: {:ok, ModerationLog} | {:error, any}
287 actor: %User{} = actor,
288 nicknames: nicknames,
294 "actor" => user_to_map(actor),
295 "nicknames" => nicknames,
301 |> insert_log_entry_with_message()
304 @spec insert_log(%{actor: User, action: String.t(), target: String.t()}) ::
305 {:ok, ModerationLog} | {:error, any}
307 actor: %User{} = actor,
311 when action in ["relay_follow", "relay_unfollow"] do
314 "actor" => user_to_map(actor),
320 |> insert_log_entry_with_message()
323 @spec insert_log_entry_with_message(ModerationLog) :: {:ok, ModerationLog} | {:error, any}
324 defp insert_log_entry_with_message(entry) do
325 entry.data["message"]
326 |> put_in(get_log_entry_message(entry))
330 defp user_to_map(users) when is_list(users) do
331 users |> Enum.map(&user_to_map/1)
334 defp user_to_map(%User{} = user) do
337 |> Map.take([:id, :nickname])
338 |> Map.new(fn {k, v} -> {Atom.to_string(k), v} end)
339 |> Map.put("type", "user")
342 defp report_to_map(%Activity{} = report) do
346 "state" => report.data["state"]
350 defp status_to_map(%Activity{} = status) do
357 def get_log_entry_message(%ModerationLog{
359 "actor" => %{"nickname" => actor_nickname},
361 "followed" => %{"nickname" => followed_nickname},
362 "follower" => %{"nickname" => follower_nickname}
365 "@#{actor_nickname} made @#{follower_nickname} #{action} @#{followed_nickname}"
368 @spec get_log_entry_message(ModerationLog) :: String.t()
369 def get_log_entry_message(%ModerationLog{
371 "actor" => %{"nickname" => actor_nickname},
372 "action" => "delete",
373 "subject" => subjects
376 "@#{actor_nickname} deleted users: #{users_to_nicknames_string(subjects)}"
379 @spec get_log_entry_message(ModerationLog) :: String.t()
380 def get_log_entry_message(%ModerationLog{
382 "actor" => %{"nickname" => actor_nickname},
383 "action" => "create",
384 "subjects" => subjects
387 "@#{actor_nickname} created users: #{users_to_nicknames_string(subjects)}"
390 @spec get_log_entry_message(ModerationLog) :: String.t()
391 def get_log_entry_message(%ModerationLog{
393 "actor" => %{"nickname" => actor_nickname},
394 "action" => "activate",
398 "@#{actor_nickname} activated users: #{users_to_nicknames_string(users)}"
401 @spec get_log_entry_message(ModerationLog) :: String.t()
402 def get_log_entry_message(%ModerationLog{
404 "actor" => %{"nickname" => actor_nickname},
405 "action" => "deactivate",
409 "@#{actor_nickname} deactivated users: #{users_to_nicknames_string(users)}"
412 @spec get_log_entry_message(ModerationLog) :: String.t()
413 def get_log_entry_message(%ModerationLog{
415 "actor" => %{"nickname" => actor_nickname},
416 "action" => "approve",
420 "@#{actor_nickname} approved users: #{users_to_nicknames_string(users)}"
423 @spec get_log_entry_message(ModerationLog) :: String.t()
424 def get_log_entry_message(%ModerationLog{
426 "actor" => %{"nickname" => actor_nickname},
427 "nicknames" => nicknames,
432 tags_string = tags |> Enum.join(", ")
434 "@#{actor_nickname} added tags: #{tags_string} to users: #{nicknames_to_string(nicknames)}"
437 @spec get_log_entry_message(ModerationLog) :: String.t()
438 def get_log_entry_message(%ModerationLog{
440 "actor" => %{"nickname" => actor_nickname},
441 "nicknames" => nicknames,
446 tags_string = tags |> Enum.join(", ")
448 "@#{actor_nickname} removed tags: #{tags_string} from users: #{nicknames_to_string(nicknames)}"
451 @spec get_log_entry_message(ModerationLog) :: String.t()
452 def get_log_entry_message(%ModerationLog{
454 "actor" => %{"nickname" => actor_nickname},
457 "permission" => permission
460 "@#{actor_nickname} made #{users_to_nicknames_string(users)} #{permission}"
463 @spec get_log_entry_message(ModerationLog) :: String.t()
464 def get_log_entry_message(%ModerationLog{
466 "actor" => %{"nickname" => actor_nickname},
467 "action" => "revoke",
469 "permission" => permission
472 "@#{actor_nickname} revoked #{permission} role from #{users_to_nicknames_string(users)}"
475 @spec get_log_entry_message(ModerationLog) :: String.t()
476 def get_log_entry_message(%ModerationLog{
478 "actor" => %{"nickname" => actor_nickname},
479 "action" => "relay_follow",
483 "@#{actor_nickname} followed relay: #{target}"
486 @spec get_log_entry_message(ModerationLog) :: String.t()
487 def get_log_entry_message(%ModerationLog{
489 "actor" => %{"nickname" => actor_nickname},
490 "action" => "relay_unfollow",
494 "@#{actor_nickname} unfollowed relay: #{target}"
497 @spec get_log_entry_message(ModerationLog) :: String.t()
498 def get_log_entry_message(%ModerationLog{
500 "actor" => %{"nickname" => actor_nickname},
501 "action" => "report_update",
502 "subject" => %{"id" => subject_id, "state" => state, "type" => "report"}
505 "@#{actor_nickname} updated report ##{subject_id} with '#{state}' state"
508 @spec get_log_entry_message(ModerationLog) :: String.t()
509 def get_log_entry_message(%ModerationLog{
511 "actor" => %{"nickname" => actor_nickname},
512 "action" => "report_note",
513 "subject" => %{"id" => subject_id, "type" => "report"},
517 "@#{actor_nickname} added note '#{text}' to report ##{subject_id}"
520 @spec get_log_entry_message(ModerationLog) :: String.t()
521 def get_log_entry_message(%ModerationLog{
523 "actor" => %{"nickname" => actor_nickname},
524 "action" => "report_note_delete",
525 "subject" => %{"id" => subject_id, "type" => "report"},
529 "@#{actor_nickname} deleted note '#{text}' from report ##{subject_id}"
532 @spec get_log_entry_message(ModerationLog) :: String.t()
533 def get_log_entry_message(%ModerationLog{
535 "actor" => %{"nickname" => actor_nickname},
536 "action" => "status_update",
537 "subject" => %{"id" => subject_id, "type" => "status"},
539 "visibility" => visibility
542 "@#{actor_nickname} updated status ##{subject_id}, set visibility: '#{visibility}'"
545 @spec get_log_entry_message(ModerationLog) :: String.t()
546 def get_log_entry_message(%ModerationLog{
548 "actor" => %{"nickname" => actor_nickname},
549 "action" => "status_update",
550 "subject" => %{"id" => subject_id, "type" => "status"},
551 "sensitive" => sensitive,
555 "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}'"
558 @spec get_log_entry_message(ModerationLog) :: String.t()
559 def get_log_entry_message(%ModerationLog{
561 "actor" => %{"nickname" => actor_nickname},
562 "action" => "status_update",
563 "subject" => %{"id" => subject_id, "type" => "status"},
564 "sensitive" => sensitive,
565 "visibility" => visibility
568 "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}', visibility: '#{
573 @spec get_log_entry_message(ModerationLog) :: String.t()
574 def get_log_entry_message(%ModerationLog{
576 "actor" => %{"nickname" => actor_nickname},
577 "action" => "status_delete",
578 "subject_id" => subject_id
581 "@#{actor_nickname} deleted status ##{subject_id}"
584 @spec get_log_entry_message(ModerationLog) :: String.t()
585 def get_log_entry_message(%ModerationLog{
587 "actor" => %{"nickname" => actor_nickname},
588 "action" => "force_password_reset",
589 "subject" => subjects
592 "@#{actor_nickname} forced password reset for users: #{users_to_nicknames_string(subjects)}"
595 @spec get_log_entry_message(ModerationLog) :: String.t()
596 def get_log_entry_message(%ModerationLog{
598 "actor" => %{"nickname" => actor_nickname},
599 "action" => "confirm_email",
600 "subject" => subjects
603 "@#{actor_nickname} confirmed email for users: #{users_to_nicknames_string(subjects)}"
606 @spec get_log_entry_message(ModerationLog) :: String.t()
607 def get_log_entry_message(%ModerationLog{
609 "actor" => %{"nickname" => actor_nickname},
610 "action" => "resend_confirmation_email",
611 "subject" => subjects
614 "@#{actor_nickname} re-sent confirmation email for users: #{
615 users_to_nicknames_string(subjects)
619 @spec get_log_entry_message(ModerationLog) :: String.t()
620 def get_log_entry_message(%ModerationLog{
622 "actor" => %{"nickname" => actor_nickname},
623 "action" => "updated_users",
624 "subject" => subjects
627 "@#{actor_nickname} updated users: #{users_to_nicknames_string(subjects)}"
630 defp nicknames_to_string(nicknames) do
632 |> Enum.map(&"@#{&1}")
636 defp users_to_nicknames_string(users) do
638 |> Enum.map(&"@#{&1["nickname"]}")