Commit 18e783bc authored by kaniini's avatar kaniini

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 !767
parents 09b71a90 035eaeb9
Pipeline #7177 failed with stages
in 1 minute and 29 seconds
......@@ -23,6 +23,7 @@ defmodule Pleroma.User.Info do
field(:ap_enabled, :boolean, default: false)
field(:is_moderator, :boolean, default: false)
field(:is_admin, :boolean, default: false)
field(:show_role, :boolean, default: true)
field(:keys, :string, default: nil)
field(:settings, :map, default: nil)
field(:magic_key, :string, default: nil)
......@@ -146,7 +147,8 @@ defmodule Pleroma.User.Info do
:banner,
:hide_followings,
:hide_followers,
:background
:background,
:show_role
])
end
......@@ -196,7 +198,8 @@ defmodule Pleroma.User.Info do
info
|> cast(params, [
:is_moderator,
:is_admin
:is_admin,
:show_role
])
end
......
......@@ -24,7 +24,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
conn
|> put_view(UserView)
|> render("show.json", %{user: user, token: token})
|> render("show.json", %{user: user, token: token, for: user})
end
def status_update(%{assigns: %{user: user}} = conn, %{"status" => _} = status_data) do
......@@ -618,7 +618,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
defp build_info_cng(user, params) do
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 ->
if value = params[key] do
Map.put(res, key, value == "true")
......
......@@ -119,6 +119,12 @@ defmodule Pleroma.Web.TwitterAPI.UserView 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
Map.put(data, "token", token_string(assigns[:token]))
else
......@@ -126,6 +132,20 @@ defmodule Pleroma.Web.TwitterAPI.UserView do
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(_), do: nil
......
......@@ -62,7 +62,8 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|> post("/api/account/verify_credentials.json")
|> 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
......@@ -107,7 +108,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|> post(request_path, %{status: "Nice meme.", visibility: "private"})
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
......@@ -418,6 +419,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
assert Enum.at(response, 0) ==
ActivityRepresenter.to_map(activity, %{
user: current_user,
for: current_user,
mentioned: [current_user]
})
end
......@@ -547,7 +549,9 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
response = json_response(conn, 200)
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
test "with credentials with user_id", %{conn: conn, user: current_user} do
......@@ -1414,6 +1418,30 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
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
user = insert(:user)
......@@ -1814,7 +1842,8 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
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
......@@ -1843,7 +1872,8 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
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
......@@ -214,6 +214,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
represented = UserView.render("show.json", %{user: user, for: user})
assert represented["rights"]["delete_others_notice"]
assert represented["role"] == "moderator"
end
test "a user that is a admin" do
......@@ -221,6 +222,21 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
represented = UserView.render("show.json", %{user: user, for: user})
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
test "A blocked user for the blocker" do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment