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)
26 defp maybe_filter_by_date(query, %{start_date: nil, end_date: nil}), do: query
28 defp maybe_filter_by_date(query, %{start_date: start_date, end_date: nil}) do
30 where: q.inserted_at >= ^parse_datetime(start_date)
34 defp maybe_filter_by_date(query, %{start_date: nil, end_date: end_date}) do
36 where: q.inserted_at <= ^parse_datetime(end_date)
40 defp maybe_filter_by_date(query, %{start_date: start_date, end_date: end_date}) do
42 where: q.inserted_at >= ^parse_datetime(start_date),
43 where: q.inserted_at <= ^parse_datetime(end_date)
47 defp maybe_filter_by_user(query, %{user_id: nil}), do: query
49 defp maybe_filter_by_user(query, %{user_id: user_id}) do
51 where: fragment("(?)->'actor'->>'id' = ?", q.data, ^user_id)
55 defp maybe_filter_by_search(query, %{search: search}) when is_nil(search) or search == "",
58 defp maybe_filter_by_search(query, %{search: search}) do
60 where: fragment("(?)->>'message' ILIKE ?", q.data, ^"%#{search}%")
64 defp get_all_query(%{page: page, page_size: page_size}) do
66 order_by: [desc: q.inserted_at],
68 offset: ^((page - 1) * page_size)
72 defp parse_datetime(datetime) do
73 {:ok, parsed_datetime, _} = DateTime.from_iso8601(datetime)
78 @spec insert_log(%{actor: User, subject: User, action: String.t(), permission: String.t()}) ::
79 {:ok, ModerationLog} | {:error, any}
81 actor: %User{} = actor,
82 subject: %User{} = subject,
84 permission: permission
88 "actor" => user_to_map(actor),
89 "subject" => user_to_map(subject),
91 "permission" => permission,
95 |> insert_log_entry_with_message()
98 @spec insert_log(%{actor: User, subject: User, action: String.t()}) ::
99 {:ok, ModerationLog} | {:error, any}
101 actor: %User{} = actor,
102 action: "report_update",
103 subject: %Activity{data: %{"type" => "Flag"}} = subject
107 "actor" => user_to_map(actor),
108 "action" => "report_update",
109 "subject" => report_to_map(subject),
113 |> insert_log_entry_with_message()
116 @spec insert_log(%{actor: User, subject: Activity, action: String.t(), text: String.t()}) ::
117 {:ok, ModerationLog} | {:error, any}
119 actor: %User{} = actor,
120 action: "report_response",
121 subject: %Activity{} = subject,
126 "actor" => user_to_map(actor),
127 "action" => "report_response",
128 "subject" => report_to_map(subject),
133 |> insert_log_entry_with_message()
140 sensitive: String.t(),
141 visibility: String.t()
142 }) :: {:ok, ModerationLog} | {:error, any}
144 actor: %User{} = actor,
145 action: "status_update",
146 subject: %Activity{} = subject,
147 sensitive: sensitive,
148 visibility: visibility
152 "actor" => user_to_map(actor),
153 "action" => "status_update",
154 "subject" => status_to_map(subject),
155 "sensitive" => sensitive,
156 "visibility" => visibility,
160 |> insert_log_entry_with_message()
163 @spec insert_log(%{actor: User, action: String.t(), subject_id: String.t()}) ::
164 {:ok, ModerationLog} | {:error, any}
166 actor: %User{} = actor,
167 action: "status_delete",
168 subject_id: subject_id
172 "actor" => user_to_map(actor),
173 "action" => "status_delete",
174 "subject_id" => subject_id,
178 |> insert_log_entry_with_message()
181 @spec insert_log(%{actor: User, subject: User, action: String.t()}) ::
182 {:ok, ModerationLog} | {:error, any}
183 def insert_log(%{actor: %User{} = actor, subject: subject, action: action}) do
186 "actor" => user_to_map(actor),
188 "subject" => user_to_map(subject),
192 |> insert_log_entry_with_message()
195 @spec insert_log(%{actor: User, subjects: [User], action: String.t()}) ::
196 {:ok, ModerationLog} | {:error, any}
197 def insert_log(%{actor: %User{} = actor, subjects: subjects, action: action}) do
198 subjects = Enum.map(subjects, &user_to_map/1)
202 "actor" => user_to_map(actor),
204 "subjects" => subjects,
208 |> insert_log_entry_with_message()
211 @spec insert_log(%{actor: User, action: String.t(), followed: User, follower: User}) ::
212 {:ok, ModerationLog} | {:error, any}
214 actor: %User{} = actor,
215 followed: %User{} = followed,
216 follower: %User{} = follower,
221 "actor" => user_to_map(actor),
222 "action" => "follow",
223 "followed" => user_to_map(followed),
224 "follower" => user_to_map(follower),
228 |> insert_log_entry_with_message()
231 @spec insert_log(%{actor: User, action: String.t(), followed: User, follower: User}) ::
232 {:ok, ModerationLog} | {:error, any}
234 actor: %User{} = actor,
235 followed: %User{} = followed,
236 follower: %User{} = follower,
241 "actor" => user_to_map(actor),
242 "action" => "unfollow",
243 "followed" => user_to_map(followed),
244 "follower" => user_to_map(follower),
248 |> insert_log_entry_with_message()
251 @spec insert_log(%{actor: User, action: String.t(), nicknames: [String.t()], tags: [String.t()]}) ::
252 {:ok, ModerationLog} | {:error, any}
254 actor: %User{} = actor,
255 nicknames: nicknames,
261 "actor" => user_to_map(actor),
262 "nicknames" => nicknames,
268 |> insert_log_entry_with_message()
271 @spec insert_log(%{actor: User, action: String.t(), target: String.t()}) ::
272 {:ok, ModerationLog} | {:error, any}
274 actor: %User{} = actor,
278 when action in ["relay_follow", "relay_unfollow"] do
281 "actor" => user_to_map(actor),
287 |> insert_log_entry_with_message()
290 @spec insert_log_entry_with_message(ModerationLog) :: {:ok, ModerationLog} | {:error, any}
292 defp insert_log_entry_with_message(entry) do
293 entry.data["message"]
294 |> put_in(get_log_entry_message(entry))
298 defp user_to_map(%User{} = user) do
301 |> Map.take([:id, :nickname])
302 |> Map.new(fn {k, v} -> {Atom.to_string(k), v} end)
303 |> Map.put("type", "user")
306 defp report_to_map(%Activity{} = report) do
310 "state" => report.data["state"]
314 defp status_to_map(%Activity{} = status) do
321 def get_log_entry_message(%ModerationLog{
323 "actor" => %{"nickname" => actor_nickname},
325 "followed" => %{"nickname" => followed_nickname},
326 "follower" => %{"nickname" => follower_nickname}
329 "@#{actor_nickname} made @#{follower_nickname} #{action} @#{followed_nickname}"
332 @spec get_log_entry_message(ModerationLog) :: String.t()
333 def get_log_entry_message(%ModerationLog{
335 "actor" => %{"nickname" => actor_nickname},
336 "action" => "delete",
337 "subject" => %{"nickname" => subject_nickname, "type" => "user"}
340 "@#{actor_nickname} deleted user @#{subject_nickname}"
343 @spec get_log_entry_message(ModerationLog) :: String.t()
344 def get_log_entry_message(%ModerationLog{
346 "actor" => %{"nickname" => actor_nickname},
347 "action" => "create",
348 "subjects" => subjects
353 |> Enum.map(&"@#{&1["nickname"]}")
356 "@#{actor_nickname} created users: #{nicknames}"
359 @spec get_log_entry_message(ModerationLog) :: String.t()
360 def get_log_entry_message(%ModerationLog{
362 "actor" => %{"nickname" => actor_nickname},
363 "action" => "activate",
364 "subject" => %{"nickname" => subject_nickname, "type" => "user"}
367 "@#{actor_nickname} activated user @#{subject_nickname}"
370 @spec get_log_entry_message(ModerationLog) :: String.t()
371 def get_log_entry_message(%ModerationLog{
373 "actor" => %{"nickname" => actor_nickname},
374 "action" => "deactivate",
375 "subject" => %{"nickname" => subject_nickname, "type" => "user"}
378 "@#{actor_nickname} deactivated user @#{subject_nickname}"
381 @spec get_log_entry_message(ModerationLog) :: String.t()
382 def get_log_entry_message(%ModerationLog{
384 "actor" => %{"nickname" => actor_nickname},
385 "nicknames" => nicknames,
392 |> Enum.map(&"@#{&1}")
395 tags_string = tags |> Enum.join(", ")
397 "@#{actor_nickname} added tags: #{tags_string} to users: #{nicknames_string}"
400 @spec get_log_entry_message(ModerationLog) :: String.t()
401 def get_log_entry_message(%ModerationLog{
403 "actor" => %{"nickname" => actor_nickname},
404 "nicknames" => nicknames,
411 |> Enum.map(&"@#{&1}")
414 tags_string = tags |> Enum.join(", ")
416 "@#{actor_nickname} removed tags: #{tags_string} from users: #{nicknames_string}"
419 @spec get_log_entry_message(ModerationLog) :: String.t()
420 def get_log_entry_message(%ModerationLog{
422 "actor" => %{"nickname" => actor_nickname},
424 "subject" => %{"nickname" => subject_nickname},
425 "permission" => permission
428 "@#{actor_nickname} made @#{subject_nickname} #{permission}"
431 @spec get_log_entry_message(ModerationLog) :: String.t()
432 def get_log_entry_message(%ModerationLog{
434 "actor" => %{"nickname" => actor_nickname},
435 "action" => "revoke",
436 "subject" => %{"nickname" => subject_nickname},
437 "permission" => permission
440 "@#{actor_nickname} revoked #{permission} role from @#{subject_nickname}"
443 @spec get_log_entry_message(ModerationLog) :: String.t()
444 def get_log_entry_message(%ModerationLog{
446 "actor" => %{"nickname" => actor_nickname},
447 "action" => "relay_follow",
451 "@#{actor_nickname} followed relay: #{target}"
454 @spec get_log_entry_message(ModerationLog) :: String.t()
455 def get_log_entry_message(%ModerationLog{
457 "actor" => %{"nickname" => actor_nickname},
458 "action" => "relay_unfollow",
462 "@#{actor_nickname} unfollowed relay: #{target}"
465 @spec get_log_entry_message(ModerationLog) :: String.t()
466 def get_log_entry_message(%ModerationLog{
468 "actor" => %{"nickname" => actor_nickname},
469 "action" => "report_update",
470 "subject" => %{"id" => subject_id, "state" => state, "type" => "report"}
473 "@#{actor_nickname} updated report ##{subject_id} with '#{state}' state"
476 @spec get_log_entry_message(ModerationLog) :: String.t()
477 def get_log_entry_message(%ModerationLog{
479 "actor" => %{"nickname" => actor_nickname},
480 "action" => "report_response",
481 "subject" => %{"id" => subject_id, "type" => "report"},
485 "@#{actor_nickname} responded with '#{text}' to report ##{subject_id}"
488 @spec get_log_entry_message(ModerationLog) :: String.t()
489 def get_log_entry_message(%ModerationLog{
491 "actor" => %{"nickname" => actor_nickname},
492 "action" => "status_update",
493 "subject" => %{"id" => subject_id, "type" => "status"},
495 "visibility" => visibility
498 "@#{actor_nickname} updated status ##{subject_id}, set visibility: '#{visibility}'"
501 @spec get_log_entry_message(ModerationLog) :: String.t()
502 def get_log_entry_message(%ModerationLog{
504 "actor" => %{"nickname" => actor_nickname},
505 "action" => "status_update",
506 "subject" => %{"id" => subject_id, "type" => "status"},
507 "sensitive" => sensitive,
511 "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}'"
514 @spec get_log_entry_message(ModerationLog) :: String.t()
515 def get_log_entry_message(%ModerationLog{
517 "actor" => %{"nickname" => actor_nickname},
518 "action" => "status_update",
519 "subject" => %{"id" => subject_id, "type" => "status"},
520 "sensitive" => sensitive,
521 "visibility" => visibility
524 "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}', visibility: '#{
529 @spec get_log_entry_message(ModerationLog) :: String.t()
530 def get_log_entry_message(%ModerationLog{
532 "actor" => %{"nickname" => actor_nickname},
533 "action" => "status_delete",
534 "subject_id" => subject_id
537 "@#{actor_nickname} deleted status ##{subject_id}"