total rebase
[anni] / lib / pleroma / moderation_log.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.ModerationLog do
6   use Ecto.Schema
7
8   alias Pleroma.Activity
9   alias Pleroma.ModerationLog
10   alias Pleroma.Repo
11   alias Pleroma.User
12
13   import Ecto.Query
14
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()
33         }
34
35   schema "moderation_log" do
36     field(:data, :map)
37
38     timestamps()
39   end
40
41   def get_all(params) do
42     base_query =
43       get_all_query()
44       |> maybe_filter_by_date(params)
45       |> maybe_filter_by_user(params)
46       |> maybe_filter_by_search(params)
47
48     query_with_pagination = base_query |> paginate_query(params)
49
50     %{
51       items: Repo.all(query_with_pagination),
52       count: Repo.aggregate(base_query, :count, :id)
53     }
54   end
55
56   defp maybe_filter_by_date(query, %{start_date: nil, end_date: nil}), do: query
57
58   defp maybe_filter_by_date(query, %{start_date: start_date, end_date: nil}) do
59     from(q in query,
60       where: q.inserted_at >= ^parse_datetime(start_date)
61     )
62   end
63
64   defp maybe_filter_by_date(query, %{start_date: nil, end_date: end_date}) do
65     from(q in query,
66       where: q.inserted_at <= ^parse_datetime(end_date)
67     )
68   end
69
70   defp maybe_filter_by_date(query, %{start_date: start_date, end_date: end_date}) do
71     from(q in query,
72       where: q.inserted_at >= ^parse_datetime(start_date),
73       where: q.inserted_at <= ^parse_datetime(end_date)
74     )
75   end
76
77   defp maybe_filter_by_user(query, %{user_id: nil}), do: query
78
79   defp maybe_filter_by_user(query, %{user_id: user_id}) do
80     from(q in query,
81       where: fragment("(?)->'actor'->>'id' = ?", q.data, ^user_id)
82     )
83   end
84
85   defp maybe_filter_by_search(query, %{search: search}) when is_nil(search) or search == "",
86     do: query
87
88   defp maybe_filter_by_search(query, %{search: search}) do
89     from(q in query,
90       where: fragment("(?)->>'message' ILIKE ?", q.data, ^"%#{search}%")
91     )
92   end
93
94   defp paginate_query(query, %{page: page, page_size: page_size}) do
95     from(q in query,
96       limit: ^page_size,
97       offset: ^((page - 1) * page_size)
98     )
99   end
100
101   defp get_all_query do
102     from(q in __MODULE__,
103       order_by: [desc: q.inserted_at]
104     )
105   end
106
107   defp parse_datetime(datetime) do
108     {:ok, parsed_datetime, _} = DateTime.from_iso8601(datetime)
109
110     parsed_datetime
111   end
112
113   defp prepare_log_data(%{actor: actor, action: action} = attrs) do
114     %{
115       "actor" => user_to_map(actor),
116       "action" => action,
117       "message" => ""
118     }
119     |> Pleroma.Maps.put_if_present("subject_actor", user_to_map(attrs[:subject_actor]))
120   end
121
122   defp prepare_log_data(attrs), do: attrs
123
124   @spec insert_log(log_params()) :: {:ok, ModerationLog.t()} | {:error, any}
125   def insert_log(%{actor: %User{}, subject: subjects, permission: permission} = attrs) do
126     data =
127       attrs
128       |> prepare_log_data
129       |> Map.merge(%{"subject" => user_to_map(subjects), "permission" => permission})
130
131     insert_log_entry_with_message(%ModerationLog{data: data})
132   end
133
134   def insert_log(%{actor: %User{}, action: action, subject: %Activity{} = subject} = attrs)
135       when action in ["report_note_delete", "report_update", "report_note"] do
136     data =
137       attrs
138       |> prepare_log_data
139       |> Pleroma.Maps.put_if_present("text", attrs[:text])
140       |> Map.merge(%{"subject" => report_to_map(subject)})
141
142     insert_log_entry_with_message(%ModerationLog{data: data})
143   end
144
145   def insert_log(
146         %{
147           actor: %User{},
148           action: action,
149           subject: %Activity{} = subject,
150           sensitive: sensitive,
151           visibility: visibility
152         } = attrs
153       )
154       when action == "status_update" do
155     data =
156       attrs
157       |> prepare_log_data
158       |> Map.merge(%{
159         "subject" => status_to_map(subject),
160         "sensitive" => sensitive,
161         "visibility" => visibility
162       })
163
164     insert_log_entry_with_message(%ModerationLog{data: data})
165   end
166
167   def insert_log(%{actor: %User{}, action: action, subject_id: subject_id} = attrs)
168       when action == "status_delete" do
169     data =
170       attrs
171       |> prepare_log_data
172       |> Map.merge(%{"subject_id" => subject_id})
173
174     insert_log_entry_with_message(%ModerationLog{data: data})
175   end
176
177   def insert_log(%{actor: %User{}, subject: subject, action: _action} = attrs) do
178     data =
179       attrs
180       |> prepare_log_data
181       |> Map.merge(%{"subject" => user_to_map(subject)})
182
183     insert_log_entry_with_message(%ModerationLog{data: data})
184   end
185
186   def insert_log(%{actor: %User{}, subjects: subjects, action: _action} = attrs) do
187     data =
188       attrs
189       |> prepare_log_data
190       |> Map.merge(%{"subjects" => user_to_map(subjects)})
191
192     insert_log_entry_with_message(%ModerationLog{data: data})
193   end
194
195   def insert_log(
196         %{
197           actor: %User{},
198           followed: %User{} = followed,
199           follower: %User{} = follower,
200           action: action
201         } = attrs
202       )
203       when action in ["unfollow", "follow"] do
204     data =
205       attrs
206       |> prepare_log_data
207       |> Map.merge(%{"followed" => user_to_map(followed), "follower" => user_to_map(follower)})
208
209     insert_log_entry_with_message(%ModerationLog{data: data})
210   end
211
212   def insert_log(%{
213         actor: %User{} = actor,
214         nicknames: nicknames,
215         tags: tags,
216         action: action
217       }) do
218     %ModerationLog{
219       data: %{
220         "actor" => user_to_map(actor),
221         "nicknames" => nicknames,
222         "tags" => tags,
223         "action" => action,
224         "message" => ""
225       }
226     }
227     |> insert_log_entry_with_message()
228   end
229
230   def insert_log(%{actor: %User{}, action: action, target: target} = attrs)
231       when action in ["relay_follow", "relay_unfollow"] do
232     data =
233       attrs
234       |> prepare_log_data
235       |> Map.merge(%{"target" => target})
236
237     insert_log_entry_with_message(%ModerationLog{data: data})
238   end
239
240   def insert_log(%{actor: %User{} = actor, action: "chat_message_delete", subject_id: subject_id}) do
241     %ModerationLog{
242       data: %{
243         "actor" => %{"nickname" => actor.nickname},
244         "action" => "chat_message_delete",
245         "subject_id" => subject_id
246       }
247     }
248     |> insert_log_entry_with_message()
249   end
250
251   @spec insert_log_entry_with_message(ModerationLog.t()) ::
252           {:ok, ModerationLog.t()} | {:error, any}
253   defp insert_log_entry_with_message(entry) do
254     entry.data["message"]
255     |> put_in(get_log_entry_message(entry))
256     |> Repo.insert()
257   end
258
259   defp user_to_map(users) when is_list(users) do
260     Enum.map(users, &user_to_map/1)
261   end
262
263   defp user_to_map(%User{} = user) do
264     user
265     |> Map.take([:id, :nickname])
266     |> Map.new(fn {k, v} -> {Atom.to_string(k), v} end)
267     |> Map.put("type", "user")
268   end
269
270   defp user_to_map(_), do: nil
271
272   defp report_to_map(%Activity{} = report) do
273     %{"type" => "report", "id" => report.id, "state" => report.data["state"]}
274   end
275
276   defp status_to_map(%Activity{} = status) do
277     %{"type" => "status", "id" => status.id}
278   end
279
280   @spec get_log_entry_message(ModerationLog.t()) :: String.t()
281   def get_log_entry_message(%ModerationLog{
282         data: %{
283           "actor" => %{"nickname" => actor_nickname},
284           "action" => action,
285           "followed" => %{"nickname" => followed_nickname},
286           "follower" => %{"nickname" => follower_nickname}
287         }
288       }) do
289     "@#{actor_nickname} made @#{follower_nickname} #{action} @#{followed_nickname}"
290   end
291
292   def get_log_entry_message(%ModerationLog{
293         data: %{
294           "actor" => %{"nickname" => actor_nickname},
295           "action" => "delete",
296           "subject" => subjects
297         }
298       }) do
299     "@#{actor_nickname} deleted users: #{users_to_nicknames_string(subjects)}"
300   end
301
302   def get_log_entry_message(%ModerationLog{
303         data: %{
304           "actor" => %{"nickname" => actor_nickname},
305           "action" => "create",
306           "subjects" => subjects
307         }
308       }) do
309     "@#{actor_nickname} created users: #{users_to_nicknames_string(subjects)}"
310   end
311
312   def get_log_entry_message(%ModerationLog{
313         data: %{
314           "actor" => %{"nickname" => actor_nickname},
315           "action" => "activate",
316           "subject" => users
317         }
318       }) do
319     "@#{actor_nickname} activated users: #{users_to_nicknames_string(users)}"
320   end
321
322   def get_log_entry_message(%ModerationLog{
323         data: %{
324           "actor" => %{"nickname" => actor_nickname},
325           "action" => "deactivate",
326           "subject" => users
327         }
328       }) do
329     "@#{actor_nickname} deactivated users: #{users_to_nicknames_string(users)}"
330   end
331
332   def get_log_entry_message(%ModerationLog{
333         data: %{
334           "actor" => %{"nickname" => actor_nickname},
335           "action" => "approve",
336           "subject" => users
337         }
338       }) do
339     "@#{actor_nickname} approved users: #{users_to_nicknames_string(users)}"
340   end
341
342   def get_log_entry_message(%ModerationLog{
343         data: %{
344           "actor" => %{"nickname" => actor_nickname},
345           "action" => "add_suggestion",
346           "subject" => users
347         }
348       }) do
349     "@#{actor_nickname} added suggested users: #{users_to_nicknames_string(users)}"
350   end
351
352   def get_log_entry_message(%ModerationLog{
353         data: %{
354           "actor" => %{"nickname" => actor_nickname},
355           "action" => "remove_suggestion",
356           "subject" => users
357         }
358       }) do
359     "@#{actor_nickname} removed suggested users: #{users_to_nicknames_string(users)}"
360   end
361
362   def get_log_entry_message(%ModerationLog{
363         data: %{
364           "actor" => %{"nickname" => actor_nickname},
365           "nicknames" => nicknames,
366           "tags" => tags,
367           "action" => "tag"
368         }
369       }) do
370     tags_string = tags |> Enum.join(", ")
371
372     "@#{actor_nickname} added tags: #{tags_string} to users: #{nicknames_to_string(nicknames)}"
373   end
374
375   def get_log_entry_message(%ModerationLog{
376         data: %{
377           "actor" => %{"nickname" => actor_nickname},
378           "nicknames" => nicknames,
379           "tags" => tags,
380           "action" => "untag"
381         }
382       }) do
383     tags_string = tags |> Enum.join(", ")
384
385     "@#{actor_nickname} removed tags: #{tags_string} from users: #{nicknames_to_string(nicknames)}"
386   end
387
388   def get_log_entry_message(%ModerationLog{
389         data: %{
390           "actor" => %{"nickname" => actor_nickname},
391           "action" => "grant",
392           "subject" => users,
393           "permission" => permission
394         }
395       }) do
396     "@#{actor_nickname} made #{users_to_nicknames_string(users)} #{permission}"
397   end
398
399   def get_log_entry_message(%ModerationLog{
400         data: %{
401           "actor" => %{"nickname" => actor_nickname},
402           "action" => "revoke",
403           "subject" => users,
404           "permission" => permission
405         }
406       }) do
407     "@#{actor_nickname} revoked #{permission} role from #{users_to_nicknames_string(users)}"
408   end
409
410   def get_log_entry_message(%ModerationLog{
411         data: %{
412           "actor" => %{"nickname" => actor_nickname},
413           "action" => "relay_follow",
414           "target" => target
415         }
416       }) do
417     "@#{actor_nickname} followed relay: #{target}"
418   end
419
420   def get_log_entry_message(%ModerationLog{
421         data: %{
422           "actor" => %{"nickname" => actor_nickname},
423           "action" => "relay_unfollow",
424           "target" => target
425         }
426       }) do
427     "@#{actor_nickname} unfollowed relay: #{target}"
428   end
429
430   def get_log_entry_message(
431         %ModerationLog{
432           data: %{
433             "actor" => %{"nickname" => actor_nickname},
434             "action" => "report_update",
435             "subject" => %{"id" => subject_id, "state" => state, "type" => "report"}
436           }
437         } = log
438       ) do
439     "@#{actor_nickname} updated report ##{subject_id}" <>
440       subject_actor_nickname(log, " (on user ", ")") <>
441       " with '#{state}' state"
442   end
443
444   def get_log_entry_message(
445         %ModerationLog{
446           data: %{
447             "actor" => %{"nickname" => actor_nickname},
448             "action" => "report_note",
449             "subject" => %{"id" => subject_id, "type" => "report"},
450             "text" => text
451           }
452         } = log
453       ) do
454     "@#{actor_nickname} added note '#{text}' to report ##{subject_id}" <>
455       subject_actor_nickname(log, " on user ")
456   end
457
458   def get_log_entry_message(
459         %ModerationLog{
460           data: %{
461             "actor" => %{"nickname" => actor_nickname},
462             "action" => "report_note_delete",
463             "subject" => %{"id" => subject_id, "type" => "report"},
464             "text" => text
465           }
466         } = log
467       ) do
468     "@#{actor_nickname} deleted note '#{text}' from report ##{subject_id}" <>
469       subject_actor_nickname(log, " on user ")
470   end
471
472   def get_log_entry_message(%ModerationLog{
473         data: %{
474           "actor" => %{"nickname" => actor_nickname},
475           "action" => "status_update",
476           "subject" => %{"id" => subject_id, "type" => "status"},
477           "sensitive" => nil,
478           "visibility" => visibility
479         }
480       }) do
481     "@#{actor_nickname} updated status ##{subject_id}, set visibility: '#{visibility}'"
482   end
483
484   def get_log_entry_message(%ModerationLog{
485         data: %{
486           "actor" => %{"nickname" => actor_nickname},
487           "action" => "status_update",
488           "subject" => %{"id" => subject_id, "type" => "status"},
489           "sensitive" => sensitive,
490           "visibility" => nil
491         }
492       }) do
493     "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}'"
494   end
495
496   def get_log_entry_message(%ModerationLog{
497         data: %{
498           "actor" => %{"nickname" => actor_nickname},
499           "action" => "status_update",
500           "subject" => %{"id" => subject_id, "type" => "status"},
501           "sensitive" => sensitive,
502           "visibility" => visibility
503         }
504       }) do
505     "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}', visibility: '#{visibility}'"
506   end
507
508   def get_log_entry_message(%ModerationLog{
509         data: %{
510           "actor" => %{"nickname" => actor_nickname},
511           "action" => "status_delete",
512           "subject_id" => subject_id
513         }
514       }) do
515     "@#{actor_nickname} deleted status ##{subject_id}"
516   end
517
518   def get_log_entry_message(%ModerationLog{
519         data: %{
520           "actor" => %{"nickname" => actor_nickname},
521           "action" => "force_password_reset",
522           "subject" => subjects
523         }
524       }) do
525     "@#{actor_nickname} forced password reset for users: #{users_to_nicknames_string(subjects)}"
526   end
527
528   def get_log_entry_message(%ModerationLog{
529         data: %{
530           "actor" => %{"nickname" => actor_nickname},
531           "action" => "confirm_email",
532           "subject" => subjects
533         }
534       }) do
535     "@#{actor_nickname} confirmed email for users: #{users_to_nicknames_string(subjects)}"
536   end
537
538   def get_log_entry_message(%ModerationLog{
539         data: %{
540           "actor" => %{"nickname" => actor_nickname},
541           "action" => "resend_confirmation_email",
542           "subject" => subjects
543         }
544       }) do
545     "@#{actor_nickname} re-sent confirmation email for users: #{users_to_nicknames_string(subjects)}"
546   end
547
548   def get_log_entry_message(%ModerationLog{
549         data: %{
550           "actor" => %{"nickname" => actor_nickname},
551           "action" => "updated_users",
552           "subject" => subjects
553         }
554       }) do
555     "@#{actor_nickname} updated users: #{users_to_nicknames_string(subjects)}"
556   end
557
558   def get_log_entry_message(%ModerationLog{
559         data: %{
560           "actor" => %{"nickname" => actor_nickname},
561           "action" => "chat_message_delete",
562           "subject_id" => subject_id
563         }
564       }) do
565     "@#{actor_nickname} deleted chat message ##{subject_id}"
566   end
567
568   def get_log_entry_message(%ModerationLog{
569         data: %{
570           "actor" => %{"nickname" => actor_nickname},
571           "action" => "create_backup",
572           "subject" => %{"nickname" => user_nickname}
573         }
574       }) do
575     "@#{actor_nickname} requested account backup for @#{user_nickname}"
576   end
577
578   defp nicknames_to_string(nicknames) do
579     nicknames
580     |> Enum.map(&"@#{&1}")
581     |> Enum.join(", ")
582   end
583
584   defp users_to_nicknames_string(users) do
585     users
586     |> Enum.map(&"@#{&1["nickname"]}")
587     |> Enum.join(", ")
588   end
589
590   defp subject_actor_nickname(%ModerationLog{data: data}, prefix_msg, postfix_msg \\ "") do
591     case data do
592       %{"subject_actor" => %{"nickname" => subject_actor}} ->
593         [prefix_msg, "@#{subject_actor}", postfix_msg]
594         |> Enum.reject(&(&1 == ""))
595         |> Enum.join()
596
597       _ ->
598         ""
599     end
600   end
601 end