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