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_response",
132 subject: %Activity{} = subject,
137 "actor" => user_to_map(actor),
138 "action" => "report_response",
139 "subject" => report_to_map(subject),
144 |> insert_log_entry_with_message()
151 sensitive: String.t(),
152 visibility: String.t()
153 }) :: {:ok, ModerationLog} | {:error, any}
155 actor: %User{} = actor,
156 action: "status_update",
157 subject: %Activity{} = subject,
158 sensitive: sensitive,
159 visibility: visibility
163 "actor" => user_to_map(actor),
164 "action" => "status_update",
165 "subject" => status_to_map(subject),
166 "sensitive" => sensitive,
167 "visibility" => visibility,
171 |> insert_log_entry_with_message()
174 @spec insert_log(%{actor: User, action: String.t(), subject_id: String.t()}) ::
175 {:ok, ModerationLog} | {:error, any}
177 actor: %User{} = actor,
178 action: "status_delete",
179 subject_id: subject_id
183 "actor" => user_to_map(actor),
184 "action" => "status_delete",
185 "subject_id" => subject_id,
189 |> insert_log_entry_with_message()
192 @spec insert_log(%{actor: User, subject: User, action: String.t()}) ::
193 {:ok, ModerationLog} | {:error, any}
194 def insert_log(%{actor: %User{} = actor, subject: subject, action: action}) do
197 "actor" => user_to_map(actor),
199 "subject" => user_to_map(subject),
203 |> insert_log_entry_with_message()
206 @spec insert_log(%{actor: User, subjects: [User], action: String.t()}) ::
207 {:ok, ModerationLog} | {:error, any}
208 def insert_log(%{actor: %User{} = actor, subjects: subjects, action: action}) do
209 subjects = Enum.map(subjects, &user_to_map/1)
213 "actor" => user_to_map(actor),
215 "subjects" => subjects,
219 |> insert_log_entry_with_message()
222 @spec insert_log(%{actor: User, action: String.t(), followed: User, follower: User}) ::
223 {:ok, ModerationLog} | {:error, any}
225 actor: %User{} = actor,
226 followed: %User{} = followed,
227 follower: %User{} = follower,
232 "actor" => user_to_map(actor),
233 "action" => "follow",
234 "followed" => user_to_map(followed),
235 "follower" => user_to_map(follower),
239 |> insert_log_entry_with_message()
242 @spec insert_log(%{actor: User, action: String.t(), followed: User, follower: User}) ::
243 {:ok, ModerationLog} | {:error, any}
245 actor: %User{} = actor,
246 followed: %User{} = followed,
247 follower: %User{} = follower,
252 "actor" => user_to_map(actor),
253 "action" => "unfollow",
254 "followed" => user_to_map(followed),
255 "follower" => user_to_map(follower),
259 |> insert_log_entry_with_message()
265 nicknames: [String.t()],
267 }) :: {:ok, ModerationLog} | {:error, any}
269 actor: %User{} = actor,
270 nicknames: nicknames,
276 "actor" => user_to_map(actor),
277 "nicknames" => nicknames,
283 |> insert_log_entry_with_message()
286 @spec insert_log(%{actor: User, action: String.t(), target: String.t()}) ::
287 {:ok, ModerationLog} | {:error, any}
289 actor: %User{} = actor,
293 when action in ["relay_follow", "relay_unfollow"] do
296 "actor" => user_to_map(actor),
302 |> insert_log_entry_with_message()
305 @spec insert_log_entry_with_message(ModerationLog) :: {:ok, ModerationLog} | {:error, any}
306 defp insert_log_entry_with_message(entry) do
307 entry.data["message"]
308 |> put_in(get_log_entry_message(entry))
312 defp user_to_map(users) when is_list(users) do
313 users |> Enum.map(&user_to_map/1)
316 defp user_to_map(%User{} = user) do
319 |> Map.take([:id, :nickname])
320 |> Map.new(fn {k, v} -> {Atom.to_string(k), v} end)
321 |> Map.put("type", "user")
324 defp report_to_map(%Activity{} = report) do
328 "state" => report.data["state"]
332 defp status_to_map(%Activity{} = status) do
339 def get_log_entry_message(%ModerationLog{
341 "actor" => %{"nickname" => actor_nickname},
343 "followed" => %{"nickname" => followed_nickname},
344 "follower" => %{"nickname" => follower_nickname}
347 "@#{actor_nickname} made @#{follower_nickname} #{action} @#{followed_nickname}"
350 @spec get_log_entry_message(ModerationLog) :: String.t()
351 def get_log_entry_message(%ModerationLog{
353 "actor" => %{"nickname" => actor_nickname},
354 "action" => "delete",
355 "subject" => subjects
358 "@#{actor_nickname} deleted users: #{users_to_nicknames_string(subjects)}"
361 @spec get_log_entry_message(ModerationLog) :: String.t()
362 def get_log_entry_message(%ModerationLog{
364 "actor" => %{"nickname" => actor_nickname},
365 "action" => "create",
366 "subjects" => subjects
369 "@#{actor_nickname} created users: #{users_to_nicknames_string(subjects)}"
372 @spec get_log_entry_message(ModerationLog) :: String.t()
373 def get_log_entry_message(%ModerationLog{
375 "actor" => %{"nickname" => actor_nickname},
376 "action" => "activate",
381 get_log_entry_message(%ModerationLog{
383 "actor" => %{"nickname" => actor_nickname},
384 "action" => "activate",
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",
410 get_log_entry_message(%ModerationLog{
412 "actor" => %{"nickname" => actor_nickname},
413 "action" => "deactivate",
419 @spec get_log_entry_message(ModerationLog) :: String.t()
420 def get_log_entry_message(%ModerationLog{
422 "actor" => %{"nickname" => actor_nickname},
423 "action" => "deactivate",
427 "@#{actor_nickname} deactivated users: #{users_to_nicknames_string(users)}"
430 @spec get_log_entry_message(ModerationLog) :: String.t()
431 def get_log_entry_message(%ModerationLog{
433 "actor" => %{"nickname" => actor_nickname},
434 "nicknames" => nicknames,
439 tags_string = tags |> Enum.join(", ")
441 "@#{actor_nickname} added tags: #{tags_string} to users: #{nicknames_to_string(nicknames)}"
444 @spec get_log_entry_message(ModerationLog) :: String.t()
445 def get_log_entry_message(%ModerationLog{
447 "actor" => %{"nickname" => actor_nickname},
448 "nicknames" => nicknames,
453 tags_string = tags |> Enum.join(", ")
455 "@#{actor_nickname} removed tags: #{tags_string} from users: #{nicknames_to_string(nicknames)}"
458 @spec get_log_entry_message(ModerationLog) :: String.t()
459 def get_log_entry_message(%ModerationLog{
461 "actor" => %{"nickname" => actor_nickname},
464 "permission" => permission
468 get_log_entry_message(%ModerationLog{
470 "actor" => %{"nickname" => actor_nickname},
473 "permission" => permission
478 @spec get_log_entry_message(ModerationLog) :: String.t()
479 def get_log_entry_message(%ModerationLog{
481 "actor" => %{"nickname" => actor_nickname},
484 "permission" => permission
487 "@#{actor_nickname} made #{users_to_nicknames_string(users)} #{permission}"
490 @spec get_log_entry_message(ModerationLog) :: String.t()
491 def get_log_entry_message(%ModerationLog{
493 "actor" => %{"nickname" => actor_nickname},
494 "action" => "revoke",
496 "permission" => permission
500 get_log_entry_message(%ModerationLog{
502 "actor" => %{"nickname" => actor_nickname},
503 "action" => "revoke",
505 "permission" => permission
510 @spec get_log_entry_message(ModerationLog) :: String.t()
511 def get_log_entry_message(%ModerationLog{
513 "actor" => %{"nickname" => actor_nickname},
514 "action" => "revoke",
516 "permission" => permission
519 "@#{actor_nickname} revoked #{permission} role from #{users_to_nicknames_string(users)}"
522 @spec get_log_entry_message(ModerationLog) :: String.t()
523 def get_log_entry_message(%ModerationLog{
525 "actor" => %{"nickname" => actor_nickname},
526 "action" => "relay_follow",
530 "@#{actor_nickname} followed relay: #{target}"
533 @spec get_log_entry_message(ModerationLog) :: String.t()
534 def get_log_entry_message(%ModerationLog{
536 "actor" => %{"nickname" => actor_nickname},
537 "action" => "relay_unfollow",
541 "@#{actor_nickname} unfollowed relay: #{target}"
544 @spec get_log_entry_message(ModerationLog) :: String.t()
545 def get_log_entry_message(%ModerationLog{
547 "actor" => %{"nickname" => actor_nickname},
548 "action" => "report_update",
549 "subject" => %{"id" => subject_id, "state" => state, "type" => "report"}
552 "@#{actor_nickname} updated report ##{subject_id} with '#{state}' state"
555 @spec get_log_entry_message(ModerationLog) :: String.t()
556 def get_log_entry_message(%ModerationLog{
558 "actor" => %{"nickname" => actor_nickname},
559 "action" => "report_response",
560 "subject" => %{"id" => subject_id, "type" => "report"},
564 "@#{actor_nickname} responded with '#{text}' to report ##{subject_id}"
567 @spec get_log_entry_message(ModerationLog) :: String.t()
568 def get_log_entry_message(%ModerationLog{
570 "actor" => %{"nickname" => actor_nickname},
571 "action" => "status_update",
572 "subject" => %{"id" => subject_id, "type" => "status"},
574 "visibility" => visibility
577 "@#{actor_nickname} updated status ##{subject_id}, set visibility: '#{visibility}'"
580 @spec get_log_entry_message(ModerationLog) :: String.t()
581 def get_log_entry_message(%ModerationLog{
583 "actor" => %{"nickname" => actor_nickname},
584 "action" => "status_update",
585 "subject" => %{"id" => subject_id, "type" => "status"},
586 "sensitive" => sensitive,
590 "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}'"
593 @spec get_log_entry_message(ModerationLog) :: String.t()
594 def get_log_entry_message(%ModerationLog{
596 "actor" => %{"nickname" => actor_nickname},
597 "action" => "status_update",
598 "subject" => %{"id" => subject_id, "type" => "status"},
599 "sensitive" => sensitive,
600 "visibility" => visibility
603 "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}', visibility: '#{
608 @spec get_log_entry_message(ModerationLog) :: String.t()
609 def get_log_entry_message(%ModerationLog{
611 "actor" => %{"nickname" => actor_nickname},
612 "action" => "status_delete",
613 "subject_id" => subject_id
616 "@#{actor_nickname} deleted status ##{subject_id}"
619 @spec get_log_entry_message(ModerationLog) :: String.t()
620 def get_log_entry_message(%ModerationLog{
622 "actor" => %{"nickname" => actor_nickname},
623 "action" => "force_password_reset",
624 "subject" => subjects
627 "@#{actor_nickname} force password reset for 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"]}")