Allow to configure visibility for admin and moderator badges

This commit is contained in:
eugenijm 2019-02-04 15:28:35 +03:00
parent 398c81f9c8
commit 035eaeb9b8
5 changed files with 78 additions and 19 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 @@ defmodule Pleroma.User.Info do
:banner, :banner,
:hide_followings, :hide_followings,
:hide_followers, :hide_followers,
:background :background,
:show_role
]) ])
end end
@ -196,7 +198,8 @@ defmodule Pleroma.User.Info 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 @@ defmodule Pleroma.Web.TwitterAPI.Controller 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 @@ defmodule Pleroma.Web.TwitterAPI.Controller 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

@ -105,8 +105,6 @@ defmodule Pleroma.Web.TwitterAPI.UserView do
"cover_photo" => User.banner_url(user) |> MediaProxy.url(), "cover_photo" => User.banner_url(user) |> MediaProxy.url(),
"background_image" => image_url(user.info.background) |> MediaProxy.url(), "background_image" => image_url(user.info.background) |> MediaProxy.url(),
"is_local" => user.local, "is_local" => user.local,
"is_moderator" => user.info.is_moderator,
"is_admin" => user.info.is_admin,
"locked" => user.info.locked, "locked" => user.info.locked,
"default_scope" => user.info.default_scope, "default_scope" => user.info.default_scope,
"no_rich_text" => user.info.no_rich_text, "no_rich_text" => user.info.no_rich_text,
@ -121,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 if assigns[:token] do
Map.put(data, "token", token_string(assigns[:token])) Map.put(data, "token", token_string(assigns[:token]))
else else
@ -128,6 +132,20 @@ defmodule Pleroma.Web.TwitterAPI.UserView 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 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest 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 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest 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 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest 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 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest 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 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest 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 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest 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 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest 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

@ -97,8 +97,6 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
"cover_photo" => banner, "cover_photo" => banner,
"background_image" => nil, "background_image" => nil,
"is_local" => true, "is_local" => true,
"is_admin" => false,
"is_moderator" => false,
"locked" => false, "locked" => false,
"default_scope" => "public", "default_scope" => "public",
"no_rich_text" => false, "no_rich_text" => false,
@ -147,8 +145,6 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
"cover_photo" => banner, "cover_photo" => banner,
"background_image" => nil, "background_image" => nil,
"is_local" => true, "is_local" => true,
"is_admin" => false,
"is_moderator" => false,
"locked" => false, "locked" => false,
"default_scope" => "public", "default_scope" => "public",
"no_rich_text" => false, "no_rich_text" => false,
@ -198,8 +194,6 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
"cover_photo" => banner, "cover_photo" => banner,
"background_image" => nil, "background_image" => nil,
"is_local" => true, "is_local" => true,
"is_admin" => false,
"is_moderator" => false,
"locked" => false, "locked" => false,
"default_scope" => "public", "default_scope" => "public",
"no_rich_text" => false, "no_rich_text" => false,
@ -220,6 +214,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest 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
@ -227,6 +222,21 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest 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
@ -263,8 +273,6 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
"cover_photo" => banner, "cover_photo" => banner,
"background_image" => nil, "background_image" => nil,
"is_local" => true, "is_local" => true,
"is_admin" => false,
"is_moderator" => false,
"locked" => false, "locked" => false,
"default_scope" => "public", "default_scope" => "public",
"no_rich_text" => false, "no_rich_text" => false,