From 035eaeb9b8702ed233e8bb589a78838efd1f131e Mon Sep 17 00:00:00 2001 From: eugenijm Date: Mon, 4 Feb 2019 15:28:35 +0300 Subject: [PATCH] Allow to configure visibility for admin and moderator badges --- lib/pleroma/user/info.ex | 7 +++- .../web/twitter_api/twitter_api_controller.ex | 4 +- .../web/twitter_api/views/user_view.ex | 22 +++++++++- .../twitter_api_controller_test.exs | 40 ++++++++++++++++--- test/web/twitter_api/views/user_view_test.exs | 24 +++++++---- 5 files changed, 78 insertions(+), 19 deletions(-) diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index 2186190a0..dcc599e7e 100644 --- a/lib/pleroma/user/info.ex +++ b/lib/pleroma/user/info.ex @@ -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 diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 5e3fe9352..3dbc49c87 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -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") diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index 2a0cc5c0f..ad37baf3a 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -105,8 +105,6 @@ defmodule Pleroma.Web.TwitterAPI.UserView do "cover_photo" => User.banner_url(user) |> MediaProxy.url(), "background_image" => image_url(user.info.background) |> MediaProxy.url(), "is_local" => user.local, - "is_moderator" => user.info.is_moderator, - "is_admin" => user.info.is_admin, "locked" => user.info.locked, "default_scope" => user.info.default_scope, "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 Map.put(data, "token", token_string(assigns[:token])) else @@ -128,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 diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 6777354c3..cb98f1809 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -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 diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index 637b84d72..b89da50a4 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -97,8 +97,6 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "cover_photo" => banner, "background_image" => nil, "is_local" => true, - "is_admin" => false, - "is_moderator" => false, "locked" => false, "default_scope" => "public", "no_rich_text" => false, @@ -147,8 +145,6 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "cover_photo" => banner, "background_image" => nil, "is_local" => true, - "is_admin" => false, - "is_moderator" => false, "locked" => false, "default_scope" => "public", "no_rich_text" => false, @@ -198,8 +194,6 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "cover_photo" => banner, "background_image" => nil, "is_local" => true, - "is_admin" => false, - "is_moderator" => false, "locked" => false, "default_scope" => "public", "no_rich_text" => false, @@ -220,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 @@ -227,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 @@ -263,8 +273,6 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "cover_photo" => banner, "background_image" => nil, "is_local" => true, - "is_admin" => false, - "is_moderator" => false, "locked" => false, "default_scope" => "public", "no_rich_text" => false,