[#471] Factored out User.visible_for?/2.
authorIvan Tashkinov <ivantashkinov@gmail.com>
Fri, 28 Dec 2018 11:35:25 +0000 (14:35 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Fri, 28 Dec 2018 11:35:25 +0000 (14:35 +0300)
lib/pleroma/user.ex
lib/pleroma/web/mastodon_api/views/account_view.ex
lib/pleroma/web/twitter_api/views/user_view.ex

index b8a7a3fae706716a5b20a74b70003bbd03948786..7d97bf7e52dd59c2b030ec91882daf9be9e30646 100644 (file)
@@ -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
index ba72e3a1098cdb01b75a568dfc008d1f549b006c..32cb1ac60a383c2c7e2718676c504df998f71883 100644 (file)
@@ -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
index 41825f8f66e38ec6b336b6dd0bffcf5d200a58eb..890d4234e4f78eaa9735ad6120a99747c497b777 100644 (file)
@@ -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