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,
93 subject: %User{} = subject,
95 permission: permission
99 "actor" => user_to_map(actor),
100 "subject" => user_to_map(subject),
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}
307 defp insert_log_entry_with_message(entry) do
308 entry.data["message"]
309 |> put_in(get_log_entry_message(entry))
313 defp user_to_map(%User{} = user) do
316 |> Map.take([:id, :nickname])
317 |> Map.new(fn {k, v} -> {Atom.to_string(k), v} end)
318 |> Map.put("type", "user")
321 defp report_to_map(%Activity{} = report) do
325 "state" => report.data["state"]
329 defp status_to_map(%Activity{} = status) do
336 def get_log_entry_message(%ModerationLog{
338 "actor" => %{"nickname" => actor_nickname},
340 "followed" => %{"nickname" => followed_nickname},
341 "follower" => %{"nickname" => follower_nickname}
344 "@#{actor_nickname} made @#{follower_nickname} #{action} @#{followed_nickname}"
347 @spec get_log_entry_message(ModerationLog) :: String.t()
348 def get_log_entry_message(%ModerationLog{
350 "actor" => %{"nickname" => actor_nickname},
351 "action" => "delete",
352 "subject" => %{"nickname" => subject_nickname, "type" => "user"}
355 "@#{actor_nickname} deleted user @#{subject_nickname}"
358 @spec get_log_entry_message(ModerationLog) :: String.t()
359 def get_log_entry_message(%ModerationLog{
361 "actor" => %{"nickname" => actor_nickname},
362 "action" => "create",
363 "subjects" => subjects
368 |> Enum.map(&"@#{&1["nickname"]}")
371 "@#{actor_nickname} created users: #{nicknames}"
374 @spec get_log_entry_message(ModerationLog) :: String.t()
375 def get_log_entry_message(%ModerationLog{
377 "actor" => %{"nickname" => actor_nickname},
378 "action" => "activate",
379 "subject" => %{"nickname" => subject_nickname, "type" => "user"}
382 "@#{actor_nickname} activated user @#{subject_nickname}"
385 @spec get_log_entry_message(ModerationLog) :: String.t()
386 def get_log_entry_message(%ModerationLog{
388 "actor" => %{"nickname" => actor_nickname},
389 "action" => "deactivate",
390 "subject" => %{"nickname" => subject_nickname, "type" => "user"}
393 "@#{actor_nickname} deactivated user @#{subject_nickname}"
396 @spec get_log_entry_message(ModerationLog) :: String.t()
397 def get_log_entry_message(%ModerationLog{
399 "actor" => %{"nickname" => actor_nickname},
400 "nicknames" => nicknames,
407 |> Enum.map(&"@#{&1}")
410 tags_string = tags |> Enum.join(", ")
412 "@#{actor_nickname} added tags: #{tags_string} to users: #{nicknames_string}"
415 @spec get_log_entry_message(ModerationLog) :: String.t()
416 def get_log_entry_message(%ModerationLog{
418 "actor" => %{"nickname" => actor_nickname},
419 "nicknames" => nicknames,
426 |> Enum.map(&"@#{&1}")
429 tags_string = tags |> Enum.join(", ")
431 "@#{actor_nickname} removed tags: #{tags_string} from users: #{nicknames_string}"
434 @spec get_log_entry_message(ModerationLog) :: String.t()
435 def get_log_entry_message(%ModerationLog{
437 "actor" => %{"nickname" => actor_nickname},
439 "subject" => %{"nickname" => subject_nickname},
440 "permission" => permission
443 "@#{actor_nickname} made @#{subject_nickname} #{permission}"
446 @spec get_log_entry_message(ModerationLog) :: String.t()
447 def get_log_entry_message(%ModerationLog{
449 "actor" => %{"nickname" => actor_nickname},
450 "action" => "revoke",
451 "subject" => %{"nickname" => subject_nickname},
452 "permission" => permission
455 "@#{actor_nickname} revoked #{permission} role from @#{subject_nickname}"
458 @spec get_log_entry_message(ModerationLog) :: String.t()
459 def get_log_entry_message(%ModerationLog{
461 "actor" => %{"nickname" => actor_nickname},
462 "action" => "relay_follow",
466 "@#{actor_nickname} followed relay: #{target}"
469 @spec get_log_entry_message(ModerationLog) :: String.t()
470 def get_log_entry_message(%ModerationLog{
472 "actor" => %{"nickname" => actor_nickname},
473 "action" => "relay_unfollow",
477 "@#{actor_nickname} unfollowed relay: #{target}"
480 @spec get_log_entry_message(ModerationLog) :: String.t()
481 def get_log_entry_message(%ModerationLog{
483 "actor" => %{"nickname" => actor_nickname},
484 "action" => "report_update",
485 "subject" => %{"id" => subject_id, "state" => state, "type" => "report"}
488 "@#{actor_nickname} updated report ##{subject_id} with '#{state}' state"
491 @spec get_log_entry_message(ModerationLog) :: String.t()
492 def get_log_entry_message(%ModerationLog{
494 "actor" => %{"nickname" => actor_nickname},
495 "action" => "report_response",
496 "subject" => %{"id" => subject_id, "type" => "report"},
500 "@#{actor_nickname} responded with '#{text}' to report ##{subject_id}"
503 @spec get_log_entry_message(ModerationLog) :: String.t()
504 def get_log_entry_message(%ModerationLog{
506 "actor" => %{"nickname" => actor_nickname},
507 "action" => "status_update",
508 "subject" => %{"id" => subject_id, "type" => "status"},
510 "visibility" => visibility
513 "@#{actor_nickname} updated status ##{subject_id}, set visibility: '#{visibility}'"
516 @spec get_log_entry_message(ModerationLog) :: String.t()
517 def get_log_entry_message(%ModerationLog{
519 "actor" => %{"nickname" => actor_nickname},
520 "action" => "status_update",
521 "subject" => %{"id" => subject_id, "type" => "status"},
522 "sensitive" => sensitive,
526 "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}'"
529 @spec get_log_entry_message(ModerationLog) :: String.t()
530 def get_log_entry_message(%ModerationLog{
532 "actor" => %{"nickname" => actor_nickname},
533 "action" => "status_update",
534 "subject" => %{"id" => subject_id, "type" => "status"},
535 "sensitive" => sensitive,
536 "visibility" => visibility
539 "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}', visibility: '#{
544 @spec get_log_entry_message(ModerationLog) :: String.t()
545 def get_log_entry_message(%ModerationLog{
547 "actor" => %{"nickname" => actor_nickname},
548 "action" => "status_delete",
549 "subject_id" => subject_id
552 "@#{actor_nickname} deleted status ##{subject_id}"