Merge branch 'add-admin-and-moderator-fields' into 'develop'

Add admin and moderator badges to user view and make their visibility configurable

See merge request pleroma/pleroma!767
This commit is contained in:
kaniini 2019-02-06 18:19:47 +00:00
commit 18e783bcb2
5 changed files with 78 additions and 9 deletions

View File

@ -23,6 +23,7 @@ defmodule Pleroma.User.Info do
field(:ap_enabled, :boolean, default: false) field(:ap_enabled, :boolean, default: false)
field(:is_moderator, :boolean, default: false) field(:is_moderator, :boolean, default: false)
field(:is_admin, :boolean, default: false) field(:is_admin, :boolean, default: false)
field(:show_role, :boolean, default: true)
field(:keys, :string, default: nil) field(:keys, :string, default: nil)
field(:settings, :map, default: nil) field(:settings, :map, default: nil)
field(:magic_key, :string, default: nil) field(:magic_key, :string, default: nil)
@ -146,7 +147,8 @@ def profile_update(info, params) do
:banner, :banner,
:hide_followings, :hide_followings,
:hide_followers, :hide_followers,
:background :background,
:show_role
]) ])
end end
@ -196,7 +198,8 @@ def admin_api_update(info, params) do
info info
|> cast(params, [ |> cast(params, [
:is_moderator, :is_moderator,
:is_admin :is_admin,
:show_role
]) ])
end end

View File

@ -24,7 +24,7 @@ def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
conn conn
|> put_view(UserView) |> put_view(UserView)
|> render("show.json", %{user: user, token: token}) |> render("show.json", %{user: user, token: token, for: user})
end end
def status_update(%{assigns: %{user: user}} = conn, %{"status" => _} = status_data) do def status_update(%{assigns: %{user: user}} = conn, %{"status" => _} = status_data) do
@ -618,7 +618,7 @@ def raw_empty_array(conn, _params) do
defp build_info_cng(user, params) do defp build_info_cng(user, params) do
info_params = info_params =
["no_rich_text", "locked", "hide_followers", "hide_followings"] ["no_rich_text", "locked", "hide_followers", "hide_followings", "show_role"]
|> Enum.reduce(%{}, fn key, res -> |> Enum.reduce(%{}, fn key, res ->
if value = params[key] do if value = params[key] do
Map.put(res, key, value == "true") Map.put(res, key, value == "true")

View File

@ -119,6 +119,12 @@ defp do_render("user.json", %{user: user = %User{}} = assigns) do
} }
} }
data =
if(user.info.is_admin || user.info.is_moderator,
do: maybe_with_role(data, user, for_user),
else: data
)
if assigns[:token] do if assigns[:token] do
Map.put(data, "token", token_string(assigns[:token])) Map.put(data, "token", token_string(assigns[:token]))
else else
@ -126,6 +132,20 @@ defp do_render("user.json", %{user: user = %User{}} = assigns) do
end end
end end
defp maybe_with_role(data, %User{id: id} = user, %User{id: id}) do
Map.merge(data, %{"role" => role(user), "show_role" => user.info.show_role})
end
defp maybe_with_role(data, %User{info: %{show_role: true}} = user, _user) do
Map.merge(data, %{"role" => role(user)})
end
defp maybe_with_role(data, _, _), do: data
defp role(%User{info: %{:is_admin => true}}), do: "admin"
defp role(%User{info: %{:is_moderator => true}}), do: "moderator"
defp role(_), do: "member"
defp image_url(%{"url" => [%{"href" => href} | _]}), do: href defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
defp image_url(_), do: nil defp image_url(_), do: nil

View File

