moderation_log.ex 17.8 KB
Newer Older
Maxim Filippov's avatar
Maxim Filippov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
defmodule Pleroma.ModerationLog do
  use Ecto.Schema

  alias Pleroma.Activity
  alias Pleroma.ModerationLog
  alias Pleroma.Repo
  alias Pleroma.User

  import Ecto.Query

  schema "moderation_log" do
    field(:data, :map)

    timestamps()
  end

Maxim Filippov's avatar
Maxim Filippov committed
17
  def get_all(params) do
Maxim Filippov's avatar
Maxim Filippov committed
18 19 20 21 22 23 24 25 26 27 28 29
    base_query =
      get_all_query()
      |> maybe_filter_by_date(params)
      |> maybe_filter_by_user(params)
      |> maybe_filter_by_search(params)

    query_with_pagination = base_query |> paginate_query(params)

    %{
      items: Repo.all(query_with_pagination),
      count: Repo.aggregate(base_query, :count, :id)
    }
Maxim Filippov's avatar
Maxim Filippov committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  end

  defp maybe_filter_by_date(query, %{start_date: nil, end_date: nil}), do: query

  defp maybe_filter_by_date(query, %{start_date: start_date, end_date: nil}) do
    from(q in query,
      where: q.inserted_at >= ^parse_datetime(start_date)
    )
  end

  defp maybe_filter_by_date(query, %{start_date: nil, end_date: end_date}) do
    from(q in query,
      where: q.inserted_at <= ^parse_datetime(end_date)
    )
  end

  defp maybe_filter_by_date(query, %{start_date: start_date, end_date: end_date}) do
    from(q in query,
      where: q.inserted_at >= ^parse_datetime(start_date),
      where: q.inserted_at <= ^parse_datetime(end_date)
    )
  end

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  defp maybe_filter_by_user(query, %{user_id: nil}), do: query

  defp maybe_filter_by_user(query, %{user_id: user_id}) do
    from(q in query,
      where: fragment("(?)->'actor'->>'id' = ?", q.data, ^user_id)
    )
  end

  defp maybe_filter_by_search(query, %{search: search}) when is_nil(search) or search == "",
    do: query

  defp maybe_filter_by_search(query, %{search: search}) do
    from(q in query,
      where: fragment("(?)->>'message' ILIKE ?", q.data, ^"%#{search}%")
    )
  end

Maxim Filippov's avatar
Maxim Filippov committed
70 71
  defp paginate_query(query, %{page: page, page_size: page_size}) do
    from(q in query,
Maxim Filippov's avatar
Maxim Filippov committed
72 73 74
      limit: ^page_size,
      offset: ^((page - 1) * page_size)
    )
Maxim Filippov's avatar
Maxim Filippov committed
75 76
  end

Maxim Filippov's avatar
Maxim Filippov committed
77 78 79 80 81 82
  defp get_all_query do
    from(q in __MODULE__,
      order_by: [desc: q.inserted_at]
    )
  end

Maxim Filippov's avatar
Maxim Filippov committed
83 84 85 86
  defp parse_datetime(datetime) do
    {:ok, parsed_datetime, _} = DateTime.from_iso8601(datetime)

    parsed_datetime
Maxim Filippov's avatar
Maxim Filippov committed
87 88
  end

89
  @spec insert_log(%{actor: User, subject: [User], action: String.t(), permission: String.t()}) ::
90
          {:ok, ModerationLog} | {:error, any}
Maxim Filippov's avatar
Maxim Filippov committed
91 92
  def insert_log(%{
        actor: %User{} = actor,
93
        subject: subjects,
Maxim Filippov's avatar
Maxim Filippov committed
94 95 96
        action: action,
        permission: permission
      }) do
97
    %ModerationLog{
Maxim Filippov's avatar
Maxim Filippov committed
98
      data: %{
99
        "actor" => user_to_map(actor),
100
        "subject" => user_to_map(subjects),
101 102 103
        "action" => action,
        "permission" => permission,
        "message" => ""
Maxim Filippov's avatar
Maxim Filippov committed
104
      }
105 106
    }
    |> insert_log_entry_with_message()
Maxim Filippov's avatar
Maxim Filippov committed
107 108
  end

109 110
  @spec insert_log(%{actor: User, subject: User, action: String.t()}) ::
          {:ok, ModerationLog} | {:error, any}
