Merge branch 'develop' into feature/multiple-users-activation-permissions
[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_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 defp insert_log_entry_with_message(entry) do
307 entry.data["message"]
308 |> put_in(get_log_entry_message(entry))
309 |> Repo.insert()
310 end
311
312 defp user_to_map(users) when is_list(users) do
313 users |> Enum.map(&user_to_map/1)
314 end
315
316 defp user_to_map(%User{} = user) do
317 user
318 |> Map.from_struct()
319 |> Map.take([:id, :nickname])
320 |> Map.new(fn {k, v} -> {Atom.to_string(k), v} end)
321 |> Map.put("type", "user")
322 end
323
324 defp report_to_map(%Activity{} = report) do
325 %{
326 "type" => "report",
327 "id" => report.id,
328 "state" => report.data["state"]
329 }
330 end
331
332 defp status_to_map(%Activity{} = status) do
333 %{
334 "type" => "status",
335 "id" => status.id
336 }
337 end
338
339 def get_log_entry_message(%ModerationLog{
340 data: %{
341 "actor" => %{"nickname" => actor_nickname},
342 "action" => action,
343 "followed" => %{"nickname" => followed_nickname},
344 "follower" => %{"nickname" => follower_nickname}
345 }
346 }) do
347 "@#{actor_nickname} made @#{follower_nickname} #{action} @#{followed_nickname}"
348 end
349
350 @spec get_log_entry_message(ModerationLog) :: String.t()
351 def get_log_entry_message(%ModerationLog{
352 data: %{
353 "actor" => %{"nickname" => actor_nickname},
354 "action" => "delete",
355 "subject" => %{"nickname" => subject_nickname, "type" => "user"}
356 }
357 }) do
358 "@#{actor_nickname} deleted user @#{subject_nickname}"
359 end
360
361 @spec get_log_entry_message(ModerationLog) :: String.t()
362 def get_log_entry_message(%ModerationLog{
363 data: %{
364 "actor" => %{"nickname" => actor_nickname},
365 "action" => "create",
366 "subjects" => subjects
367 }
368 }) do
369 "@#{actor_nickname} created users: #{users_to_nicknames_string(subjects)}"
370 end
371
372 @spec get_log_entry_message(ModerationLog) :: String.t()
373 def get_log_entry_message(%ModerationLog{
374 data: %{
375 "actor" => %{"nickname" => actor_nickname},
376 "action" => "activate",
377 "subject" => users
378 }
379 }) do
380 "@#{actor_nickname} activated users: #{users_to_nicknames_string(users)}"
381 end
382
383 @spec get_log_entry_message(ModerationLog) :: String.t()
384 def get_log_entry_message(%ModerationLog{
385 data: %{
386 "actor" => %{"nickname" => actor_nickname},
387 "action" => "deactivate",
388 "subject" => users
389 }
390 }) do
391 "@#{actor_nickname} deactivated users: #{users_to_nicknames_string(users)}"
392 end
393
394 @spec get_log_entry_message(ModerationLog) :: String.t()
395 def get_log_entry_message(%ModerationLog{
396 data: %{
397 "actor" => %{"nickname" => actor_nickname},
398 "nicknames" => nicknames,
399 "tags" => tags,
400 "action" => "tag"
401 }
402 }) do
403 tags_string = tags |> Enum.join(", ")
404
405 "@#{actor_nickname} added tags: #{tags_string} to users: #{nicknames_to_string(nicknames)}"
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 "nicknames" => nicknames,
413 "tags" => tags,
414 "action" => "untag"
415 }
416 }) do
417 tags_string = tags |> Enum.join(", ")
418
419 "@#{actor_nickname} removed tags: #{tags_string} from users: #{nicknames_to_string(nicknames)}"
420 end
421
422 @spec get_log_entry_message(ModerationLog) :: String.t()
423 def get_log_entry_message(%ModerationLog{
424 data: %{
425 "actor" => %{"nickname" => actor_nickname},
426 "action" => "grant",
427 "subject" => users,
428 "permission" => permission
429 }
430 }) do
431 "@#{actor_nickname} made #{users_to_nicknames_string(users)} #{permission}"
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" => "revoke",
439 "subject" => users,
440 "permission" => permission
441 }
442 }) do
443 "@#{actor_nickname} revoked #{permission} role from #{users_to_nicknames_string(users)}"
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" => "relay_follow",
451 "target" => target
452 }
453 }) do
454 "@#{actor_nickname} followed relay: #{target}"
455 end
456
457 @spec get_log_entry_message(ModerationLog) :: String.t()
458 def get_log_entry_message(%ModerationLog{
459 data: %{
460 "actor" => %{"nickname" => actor_nickname},
461 "action" => "relay_unfollow",
462 "target" => target
463 }
464 }) do
465 "@#{actor_nickname} unfollowed relay: #{target}"
466 end
467
468 @spec get_log_entry_message(ModerationLog) :: String.t()
469 def get_log_entry_message(%ModerationLog{
470 data: %{
471 "actor" => %{"nickname" => actor_nickname},
472 "action" => "report_update",
473 "subject" => %{"id" => subject_id, "state" => state, "type" => "report"}
474 }
475 }) do
476 "@#{actor_nickname} updated report ##{subject_id} with '#{state}' state"
477 end
478
479 @spec get_log_entry_message(ModerationLog) :: String.t()
480 def get_log_entry_message(%ModerationLog{
481 data: %{
482 "actor" => %{"nickname" => actor_nickname},
483 "action" => "report_response",
484 "subject" => %{"id" => subject_id, "type" => "report"},
485 "text" => text
486 }
487 }) do
488 "@#{actor_nickname} responded with '#{text}' to report ##{subject_id}"
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" => "status_update",
496 "subject" => %{"id" => subject_id, "type" => "status"},
497 "sensitive" => nil,
498 "visibility" => visibility
499 }
500 }) do
501 "@#{actor_nickname} updated status ##{subject_id}, set visibility: '#{visibility}'"
502 end
503
504 @spec get_log_entry_message(ModerationLog) :: String.t()
505 def get_log_entry_message(%ModerationLog{
506 data: %{
507 "actor" => %{"nickname" => actor_nickname},
508 "action" => "status_update",
509 "subject" => %{"id" => subject_id, "type" => "status"},
510 "sensitive" => sensitive,
511 "visibility" => nil
512 }
513 }) do
514 "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}'"
515 end
516
517 @spec get_log_entry_message(ModerationLog) :: String.t()
518 def get_log_entry_message(%ModerationLog{
519 data: %{
520 "actor" => %{"nickname" => actor_nickname},
521 "action" => "status_update",
522 "subject" => %{"id" => subject_id, "type" => "status"},
523 "sensitive" => sensitive,
524 "visibility" => visibility
525 }
526 }) do
527 "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}', visibility: '#{
528 visibility
529 }'"
530 end
531
532 @spec get_log_entry_message(ModerationLog) :: String.t()
533 def get_log_entry_message(%ModerationLog{
534 data: %{
535 "actor" => %{"nickname" => actor_nickname},
536 "action" => "status_delete",
537 "subject_id" => subject_id
538 }
539 }) do
540 "@#{actor_nickname} deleted status ##{subject_id}"
541 end
542
543 defp nicknames_to_string(nicknames) do
544 nicknames
545 |> Enum.map(&"@#{&1}")
546 |> Enum.join(", ")
547 end
548
549 defp users_to_nicknames_string(users) do
550 users
551 |> Enum.map(&"@#{&1["nickname"]}")
552 |> Enum.join(", ")
553 end
554 end