@ -62,7 +62,8 @@ test "with credentials", %{conn: conn, user: user} do
|> post("/api/account/verify_credentials.json") |> post("/api/account/verify_credentials.json")
|> json_response(200) |> json_response(200)
assert response == UserView.render("show.json", %{user: user, token: response["token"]}) assert response ==
UserView.render("show.json", %{user: user, token: response["token"], for: user})
end end
end end
@ -107,7 +108,7 @@ test "with credentials", %{conn: conn, user: user} do
|> post(request_path, %{status: "Nice meme.", visibility: "private"}) |> post(request_path, %{status: "Nice meme.", visibility: "private"})
assert json_response(conn, 200) == assert json_response(conn, 200) ==
ActivityRepresenter.to_map(Repo.one(Activity), %{user: user}) ActivityRepresenter.to_map(Repo.one(Activity), %{user: user, for: user})
end end
end end
@ -418,6 +419,7 @@ test "with credentials", %{conn: conn, user: current_user} do
assert Enum.at(response, 0) == assert Enum.at(response, 0) ==
ActivityRepresenter.to_map(activity, %{ ActivityRepresenter.to_map(activity, %{
user: current_user, user: current_user,
for: current_user,
mentioned: [current_user] mentioned: [current_user]
}) })
end end
@ -547,7 +549,9 @@ test "with credentials", %{conn: conn, user: current_user} do
response = json_response(conn, 200) response = json_response(conn, 200)
assert length(response) == 1 assert length(response) == 1
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user})
assert Enum.at(response, 0) ==
ActivityRepresenter.to_map(activity, %{user: current_user, for: current_user})
end end
test "with credentials with user_id", %{conn: conn, user: current_user} do test "with credentials with user_id", %{conn: conn, user: current_user} do
@ -1414,6 +1418,30 @@ test "it sets and un-sets hide_followers", %{conn: conn} do
assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user}) assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
end end
test "it sets and un-sets show_role", %{conn: conn} do
user = insert(:user)
conn
|> assign(:user, user)
|> post("/api/account/update_profile.json", %{
"show_role" => "true"
})
user = Repo.get!(User, user.id)
assert user.info.show_role == true
conn =
conn
|> assign(:user, user)
|> post("/api/account/update_profile.json", %{
"show_role" => "false"
})
user = Repo.get!(User, user.id)
assert user.info.show_role == false
assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
end
test "it locks an account", %{conn: conn} do test "it locks an account", %{conn: conn} do
user = insert(:user) user = insert(:user)
@ -1814,7 +1842,8 @@ test "with credentials", %{conn: conn, user: user} do
user = refresh_record(user) user = refresh_record(user)
assert json_response(response, 200) == ActivityRepresenter.to_map(activity, %{user: user}) assert json_response(response, 200) ==
ActivityRepresenter.to_map(activity, %{user: user, for: user})
end end
end end
@ -1843,7 +1872,8 @@ test "with credentials", %{conn: conn, user: user} do
user = refresh_record(user) user = refresh_record(user)
assert json_response(response, 200) == ActivityRepresenter.to_map(activity, %{user: user}) assert json_response(response, 200) ==
ActivityRepresenter.to_map(activity, %{user: user, for: user})
end end
end end
end end

View File

@ -214,6 +214,7 @@ test "a user that is a moderator" do
represented = UserView.render("show.json", %{user: user, for: user}) represented = UserView.render("show.json", %{user: user, for: user})
assert represented["rights"]["delete_others_notice"] assert represented["rights"]["delete_others_notice"]
assert represented["role"] == "moderator"
end end
test "a user that is a admin" do test "a user that is a admin" do
@ -221,6 +222,21 @@ test "a user that is a admin" do
represented = UserView.render("show.json", %{user: user, for: user}) represented = UserView.render("show.json", %{user: user, for: user})
assert represented["rights"]["admin"] assert represented["rights"]["admin"]
assert represented["role"] == "admin"
end
test "A moderator with hidden role for another user", %{user: user} do
admin = insert(:user, %{info: %{is_moderator: true, show_role: false}})
represented = UserView.render("show.json", %{user: admin, for: user})
assert represented["role"] == nil
end
test "An admin with hidden role for another user", %{user: user} do
admin = insert(:user, %{info: %{is_admin: true, show_role: false}})
represented = UserView.render("show.json", %{user: admin, for: user})
assert represented["role"] == nil
end end
test "A blocked user for the blocker" do test "A blocked user for the blocker" do