Maxim Filippov's avatar
Maxim Filippov committed
111 112 113 114 115
  def insert_log(%{
        actor: %User{} = actor,
        action: "report_update",
        subject: %Activity{data: %{"type" => "Flag"}} = subject
      }) do
116
    %ModerationLog{
Maxim Filippov's avatar
Maxim Filippov committed
117
      data: %{
118 119 120 121
        "actor" => user_to_map(actor),
        "action" => "report_update",
        "subject" => report_to_map(subject),
        "message" => ""
Maxim Filippov's avatar
Maxim Filippov committed
122
      }
123 124
    }
    |> insert_log_entry_with_message()
Maxim Filippov's avatar
Maxim Filippov committed
125 126
  end

127 128
  @spec insert_log(%{actor: User, subject: Activity, action: String.t(), text: String.t()}) ::
          {:ok, ModerationLog} | {:error, any}
Maxim Filippov's avatar
Maxim Filippov committed
129 130
  def insert_log(%{
        actor: %User{} = actor,
131
        action: "report_note",
Maxim Filippov's avatar
Maxim Filippov committed
132 133 134
        subject: %Activity{} = subject,
        text: text
      }) do
135
    %ModerationLog{
Maxim Filippov's avatar
Maxim Filippov committed
136
      data: %{
137
        "actor" => user_to_map(actor),
138
        "action" => "report_note",
139
        "subject" => report_to_map(subject),
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
        "text" => text
      }
    }
    |> insert_log_entry_with_message()
  end

  @spec insert_log(%{actor: User, subject: Activity, action: String.t(), text: String.t()}) ::
          {:ok, ModerationLog} | {:error, any}
  def insert_log(%{
        actor: %User{} = actor,
        action: "report_note_delete",
        subject: %Activity{} = subject,
        text: text
      }) do
    %ModerationLog{
      data: %{
        "actor" => user_to_map(actor),
        "action" => "report_note_delete",
        "subject" => report_to_map(subject),
        "text" => text
Maxim Filippov's avatar
Maxim Filippov committed
160
      }
161 162
    }
    |> insert_log_entry_with_message()
Maxim Filippov's avatar
Maxim Filippov committed
163 164
  end

165 166 167 168 169 170 171
  @spec insert_log(%{
          actor: User,
          subject: Activity,
          action: String.t(),
          sensitive: String.t(),
          visibility: String.t()
        }) :: {:ok, ModerationLog} | {:error, any}
Maxim Filippov's avatar
Maxim Filippov committed
172 173 174 175 176 177 178
  def insert_log(%{
        actor: %User{} = actor,
        action: "status_update",
        subject: %Activity{} = subject,
        sensitive: sensitive,
        visibility: visibility
      }) do
179
    %ModerationLog{
Maxim Filippov's avatar
Maxim Filippov committed
180
      data: %{
181 182 183 184 185 186
        "actor" => user_to_map(actor),
        "action" => "status_update",
        "subject" => status_to_map(subject),
        "sensitive" => sensitive,
        "visibility" => visibility,
        "message" => ""
Maxim Filippov's avatar
Maxim Filippov committed
187
      }
188 189
    }
    |> insert_log_entry_with_message()
Maxim Filippov's avatar
Maxim Filippov committed
190 191
  end

192 193
  @spec insert_log(%{actor: User, action: String.t(), subject_id: String.t()}) ::
          {:ok, ModerationLog} | {:error, any}
Maxim Filippov's avatar
Maxim Filippov committed
194 195 196 197 198
  def insert_log(%{
        actor: %User{} = actor,
        action: "status_delete",
        subject_id: subject_id
      }) do
199
    %ModerationLog{
Maxim Filippov's avatar
Maxim Filippov committed
200
      data: %{
201 202 203 204
        "actor" => user_to_map(actor),
        "action" => "status_delete",
        "subject_id" => subject_id,
        "message" => ""
Maxim Filippov's avatar
Maxim Filippov committed
205
      }
206 207
    }
    |> insert_log_entry_with_message()
Maxim Filippov's avatar
Maxim Filippov committed
208 209 210 211 212
  end

  @spec insert_log(%{actor: User, subject: User, action: String.t()}) ::
          {:ok, ModerationLog} | {:error, any}
  def insert_log(%{actor: %User{} = actor, subject: subject, action: action}) do
