make_moderator.ex 894 Bytes
Newer Older
lain's avatar
lain committed
1
defmodule Mix.Tasks.SetModerator do
Haelwenn's avatar
Haelwenn committed
2
3
4
  @moduledoc """
  Set moderator to a local user

5
  Usage: ``mix set_moderator <nickname>``
Haelwenn's avatar
Haelwenn committed
6
7
8
9

  Example: ``mix set_moderator lain``
  """

lain's avatar
lain committed
10
  use Mix.Task
11
  import Ecto.Changeset
lain's avatar
lain committed
12
13
14
  alias Pleroma.{Repo, User}

  def run([nickname | rest]) do
15
    Application.ensure_all_started(:pleroma)
lain's avatar
lain committed
16

lain's avatar
lain committed
17
18
19
20
21
    moderator =
      case rest do
        [moderator] -> moderator == "true"
        _ -> true
      end
lain's avatar
lain committed
22
23

    with %User{local: true} = user <- User.get_by_nickname(nickname) do
24
      info_cng = User.Info.admin_api_update(user.info, %{is_moderator: !!moderator})
Rin Toshaka's avatar
Sorry    
Rin Toshaka committed
25
26
27
28
29

      user_cng =
        Ecto.Changeset.change(user)
        |> put_embed(:info, info_cng)

30
      {:ok, user} = User.update_and_set_cache(user_cng)
lain's avatar
lain committed
31

32
      IO.puts("Moderator status of #{nickname}: #{user.info.is_moderator}")
lain's avatar
lain committed
33
34
    else
      _ ->
lain's avatar
lain committed
35
        IO.puts("No local user #{nickname}")
lain's avatar
lain committed
36
37
38
    end
  end
end