Browse Source

return :visible instead of boolean

merge-requests/2720/head
Alexander Strizhakov 4 years ago
parent
commit
1671864d88
No known key found for this signature in database GPG Key ID: 22896A53AEF1381
4 changed files with 21 additions and 12 deletions
  1. +14
    -5
      lib/pleroma/user.ex
  2. +2
    -2
      lib/pleroma/web/mastodon_api/controllers/account_controller.ex
  3. +1
    -1
      lib/pleroma/web/mastodon_api/views/account_view.ex
  4. +4
    -4
      test/user_test.exs

+ 14
- 5
lib/pleroma/user.ex View File

@@ -272,7 +272,7 @@ defmodule Pleroma.User 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 @@ defmodule Pleroma.User do

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 @@ defmodule Pleroma.User 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 @@ defmodule Pleroma.User 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()


+ 2
- 2
lib/pleroma/web/mastodon_api/controllers/account_controller.ex View File

@@ -221,7 +221,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
@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 @@ defmodule Pleroma.Web.MastodonAPI.AccountController 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)


+ 1
- 1
lib/pleroma/web/mastodon_api/views/account_view.ex View File

@@ -35,7 +35,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView 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
%{}


+ 4
- 4
test/user_test.exs View File

@@ -1293,7 +1293,7 @@ defmodule Pleroma.UserTest 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 @@ defmodule Pleroma.UserTest 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 @@ defmodule Pleroma.UserTest do
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



Loading…
Cancel
Save