213
    %ModerationLog{
Maxim Filippov's avatar
Maxim Filippov committed
214
      data: %{
215 216 217 218
        "actor" => user_to_map(actor),
        "action" => action,
        "subject" => user_to_map(subject),
        "message" => ""
Maxim Filippov's avatar
Maxim Filippov committed
219
      }
220 221
    }
    |> insert_log_entry_with_message()
Maxim Filippov's avatar
Maxim Filippov committed
222 223 224 225 226 227 228
  end

  @spec insert_log(%{actor: User, subjects: [User], action: String.t()}) ::
          {:ok, ModerationLog} | {:error, any}
  def insert_log(%{actor: %User{} = actor, subjects: subjects, action: action}) do
    subjects = Enum.map(subjects, &user_to_map/1)

229
    %ModerationLog{
Maxim Filippov's avatar
Maxim Filippov committed
230
      data: %{
231 232 233 234
        "actor" => user_to_map(actor),
        "action" => action,
        "subjects" => subjects,
        "message" => ""
Maxim Filippov's avatar
Maxim Filippov committed
235
      }
236 237
    }
    |> insert_log_entry_with_message()
Maxim Filippov's avatar
Maxim Filippov committed
238 239
  end

240 241
  @spec insert_log(%{actor: User, action: String.t(), followed: User, follower: User}) ::
          {:ok, ModerationLog} | {:error, any}
Maxim Filippov's avatar
Maxim Filippov committed
242 243 244 245 246 247
  def insert_log(%{
        actor: %User{} = actor,
        followed: %User{} = followed,
        follower: %User{} = follower,
        action: "follow"
      }) do
248
    %ModerationLog{
Maxim Filippov's avatar
Maxim Filippov committed
249
      data: %{
250 251 252 253 254
        "actor" => user_to_map(actor),
        "action" => "follow",
        "followed" => user_to_map(followed),
        "follower" => user_to_map(follower),
        "message" => ""
Maxim Filippov's avatar
Maxim Filippov committed
255
      }
256 257
    }
    |> insert_log_entry_with_message()
Maxim Filippov's avatar
Maxim Filippov committed
258 259
  end

260 261
  @spec insert_log(%{actor: User, action: String.t(), followed: User, follower: User}) ::
          {:ok, ModerationLog} | {:error, any}
Maxim Filippov's avatar
Maxim Filippov committed
262 263 264 265 266 267
  def insert_log(%{
        actor: %User{} = actor,
        followed: %User{} = followed,
        follower: %User{} = follower,
        action: "unfollow"
      }) do
268
    %ModerationLog{
Maxim Filippov's avatar
Maxim Filippov committed
269
      data: %{
270 271 272 273 274
        "actor" => user_to_map(actor),
        "action" => "unfollow",
        "followed" => user_to_map(followed),
        "follower" => user_to_map(follower),
        "message" => ""
Maxim Filippov's avatar
Maxim Filippov committed
275
      }
276 277
    }
    |> insert_log_entry_with_message()
Maxim Filippov's avatar
Maxim Filippov committed
278 279
  end

Maxim Filippov's avatar
Style  
Maxim Filippov committed
280 281 282 283 284 285
  @spec insert_log(%{
          actor: User,
          action: String.t(),
          nicknames: [String.t()],
          tags: [String.t()]
        }) :: {:ok, ModerationLog} | {:error, any}
Maxim Filippov's avatar
Maxim Filippov committed
286 287 288 289 290 291
  def insert_log(%{
        actor: %User{} = actor,
        nicknames: nicknames,
        tags: tags,
        action: action
      }) do
292
    %ModerationLog{
Maxim Filippov's avatar
Maxim Filippov committed
293
      data: %{
294 295 296 297 298
        "actor" => user_to_map(actor),
        "nicknames" => nicknames,
        "tags" => tags,
        "action" => action,
        "message" => ""
Maxim Filippov's avatar
Maxim Filippov committed
299
      }
300 301
    }
    |> insert_log_entry_with_message()
Maxim Filippov's avatar
Maxim Filippov committed
302 303
  end

304 305
  @spec insert_log(%{actor: User, action: String.t(), target: String.t()}) ::
          {:ok, ModerationLog} | {:error, any}
