return :visible instead of boolean

This commit is contained in:
Alexander Strizhakov 2020-05-15 20:29:09 +03:00
parent b1aa402229
commit 1671864d88
No known key found for this signature in database
GPG Key ID: 022896A53AEF1381
4 changed files with 21 additions and 12 deletions

View File

@ -272,7 +272,7 @@ def account_status(%User{confirmation_pending: true}) do
def account_status(%User{}), do: :active
@spec visible_for(User.t(), User.t() | nil) ::
boolean()
:visible
| :invisible
| :restricted_unauthenticated
| :deactivated
@ -281,7 +281,7 @@ def visible_for(user, for_user \\ nil)
def visible_for(%User{invisible: true}, _), do: :invisible
def visible_for(%User{id: user_id}, %User{id: user_id}), do: true
def visible_for(%User{id: user_id}, %User{id: user_id}), do: :visible
def visible_for(%User{} = user, nil) do
if restrict_unauthenticated?(user) do
@ -292,10 +292,14 @@ def visible_for(%User{} = user, nil) do
end
def visible_for(%User{} = user, for_user) do
superuser?(for_user) || visible_account_status(user)
if superuser?(for_user) do
:visible
else
visible_account_status(user)
end
end
def visible_for(_, _), do: false
def visible_for(_, _), do: :invisible
defp restrict_unauthenticated?(%User{local: local}) do
config_key = if local, do: :local, else: :remote
@ -305,7 +309,12 @@ defp restrict_unauthenticated?(%User{local: local}) do
defp visible_account_status(user) do
status = account_status(user)
status in [:active, :password_reset_pending] || status
if status in [:active, :password_reset_pending] do
:visible
else
status
end
end
@spec superuser?(User.t()) :: boolean()

View File

@ -221,7 +221,7 @@ def relationships(%{assigns: %{user: _user}} = conn, _), do: json(conn, [])
@doc "GET /api/v1/accounts/:id"
def show(%{assigns: %{user: for_user}} = conn, %{id: nickname_or_id}) do
with %User{} = user <- User.get_cached_by_nickname_or_id(nickname_or_id, for: for_user),
true <- User.visible_for(user, for_user) do
:visible <- User.visible_for(user, for_user) do
render(conn, "show.json", user: user, for: for_user)
else
error -> user_visibility_error(conn, error)
@ -231,7 +231,7 @@ def show(%{assigns: %{user: for_user}} = conn, %{id: nickname_or_id}) do
@doc "GET /api/v1/accounts/:id/statuses"
def statuses(%{assigns: %{user: reading_user}} = conn, params) do
with %User{} = user <- User.get_cached_by_nickname_or_id(params.id, for: reading_user),
true <- User.visible_for(user, reading_user) do
:visible <- User.visible_for(user, reading_user) do
params =
params
|> Map.delete(:tagged)

View File

@ -35,7 +35,7 @@ def render("index.json", %{users: users} = opts) do
end
def render("show.json", %{user: user} = opts) do
if User.visible_for(user, opts[:for]) == true do
if User.visible_for(user, opts[:for]) == :visible do
do_render("show.json", opts)
else
%{}

View File

@ -1293,7 +1293,7 @@ test "returns false for a non-invisible user" do
test "returns true when the account is itself" do
user = insert(:user, local: true)
assert User.visible_for(user, user)
assert User.visible_for(user, user) == :visible
end
test "returns false when the account is unauthenticated and auth is required" do
@ -1302,14 +1302,14 @@ test "returns false when the account is unauthenticated and auth is required" do
user = insert(:user, local: true, confirmation_pending: true)
other_user = insert(:user, local: true)
refute User.visible_for(user, other_user) == true
refute User.visible_for(user, other_user) == :visible
end
test "returns true when the account is unauthenticated and auth is not required" do
user = insert(:user, local: true, confirmation_pending: true)
other_user = insert(:user, local: true)
assert User.visible_for(user, other_user)
assert User.visible_for(user, other_user) == :visible
end
test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
@ -1318,7 +1318,7 @@ test "returns true when the account is unauthenticated and being viewed by a pri
user = insert(:user, local: true, confirmation_pending: true)
other_user = insert(:user, local: true, is_admin: true)
assert User.visible_for(user, other_user)
assert User.visible_for(user, other_user) == :visible
end
end