1 defmodule Pleroma.ModerationLog do
5 alias Pleroma.ModerationLog
11 schema "moderation_log" do
17 def get_all(page, page_size) do
19 order_by: [desc: q.inserted_at],
21 offset: ^((page - 1) * page_size)
27 actor: %User{} = actor,
28 subject: %User{} = subject,
30 permission: permission
32 Repo.insert(%ModerationLog{
34 actor: user_to_map(actor),
35 subject: user_to_map(subject),
37 permission: permission
43 actor: %User{} = actor,
44 action: "report_update",
45 subject: %Activity{data: %{"type" => "Flag"}} = subject
47 Repo.insert(%ModerationLog{
49 actor: user_to_map(actor),
50 action: "report_update",
51 subject: report_to_map(subject)
57 actor: %User{} = actor,
58 action: "report_response",
59 subject: %Activity{} = subject,
62 Repo.insert(%ModerationLog{
64 actor: user_to_map(actor),
65 action: "report_response",
66 subject: report_to_map(subject),
73 actor: %User{} = actor,
74 action: "status_update",
75 subject: %Activity{} = subject,
77 visibility: visibility
79 Repo.insert(%ModerationLog{
81 actor: user_to_map(actor),
82 action: "status_update",
83 subject: status_to_map(subject),
85 visibility: visibility
91 actor: %User{} = actor,
92 action: "status_delete",
93 subject_id: subject_id
95 Repo.insert(%ModerationLog{
97 actor: user_to_map(actor),
98 action: "status_delete",
99 subject_id: subject_id
104 @spec insert_log(%{actor: User, subject: User, action: String.t()}) ::
105 {:ok, ModerationLog} | {:error, any}
106 def insert_log(%{actor: %User{} = actor, subject: subject, action: action}) do
107 Repo.insert(%ModerationLog{
109 actor: user_to_map(actor),
111 subject: user_to_map(subject)
116 @spec insert_log(%{actor: User, subjects: [User], action: String.t()}) ::
117 {:ok, ModerationLog} | {:error, any}
118 def insert_log(%{actor: %User{} = actor, subjects: subjects, action: action}) do
119 subjects = Enum.map(subjects, &user_to_map/1)
121 Repo.insert(%ModerationLog{
123 actor: user_to_map(actor),
131 actor: %User{} = actor,
132 followed: %User{} = followed,
133 follower: %User{} = follower,
136 Repo.insert(%ModerationLog{
138 actor: user_to_map(actor),
140 followed: user_to_map(followed),
141 follower: user_to_map(follower)
147 actor: %User{} = actor,
148 followed: %User{} = followed,
149 follower: %User{} = follower,
152 Repo.insert(%ModerationLog{
154 actor: user_to_map(actor),
156 followed: user_to_map(followed),
157 follower: user_to_map(follower)
163 actor: %User{} = actor,
164 nicknames: nicknames,
168 Repo.insert(%ModerationLog{
170 actor: user_to_map(actor),
171 nicknames: nicknames,
179 actor: %User{} = actor,
183 when action in ["relay_follow", "relay_unfollow"] do
184 Repo.insert(%ModerationLog{
186 actor: user_to_map(actor),
193 defp user_to_map(%User{} = user) do
196 |> Map.take([:id, :nickname])
197 |> Map.put(:type, "user")
200 defp report_to_map(%Activity{} = report) do
204 state: report.data["state"]
208 defp status_to_map(%Activity{} = status) do
215 def get_log_entry_message(%ModerationLog{
217 "actor" => %{"nickname" => actor_nickname},
219 "followed" => %{"nickname" => followed_nickname},
220 "follower" => %{"nickname" => follower_nickname}
223 "@#{actor_nickname} made @#{follower_nickname} #{action} @#{followed_nickname}"
226 @spec get_log_entry_message(ModerationLog) :: String.t()
227 def get_log_entry_message(%ModerationLog{
229 "actor" => %{"nickname" => actor_nickname},
230 "action" => "delete",
231 "subject" => %{"nickname" => subject_nickname, "type" => "user"}
234 "@#{actor_nickname} deleted user @#{subject_nickname}"
237 @spec get_log_entry_message(ModerationLog) :: String.t()
238 def get_log_entry_message(%ModerationLog{
240 "actor" => %{"nickname" => actor_nickname},
241 "action" => "create",
242 "subjects" => subjects
247 |> Enum.map(&"@#{&1["nickname"]}")
250 "@#{actor_nickname} created users: #{nicknames}"
253 @spec get_log_entry_message(ModerationLog) :: String.t()
254 def get_log_entry_message(%ModerationLog{
256 "actor" => %{"nickname" => actor_nickname},
257 "action" => "activate",
258 "subject" => %{"nickname" => subject_nickname, "type" => "user"}
261 "@#{actor_nickname} activated user @#{subject_nickname}"
264 @spec get_log_entry_message(ModerationLog) :: String.t()
265 def get_log_entry_message(%ModerationLog{
267 "actor" => %{"nickname" => actor_nickname},
268 "action" => "deactivate",
269 "subject" => %{"nickname" => subject_nickname, "type" => "user"}
272 "@#{actor_nickname} deactivated user @#{subject_nickname}"
275 @spec get_log_entry_message(ModerationLog) :: String.t()
276 def get_log_entry_message(%ModerationLog{
278 "actor" => %{"nickname" => actor_nickname},
279 "nicknames" => nicknames,
286 |> Enum.map(&"@#{&1}")
289 tags_string = tags |> Enum.join(", ")
291 "@#{actor_nickname} added tags: #{tags_string} to users: #{nicknames_string}"
294 @spec get_log_entry_message(ModerationLog) :: String.t()
295 def get_log_entry_message(%ModerationLog{
297 "actor" => %{"nickname" => actor_nickname},
298 "nicknames" => nicknames,
305 |> Enum.map(&"@#{&1}")
308 tags_string = tags |> Enum.join(", ")
310 "@#{actor_nickname} removed tags: #{tags_string} from users: #{nicknames_string}"
313 @spec get_log_entry_message(ModerationLog) :: String.t()
314 def get_log_entry_message(%ModerationLog{
316 "actor" => %{"nickname" => actor_nickname},
318 "subject" => %{"nickname" => subject_nickname},
319 "permission" => permission
322 "@#{actor_nickname} made @#{subject_nickname} #{permission}"
325 @spec get_log_entry_message(ModerationLog) :: String.t()
326 def get_log_entry_message(%ModerationLog{
328 "actor" => %{"nickname" => actor_nickname},
329 "action" => "revoke",
330 "subject" => %{"nickname" => subject_nickname},
331 "permission" => permission
334 "@#{actor_nickname} revoked #{permission} role from @#{subject_nickname}"
337 @spec get_log_entry_message(ModerationLog) :: String.t()
338 def get_log_entry_message(%ModerationLog{
340 "actor" => %{"nickname" => actor_nickname},
341 "action" => "relay_follow",
345 "@#{actor_nickname} followed relay: #{target}"
348 @spec get_log_entry_message(ModerationLog) :: String.t()
349 def get_log_entry_message(%ModerationLog{
351 "actor" => %{"nickname" => actor_nickname},
352 "action" => "relay_unfollow",
356 "@#{actor_nickname} unfollowed relay: #{target}"
359 @spec get_log_entry_message(ModerationLog) :: String.t()
360 def get_log_entry_message(%ModerationLog{
362 "actor" => %{"nickname" => actor_nickname},
363 "action" => "report_update",
364 "subject" => %{"id" => subject_id, "state" => state, "type" => "report"}
367 "@#{actor_nickname} updated report ##{subject_id} with '#{state}' state"
370 @spec get_log_entry_message(ModerationLog) :: String.t()
371 def get_log_entry_message(%ModerationLog{
373 "actor" => %{"nickname" => actor_nickname},
374 "action" => "report_response",
375 "subject" => %{"id" => subject_id, "type" => "report"},
379 "@#{actor_nickname} responded with '#{text}' to report ##{subject_id}"
382 @spec get_log_entry_message(ModerationLog) :: String.t()
383 def get_log_entry_message(%ModerationLog{
385 "actor" => %{"nickname" => actor_nickname},
386 "action" => "status_update",
387 "subject" => %{"id" => subject_id, "type" => "status"},
389 "visibility" => visibility
392 "@#{actor_nickname} updated status ##{subject_id}, set visibility: '#{visibility}'"
395 @spec get_log_entry_message(ModerationLog) :: String.t()
396 def get_log_entry_message(%ModerationLog{
398 "actor" => %{"nickname" => actor_nickname},
399 "action" => "status_update",
400 "subject" => %{"id" => subject_id, "type" => "status"},
401 "sensitive" => sensitive,
405 "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}'"
408 @spec get_log_entry_message(ModerationLog) :: String.t()
409 def get_log_entry_message(%ModerationLog{
411 "actor" => %{"nickname" => actor_nickname},
412 "action" => "status_update",
413 "subject" => %{"id" => subject_id, "type" => "status"},
414 "sensitive" => sensitive,
415 "visibility" => visibility
418 "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}', visibility: '#{
423 @spec get_log_entry_message(ModerationLog) :: String.t()
424 def get_log_entry_message(%ModerationLog{
426 "actor" => %{"nickname" => actor_nickname},
427 "action" => "status_delete",
428 "subject_id" => subject_id
431 "@#{actor_nickname} deleted status ##{subject_id}"