Maxim Filippov's avatar
Maxim Filippov committed
306 307 308 309 310 311
  def insert_log(%{
        actor: %User{} = actor,
        action: action,
        target: target
      })
      when action in ["relay_follow", "relay_unfollow"] do
312
    %ModerationLog{
Maxim Filippov's avatar
Maxim Filippov committed
313
      data: %{
314 315 316 317
        "actor" => user_to_map(actor),
        "action" => action,
        "target" => target,
        "message" => ""
Maxim Filippov's avatar
Maxim Filippov committed
318
      }
319 320 321 322 323 324 325 326 327
    }
    |> insert_log_entry_with_message()
  end

  @spec insert_log_entry_with_message(ModerationLog) :: {:ok, ModerationLog} | {:error, any}
  defp insert_log_entry_with_message(entry) do
    entry.data["message"]
    |> put_in(get_log_entry_message(entry))
    |> Repo.insert()
Maxim Filippov's avatar
Maxim Filippov committed
328 329
  end

330 331 332 333
  defp user_to_map(users) when is_list(users) do
    users |> Enum.map(&user_to_map/1)
  end

Maxim Filippov's avatar
Maxim Filippov committed
334 335 336 337
  defp user_to_map(%User{} = user) do
    user
    |> Map.from_struct()
    |> Map.take([:id, :nickname])
338 339
    |> Map.new(fn {k, v} -> {Atom.to_string(k), v} end)
    |> Map.put("type", "user")
Maxim Filippov's avatar
Maxim Filippov committed
340 341 342 343
  end

  defp report_to_map(%Activity{} = report) do
    %{
344 345 346
      "type" => "report",
      "id" => report.id,
      "state" => report.data["state"]
Maxim Filippov's avatar
Maxim Filippov committed
347 348 349 350 351
    }
  end

  defp status_to_map(%Activity{} = status) do
    %{
352 353
      "type" => "status",
      "id" => status.id
Maxim Filippov's avatar
Maxim Filippov committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    }
  end

  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "action" => action,
          "followed" => %{"nickname" => followed_nickname},
          "follower" => %{"nickname" => follower_nickname}
        }
      }) do
    "@#{actor_nickname} made @#{follower_nickname} #{action} @#{followed_nickname}"
  end

  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "action" => "delete",
373
          "subject" => subjects
Maxim Filippov's avatar
Maxim Filippov committed
374 375
        }
      }) do
376
    "@#{actor_nickname} deleted users: #{users_to_nicknames_string(subjects)}"
Maxim Filippov's avatar
Maxim Filippov committed
377 378 379 380 381 382 383 384 385 386
  end

  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "action" => "create",
          "subjects" => subjects
        }
      }) do
387
    "@#{actor_nickname} created users: #{users_to_nicknames_string(subjects)}"
Maxim Filippov's avatar
Maxim Filippov committed
388 389 390 391 392 393 394
  end

  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "action" => "activate",
395
          "subject" => users
Maxim Filippov's avatar
Maxim Filippov committed
396 397
        }
      }) do
398
    "@#{actor_nickname} activated users: #{users_to_nicknames_string(users)}"
Maxim Filippov's avatar
Maxim Filippov committed
399 400 401 402 403 404 405
  end

  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "action" => "deactivate",
406
          "subject" => users
Maxim Filippov's avatar
Maxim Filippov committed
407 408
        }
      }) do
409
    "@#{actor_nickname} deactivated users: #{users_to_nicknames_string(users)}"
Maxim Filippov's avatar
Maxim Filippov committed
410 411 412 413 414 415 416 417 418 419 420 421 422
  end

  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "nicknames" => nicknames,
          "tags" => tags,
          "action" => "tag"
        }
      }) do
    tags_string = tags |> Enum.join(", ")

423
    "@#{actor_nickname} added tags: #{tags_string} to users: #{nicknames_to_string(nicknames)}"
Maxim Filippov's avatar
Maxim Filippov committed
424 425 426 427 428 429 430 431 432 433 434 435 436
  end

  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "nicknames" => nicknames,
          "tags" => tags,
          "action" => "untag"
        }
      }) do
    tags_string = tags |> Enum.join(", ")

437
    "@#{actor_nickname} removed tags: #{tags_string} from users: #{nicknames_to_string(nicknames)}"
Maxim Filippov's avatar
Maxim Filippov committed
438 439 440 441 442 443 444
  end

  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "action" => "grant",
