Merge branch 'exclude-visibilities-for-timelines' 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: %User{} = subject,
94 action: action,
95 permission: permission
96 }) do
97 %ModerationLog{
98 data: %{
99 "actor" => user_to_map(actor),
100 "subject" => user_to_map(subject),
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_response",
132 subject: %Activity{} = subject,
133 text: text
134 }) do
135 %ModerationLog{
136 data: %{
137 "actor" => user_to_map(actor),
138 "action" => "report_response",
139 "subject" => report_to_map(subject),
140 "text" => text,
141 "message" => ""
142 }
143 }
144 |> insert_log_entry_with_message()
145 end
146
147 @spec insert_log(%{
148 actor: User,
149 subject: Activity,
150 action: String.t(),
151 sensitive: String.t(),
152 visibility: String.t()
153 }) :: {:ok, ModerationLog} | {:error, any}
154 def insert_log(%{
155 actor: %User{} = actor,
156 action: "status_update",
157 subject: %Activity{} = subject,
158 sensitive: sensitive,
159 visibility: visibility
160 }) do
161 %ModerationLog{
162 data: %{
163 "actor" => user_to_map(actor),
164 "action" => "status_update",
165 "subject" => status_to_map(subject),
166 "sensitive" => sensitive,
167 "visibility" => visibility,
168 "message" => ""
169 }
170 }
171 |> insert_log_entry_with_message()
172 end
173
174 @spec insert_log(%{actor: User, action: String.t(), subject_id: String.t()}) ::
175 {:ok, ModerationLog} | {:error, any}
176 def insert_log(%{
177 actor: %User{} = actor,
178 action: "status_delete",
179 subject_id: subject_id
180 }) do
181 %ModerationLog{
182 data: %{
183 "actor" => user_to_map(actor),
184 "action" => "status_delete",
185 "subject_id" => subject_id,
186 "message" => ""
187 }
188 }
189 |> insert_log_entry_with_message()
190 end
191
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
195 %ModerationLog{
196 data: %{
197 "actor" => user_to_map(actor),
198 "action" => action,
199 "subject" => user_to_map(subject),
200 "message" => ""
201 }
202 }
203 |> insert_log_entry_with_message()
204 end
205
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)
210
211 %ModerationLog{
212 data: %{
213 "actor" => user_to_map(actor),
214 "action" => action,
215 "subjects" => subjects,
216 "message" => ""
217 }
218 }
219 |> insert_log_entry_with_message()
220 end
221
222 @spec insert_log(%{actor: User, action: String.t(), followed: User, follower: User}) ::
223 {:ok, ModerationLog} | {:error, any}
224 def insert_log(%{
225 actor: %User{} = actor,
226 followed: %User{} = followed,
227 follower: %User{} = follower,
228 action: "follow"
229 }) do
230 %ModerationLog{
231 data: %{
232 "actor" => user_to_map(actor),
233 "action" => "follow",
234 "followed" => user_to_map(followed),
235 "follower" => user_to_map(follower),
236 "message" => ""
237 }
238 }
239 |> insert_log_entry_with_message()
240 end
241
242 @spec insert_log(%{actor: User, action: String.t(), followed: User, follower: User}) ::
243 {:ok, ModerationLog} | {:error, any}
244 def insert_log(%{
245 actor: %User{} = actor,
246 followed: %User{} = followed,
247 follower: %User{} = follower,
248 action: "unfollow"
249 }) do
250 %ModerationLog{
251 data: %{
252 "actor" => user_to_map(actor),
253 "action" => "unfollow",
254 "followed" => user_to_map(followed),
255 "follower" => user_to_map(follower),
256 "message" => ""
257 }
258 }
259 |> insert_log_entry_with_message()
260 end
261
262 @spec insert_log(%{
263 actor: User,
264 action: String.t(),
265 nicknames: [String.t()],
266 tags: [String.t()]
267 }) :: {:ok, ModerationLog} | {:error, any}
268 def insert_log(%{
269 actor: %User{} = actor,
270 nicknames: nicknames,
271 tags: tags,
272 action: action
273 }) do
274 %ModerationLog{
275 data: %{
276 "actor" => user_to_map(actor),
277 "nicknames" => nicknames,
278 "tags" => tags,
279 "action" => action,
280 "message" => ""
281 }
282 }
283 |> insert_log_entry_with_message()
284 end
285
286 @spec insert_log(%{actor: User, action: String.t(), target: String.t()}) ::
287 {:ok, ModerationLog} | {:error, any}
288 def insert_log(%{
289 actor: %User{} = actor,
290 action: action,
291 target: target
292 })
293 when action in ["relay_follow", "relay_unfollow"] do
294 %ModerationLog{
295 data: %{
296 "actor" => user_to_map(actor),
297 "action" => action,
298 "target" => target,
299 "message" => ""
300 }
301 }
302 |> insert_log_entry_with_message()
303 end
304
305 @spec insert_log_entry_with_message(ModerationLog) :: {:ok, ModerationLog} | {:error, any}
306
307 defp insert_log_entry_with_message(entry) do
308 entry.data["message"]
309 |> put_in(get_log_entry_message(entry))
310 |> Repo.insert()
311 end
312
313 defp user_to_map(%User{} = user) do
314 user
315 |> Map.from_struct()
316 |> Map.take([:id, :nickname])
317 |> Map.new(fn {k, v} -> {Atom.to_string(k), v} end)
318 |> Map.put("type", "user")
319 end
320
321 defp report_to_map(%Activity{} = report) do
322 %{
323 "type" => "report",
324 "id" => report.id,
325 "state" => report.data["state"]
326 }
327 end
328
329 defp status_to_map(%Activity{} = status) do
330 %{
331 "type" => "status",
332 "id" => status.id
333 }
334 end
335
336 def get_log_entry_message(%ModerationLog{
337 data: %{
338 "actor" => %{"nickname" => actor_nickname},
339 "action" => action,
340 "followed" => %{"nickname" => followed_nickname},
341 "follower" => %{"nickname" => follower_nickname}
342 }
343 }) do
344 "@#{actor_nickname} made @#{follower_nickname} #{action} @#{followed_nickname}"
345 end
346
347 @spec get_log_entry_message(ModerationLog) :: String.t()
348 def get_log_entry_message(%ModerationLog{
349 data: %{
350 "actor" => %{"nickname" => actor_nickname},
351 "action" => "delete",
352 "subject" => %{"nickname" => subject_nickname, "type" => "user"}
353 }
354 }) do
355 "@#{actor_nickname} deleted user @#{subject_nickname}"
356 end
357
358 @spec get_log_entry_message(ModerationLog) :: String.t()
359 def get_log_entry_message(%ModerationLog{
360 data: %{
361 "actor" => %{"nickname" => actor_nickname},
362 "action" => "create",
363 "subjects" => subjects
364 }
365 }) do
366 nicknames =
367 subjects
368 |> Enum.map(&"@#{&1["nickname"]}")
369 |> Enum.join(", ")
370
371 "@#{actor_nickname} created users: #{nicknames}"
372 end
373
374 @spec get_log_entry_message(ModerationLog) :: String.t()
375 def get_log_entry_message(%ModerationLog{
376 data: %{
377 "actor" => %{"nickname" => actor_nickname},
378 "action" => "activate",
379 "subject" => %{"nickname" => subject_nickname, "type" => "user"}
380 }
381 }) do
382 "@#{actor_nickname} activated user @#{subject_nickname}"
383 end
384
385 @spec get_log_entry_message(ModerationLog) :: String.t()
386 def get_log_entry_message(%ModerationLog{
387 data: %{
388 "actor" => %{"nickname" => actor_nickname},
389 "action" => "deactivate",
390 "subject" => %{"nickname" => subject_nickname, "type" => "user"}
391 }
392 }) do
393 "@#{actor_nickname} deactivated user @#{subject_nickname}"
394 end
395
396 @spec get_log_entry_message(ModerationLog) :: String.t()
397 def get_log_entry_message(%ModerationLog{
398 data: %{
399 "actor" => %{"nickname" => actor_nickname},
400 "nicknames" => nicknames,
401 "tags" => tags,
402 "action" => "tag"
403 }
404 }) do
405 nicknames_string =
406 nicknames
407 |> Enum.map(&"@#{&1}")
408 |> Enum.join(", ")
409
410 tags_string = tags |> Enum.join(", ")
411
412 "@#{actor_nickname} added tags: #{tags_string} to users: #{nicknames_string}"
413 end
414
415 @spec get_log_entry_message(ModerationLog) :: String.t()
416 def get_log_entry_message(%ModerationLog{
417 data: %{
418 "actor" => %{"nickname" => actor_nickname},
419 "nicknames" => nicknames,
420 "tags" => tags,
421 "action" => "untag"
422 }
423 }) do
424 nicknames_string =
425 nicknames
426 |> Enum.map(&"@#{&1}")
427 |> Enum.join(", ")
428
429 tags_string = tags |> Enum.join(", ")
430
431 "@#{actor_nickname} removed tags: #{tags_string} from users: #{nicknames_string}"
432 end
433
434 @spec get_log_entry_message(ModerationLog) :: String.t()
435 def get_log_entry_message(%ModerationLog{
436 data: %{
437 "actor" => %{"nickname" => actor_nickname},
438 "action" => "grant",
439 "subject" => %{"nickname" => subject_nickname},
440 "permission" => permission
441 }
442 }) do
443 "@#{actor_nickname} made @#{subject_nickname} #{permission}"
444 end
445
446 @spec get_log_entry_message(ModerationLog) :: String.t()
447 def get_log_entry_message(%ModerationLog{
448 data: %{
449 "actor" => %{"nickname" => actor_nickname},
450 "action" => "revoke",
451 "subject" => %{"nickname" => subject_nickname},
452 "permission" => permission
453 }
454 }) do
455 "@#{actor_nickname} revoked #{permission} role from @#{subject_nickname}"
456 end
457
458 @spec get_log_entry_message(ModerationLog) :: String.t()
459 def get_log_entry_message(%ModerationLog{
460 data: %{
461 "actor" => %{"nickname" => actor_nickname},
462 "action" => "relay_follow",
463 "target" => target
464 }
465 }) do
466 "@#{actor_nickname} followed relay: #{target}"
467 end
468
469 @spec get_log_entry_message(ModerationLog) :: String.t()
470 def get_log_entry_message(%ModerationLog{
471 data: %{
472 "actor" => %{"nickname" => actor_nickname},
473 "action" => "relay_unfollow",
474 "target" => target
475 }
476 }) do
477 "@#{actor_nickname} unfollowed relay: #{target}"
478 end
479
480 @spec get_log_entry_message(ModerationLog) :: String.t()
481 def get_log_entry_message(%ModerationLog{
482 data: %{
483 "actor" => %{"nickname" => actor_nickname},
484 "action" => "report_update",
485 "subject" => %{"id" => subject_id, "state" => state, "type" => "report"}
486 }
487 }) do
488 "@#{actor_nickname} updated report ##{subject_id} with '#{state}' state"
489 end
490
491 @spec get_log_entry_message(ModerationLog) :: String.t()
492 def get_log_entry_message(%ModerationLog{
493 data: %{
494 "actor" => %{"nickname" => actor_nickname},
495 "action" => "report_response",
496 "subject" => %{"id" => subject_id, "type" => "report"},
497 "text" => text
498 }
499 }) do
500 "@#{actor_nickname} responded with '#{text}' to report ##{subject_id}"
501 end
502
503 @spec get_log_entry_message(ModerationLog) :: String.t()
504 def get_log_entry_message(%ModerationLog{
505 data: %{
506 "actor" => %{"nickname" => actor_nickname},
507 "action" => "status_update",
508 "subject" => %{"id" => subject_id, "type" => "status"},
509 "sensitive" => nil,
510 "visibility" => visibility
511 }
512 }) do
513 "@#{actor_nickname} updated status ##{subject_id}, set visibility: '#{visibility}'"
514 end
515
516 @spec get_log_entry_message(ModerationLog) :: String.t()
517 def get_log_entry_message(%ModerationLog{
518 data: %{
519 "actor" => %{"nickname" => actor_nickname},
520 "action" => "status_update",
521 "subject" => %{"id" => subject_id, "type" => "status"},
522 "sensitive" => sensitive,
523 "visibility" => nil
524 }
525 }) do
526 "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}'"
527 end
528
529 @spec get_log_entry_message(ModerationLog) :: String.t()
530 def get_log_entry_message(%ModerationLog{
531 data: %{
532 "actor" => %{"nickname" => actor_nickname},
533 "action" => "status_update",
534 "subject" => %{"id" => subject_id, "type" => "status"},
535 "sensitive" => sensitive,
536 "visibility" => visibility
537 }
538 }) do
539 "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}', visibility: '#{
540 visibility
541 }'"
542 end
543
544 @spec get_log_entry_message(ModerationLog) :: String.t()
545 def get_log_entry_message(%ModerationLog{
546 data: %{
547 "actor" => %{"nickname" => actor_nickname},
548 "action" => "status_delete",
549 "subject_id" => subject_id
550 }
551 }) do
552 "@#{actor_nickname} deleted status ##{subject_id}"
553 end
554 end