add inbound language test
[akkoma] / lib / pleroma / moderation_log.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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} | {: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 @spec insert_log_entry_with_message(ModerationLog.t()) ::
241 {:ok, ModerationLog.t()} | {:error, any}
242 defp insert_log_entry_with_message(entry) do
243 entry.data["message"]
244 |> put_in(get_log_entry_message(entry))
245 |> Repo.insert()
246 end
247
248 defp user_to_map(users) when is_list(users) do
249 Enum.map(users, &user_to_map/1)
250 end
251
252 defp user_to_map(%User{} = user) do
253 user
254 |> Map.take([:id, :nickname])
255 |> Map.new(fn {k, v} -> {Atom.to_string(k), v} end)
256 |> Map.put("type", "user")
257 end
258
259 defp user_to_map(_), do: nil
260
261 defp report_to_map(%Activity{} = report) do
262 %{"type" => "report", "id" => report.id, "state" => report.data["state"]}
263 end
264
265 defp status_to_map(%Activity{} = status) do
266 %{"type" => "status", "id" => status.id}
267 end
268
269 @spec get_log_entry_message(ModerationLog.t()) :: String.t()
270 def get_log_entry_message(%ModerationLog{
271 data: %{
272 "actor" => %{"nickname" => actor_nickname},
273 "action" => action,
274 "followed" => %{"nickname" => followed_nickname},
275 "follower" => %{"nickname" => follower_nickname}
276 }
277 }) do
278 "@#{actor_nickname} made @#{follower_nickname} #{action} @#{followed_nickname}"
279 end
280
281 def get_log_entry_message(%ModerationLog{
282 data: %{
283 "actor" => %{"nickname" => actor_nickname},
284 "action" => "delete",
285 "subject" => subjects
286 }
287 }) do
288 "@#{actor_nickname} deleted users: #{users_to_nicknames_string(subjects)}"
289 end
290
291 def get_log_entry_message(%ModerationLog{
292 data: %{
293 "actor" => %{"nickname" => actor_nickname},
294 "action" => "create",
295 "subjects" => subjects
296 }
297 }) do
298 "@#{actor_nickname} created users: #{users_to_nicknames_string(subjects)}"
299 end
300
301 def get_log_entry_message(%ModerationLog{
302 data: %{
303 "actor" => %{"nickname" => actor_nickname},
304 "action" => "activate",
305 "subject" => users
306 }
307 }) do
308 "@#{actor_nickname} activated users: #{users_to_nicknames_string(users)}"
309 end
310
311 def get_log_entry_message(%ModerationLog{
312 data: %{
313 "actor" => %{"nickname" => actor_nickname},
314 "action" => "deactivate",
315 "subject" => users
316 }
317 }) do
318 "@#{actor_nickname} deactivated users: #{users_to_nicknames_string(users)}"
319 end
320
321 def get_log_entry_message(%ModerationLog{
322 data: %{
323 "actor" => %{"nickname" => actor_nickname},
324 "action" => "approve",
325 "subject" => users
326 }
327 }) do
328 "@#{actor_nickname} approved users: #{users_to_nicknames_string(users)}"
329 end
330
331 def get_log_entry_message(%ModerationLog{
332 data: %{
333 "actor" => %{"nickname" => actor_nickname},
334 "action" => "add_suggestion",
335 "subject" => users
336 }
337 }) do
338 "@#{actor_nickname} added suggested users: #{users_to_nicknames_string(users)}"
339 end
340
341 def get_log_entry_message(%ModerationLog{
342 data: %{
343 "actor" => %{"nickname" => actor_nickname},
344 "action" => "remove_suggestion",
345 "subject" => users
346 }
347 }) do
348 "@#{actor_nickname} removed suggested users: #{users_to_nicknames_string(users)}"
349 end
350
351 def get_log_entry_message(%ModerationLog{
352 data: %{
353 "actor" => %{"nickname" => actor_nickname},
354 "nicknames" => nicknames,
355 "tags" => tags,
356 "action" => "tag"
357 }
358 }) do
359 tags_string = tags |> Enum.join(", ")
360
361 "@#{actor_nickname} added tags: #{tags_string} to users: #{nicknames_to_string(nicknames)}"
362 end
363
364 def get_log_entry_message(%ModerationLog{
365 data: %{
366 "actor" => %{"nickname" => actor_nickname},
367 "nicknames" => nicknames,
368 "tags" => tags,
369 "action" => "untag"
370 }
371 }) do
372 tags_string = tags |> Enum.join(", ")
373
374 "@#{actor_nickname} removed tags: #{tags_string} from users: #{nicknames_to_string(nicknames)}"
375 end
376
377 def get_log_entry_message(%ModerationLog{
378 data: %{
379 "actor" => %{"nickname" => actor_nickname},
380 "action" => "grant",
381 "subject" => users,
382 "permission" => permission
383 }
384 }) do
385 "@#{actor_nickname} made #{users_to_nicknames_string(users)} #{permission}"
386 end
387
388 def get_log_entry_message(%ModerationLog{
389 data: %{
390 "actor" => %{"nickname" => actor_nickname},
391 "action" => "revoke",
392 "subject" => users,
393 "permission" => permission
394 }
395 }) do
396 "@#{actor_nickname} revoked #{permission} role from #{users_to_nicknames_string(users)}"
397 end
398
399 def get_log_entry_message(%ModerationLog{
400 data: %{
401 "actor" => %{"nickname" => actor_nickname},
402 "action" => "relay_follow",
403 "target" => target
404 }
405 }) do
406 "@#{actor_nickname} followed relay: #{target}"
407 end
408
409 def get_log_entry_message(%ModerationLog{
410 data: %{
411 "actor" => %{"nickname" => actor_nickname},
412 "action" => "relay_unfollow",
413 "target" => target
414 }
415 }) do
416 "@#{actor_nickname} unfollowed relay: #{target}"
417 end
418
419 def get_log_entry_message(
420 %ModerationLog{
421 data: %{
422 "actor" => %{"nickname" => actor_nickname},
423 "action" => "report_update",
424 "subject" => %{"id" => subject_id, "state" => state, "type" => "report"}
425 }
426 } = log
427 ) do
428 "@#{actor_nickname} updated report ##{subject_id}" <>
429 subject_actor_nickname(log, " (on user ", ")") <>
430 " with '#{state}' state"
431 end
432
433 def get_log_entry_message(
434 %ModerationLog{
435 data: %{
436 "actor" => %{"nickname" => actor_nickname},
437 "action" => "report_note",
438 "subject" => %{"id" => subject_id, "type" => "report"},
439 "text" => text
440 }
441 } = log
442 ) do
443 "@#{actor_nickname} added note '#{text}' to report ##{subject_id}" <>
444 subject_actor_nickname(log, " on user ")
445 end
446
447 def get_log_entry_message(
448 %ModerationLog{
449 data: %{
450 "actor" => %{"nickname" => actor_nickname},
451 "action" => "report_note_delete",
452 "subject" => %{"id" => subject_id, "type" => "report"},
453 "text" => text
454 }
455 } = log
456 ) do
457 "@#{actor_nickname} deleted note '#{text}' from report ##{subject_id}" <>
458 subject_actor_nickname(log, " on user ")
459 end
460
461 def get_log_entry_message(%ModerationLog{
462 data: %{
463 "actor" => %{"nickname" => actor_nickname},
464 "action" => "status_update",
465 "subject" => %{"id" => subject_id, "type" => "status"},
466 "sensitive" => nil,
467 "visibility" => visibility
468 }
469 }) do
470 "@#{actor_nickname} updated status ##{subject_id}, set visibility: '#{visibility}'"
471 end
472
473 def get_log_entry_message(%ModerationLog{
474 data: %{
475 "actor" => %{"nickname" => actor_nickname},
476 "action" => "status_update",
477 "subject" => %{"id" => subject_id, "type" => "status"},
478 "sensitive" => sensitive,
479 "visibility" => nil
480 }
481 }) do
482 "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}'"
483 end
484
485 def get_log_entry_message(%ModerationLog{
486 data: %{
487 "actor" => %{"nickname" => actor_nickname},
488 "action" => "status_update",
489 "subject" => %{"id" => subject_id, "type" => "status"},
490 "sensitive" => sensitive,
491 "visibility" => visibility
492 }
493 }) do
494 "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}', visibility: '#{visibility}'"
495 end
496
497 def get_log_entry_message(%ModerationLog{
498 data: %{
499 "actor" => %{"nickname" => actor_nickname},
500 "action" => "status_delete",
501 "subject_id" => subject_id
502 }
503 }) do
504 "@#{actor_nickname} deleted status ##{subject_id}"
505 end
506
507 def get_log_entry_message(%ModerationLog{
508 data: %{
509 "actor" => %{"nickname" => actor_nickname},
510 "action" => "force_password_reset",
511 "subject" => subjects
512 }
513 }) do
514 "@#{actor_nickname} forced password reset for users: #{users_to_nicknames_string(subjects)}"
515 end
516
517 def get_log_entry_message(%ModerationLog{
518 data: %{
519 "actor" => %{"nickname" => actor_nickname},
520 "action" => "confirm_email",
521 "subject" => subjects
522 }
523 }) do
524 "@#{actor_nickname} confirmed email for users: #{users_to_nicknames_string(subjects)}"
525 end
526
527 def get_log_entry_message(%ModerationLog{
528 data: %{
529 "actor" => %{"nickname" => actor_nickname},
530 "action" => "resend_confirmation_email",
531 "subject" => subjects
532 }
533 }) do
534 "@#{actor_nickname} re-sent confirmation email for users: #{users_to_nicknames_string(subjects)}"
535 end
536
537 def get_log_entry_message(%ModerationLog{
538 data: %{
539 "actor" => %{"nickname" => actor_nickname},
540 "action" => "updated_users",
541 "subject" => subjects
542 }
543 }) do
544 "@#{actor_nickname} updated users: #{users_to_nicknames_string(subjects)}"
545 end
546
547 def get_log_entry_message(%ModerationLog{
548 data: %{
549 "actor" => %{"nickname" => actor_nickname},
550 "action" => "create_backup",
551 "subject" => %{"nickname" => user_nickname}
552 }
553 }) do
554 "@#{actor_nickname} requested account backup for @#{user_nickname}"
555 end
556
557 defp nicknames_to_string(nicknames) do
558 nicknames
559 |> Enum.map(&"@#{&1}")
560 |> Enum.join(", ")
561 end
562
563 defp users_to_nicknames_string(users) do
564 users
565 |> Enum.map(&"@#{&1["nickname"]}")
566 |> Enum.join(", ")
567 end
568
569 defp subject_actor_nickname(%ModerationLog{data: data}, prefix_msg, postfix_msg \\ "") do
570 case data do
571 %{"subject_actor" => %{"nickname" => subject_actor}} ->
572 [prefix_msg, "@#{subject_actor}", postfix_msg]
573 |> Enum.reject(&(&1 == ""))
574 |> Enum.join()
575
576 _ ->
577 ""
578 end
579 end
580 end