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)
:banner,
:hide_followings,
:hide_followers,
- :background
+ :background,
+ :show_role
])
end
info
|> cast(params, [
:is_moderator,
- :is_admin
+ :is_admin,
+ :show_role
])
end
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
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")
"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,
}
}
+ 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
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
|> 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
|> 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
assert Enum.at(response, 0) ==
ActivityRepresenter.to_map(activity, %{
user: current_user,
+ for: current_user,
mentioned: [current_user]
})
end
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
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)
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
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
"cover_photo" => banner,
"background_image" => nil,
"is_local" => true,
- "is_admin" => false,
- "is_moderator" => false,
"locked" => false,
"default_scope" => "public",
"no_rich_text" => false,
"cover_photo" => banner,
"background_image" => nil,
"is_local" => true,
- "is_admin" => false,
- "is_moderator" => false,
"locked" => false,
"default_scope" => "public",
"no_rich_text" => false,
"cover_photo" => banner,
"background_image" => nil,
"is_local" => true,
- "is_admin" => false,
- "is_moderator" => false,
"locked" => false,
"default_scope" => "public",
"no_rich_text" => false,
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
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
"cover_photo" => banner,
"background_image" => nil,
"is_local" => true,
- "is_admin" => false,
- "is_moderator" => false,
"locked" => false,
"default_scope" => "public",
"no_rich_text" => false,