445
          "subject" => users,
Maxim Filippov's avatar
Maxim Filippov committed
446 447 448
          "permission" => permission
        }
      }) do
449
    "@#{actor_nickname} made #{users_to_nicknames_string(users)} #{permission}"
Maxim Filippov's avatar
Maxim Filippov committed
450 451 452 453 454 455 456
  end

  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "action" => "revoke",
457
          "subject" => users,
Maxim Filippov's avatar
Maxim Filippov committed
458 459 460
          "permission" => permission
        }
      }) do
461
    "@#{actor_nickname} revoked #{permission} role from #{users_to_nicknames_string(users)}"
Maxim Filippov's avatar
Maxim Filippov committed
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
  end

  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "action" => "relay_follow",
          "target" => target
        }
      }) do
    "@#{actor_nickname} followed relay: #{target}"
  end

  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "action" => "relay_unfollow",
          "target" => target
        }
      }) do
    "@#{actor_nickname} unfollowed relay: #{target}"
  end

  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "action" => "report_update",
          "subject" => %{"id" => subject_id, "state" => state, "type" => "report"}
        }
      }) do
    "@#{actor_nickname} updated report ##{subject_id} with '#{state}' state"
  end

  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
501 502 503 504 505 506 507 508 509 510 511 512 513
          "action" => "report_note",
          "subject" => %{"id" => subject_id, "type" => "report"},
          "text" => text
        }
      }) do
    "@#{actor_nickname} added note '#{text}' to report ##{subject_id}"
  end

  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "action" => "report_note_delete",
Maxim Filippov's avatar
Maxim Filippov committed
514 515 516 517
          "subject" => %{"id" => subject_id, "type" => "report"},
          "text" => text
        }
      }) do
518
    "@#{actor_nickname} deleted note '#{text}' from report ##{subject_id}"
Maxim Filippov's avatar
Maxim Filippov committed
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
  end

  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "action" => "status_update",
          "subject" => %{"id" => subject_id, "type" => "status"},
          "sensitive" => nil,
          "visibility" => visibility
        }
      }) do
    "@#{actor_nickname} updated status ##{subject_id}, set visibility: '#{visibility}'"
  end

  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "action" => "status_update",
          "subject" => %{"id" => subject_id, "type" => "status"},
          "sensitive" => sensitive,
          "visibility" => nil
        }
      }) do
    "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}'"
  end

  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "action" => "status_update",
          "subject" => %{"id" => subject_id, "type" => "status"},
          "sensitive" => sensitive,
          "visibility" => visibility
        }
      }) do
    "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}', visibility: '#{
      visibility
    }'"
  end

  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "action" => "status_delete",
          "subject_id" => subject_id
        }
      }) do
    "@#{actor_nickname} deleted status ##{subject_id}"
  end
572

573 574 575 576 577 578 579 580
  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "action" => "force_password_reset",
          "subject" => subjects
        }
      }) do
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
    "@#{actor_nickname} forced password reset for users: #{users_to_nicknames_string(subjects)}"
  end

  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "action" => "confirm_email",
          "subject" => subjects
        }
      }) do
    "@#{actor_nickname} confirmed email for users: #{users_to_nicknames_string(subjects)}"
  end

  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "action" => "resend_confirmation_email",
          "subject" => subjects
        }
      }) do
    "@#{actor_nickname} re-sent confirmation email for users: #{
      users_to_nicknames_string(subjects)
    }"
606 607
  end

608 609 610 611 612 613 614 615 616 617 618
  @spec get_log_entry_message(ModerationLog) :: String.t()
  def get_log_entry_message(%ModerationLog{
        data: %{
          "actor" => %{"nickname" => actor_nickname},
          "action" => "change_password",
          "subject" => subjects
        }
      }) do
    "@#{actor_nickname} changed password for users: #{users_to_nicknames_string(subjects)}"
  end

619 620 621 622 623 624 625 626 627 628 629
  defp nicknames_to_string(nicknames) do
    nicknames
    |> Enum.map(&"@#{&1}")
    |> Enum.join(", ")
  end

  defp users_to_nicknames_string(users) do
    users
    |> Enum.map(&"@#{&1["nickname"]}")
    |> Enum.join(", ")
  end
Maxim Filippov's avatar
Maxim Filippov committed
630
end