From 0d1788ce44cf7bf50aa40e56988be0d2e315d2e1 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Fri, 28 Dec 2018 14:35:25 +0300 Subject: [PATCH] [#471] Factored out User.visible_for?/2. --- lib/pleroma/user.ex | 6 ++++++ lib/pleroma/web/mastodon_api/views/account_view.ex | 14 +++----------- lib/pleroma/web/twitter_api/views/user_view.ex | 14 +++----------- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index b8a7a3fae..7d97bf7e5 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -49,6 +49,12 @@ defmodule Pleroma.User do def remote_or_auth_active?(%User{} = user), do: !user.local || auth_active?(user) + def visible_for?(%User{} = user, for_user \\ nil) do + User.remote_or_auth_active?(user) || (for_user && for_user.id == user.id) || + User.superuser?(for_user) + end + + def superuser?(nil), do: false def superuser?(%User{} = user), do: user.info && User.Info.superuser?(user.info) def avatar_url(user) do diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index ba72e3a10..32cb1ac60 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -17,17 +17,9 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do end def render("account.json", %{user: user} = opts) do - for_user = opts[:for] - - allow_render = - User.remote_or_auth_active?(user) || - (for_user && (for_user.id == user.id || User.superuser?(for_user))) - - if allow_render do - render("valid_account.json", opts) - else - render("invalid_account.json", opts) - end + if User.visible_for?(user, opts[:for]), + do: render("valid_account.json", opts), + else: render("invalid_account.json", opts) end def render("invalid_account.json", _opts) do diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index 41825f8f6..890d4234e 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -21,17 +21,9 @@ defmodule Pleroma.Web.TwitterAPI.UserView do end def render("user.json", %{user: user = %User{}} = assigns) do - for_user = assigns[:for] - - allow_render = - User.remote_or_auth_active?(user) || - (for_user && (for_user.id == user.id || User.superuser?(for_user))) - - if allow_render do - render("valid_user.json", assigns) - else - render("invalid_user.json", assigns) - end + if User.visible_for?(user, assigns[:for]), + do: render("valid_user.json", assigns), + else: render("invalid_user.json", assigns) end def render("invalid_user.json", _assigns) do -- 2.45.2