[#467] Refactored valid / invalid user rendering.
authorIvan Tashkinov <ivantashkinov@gmail.com>
Fri, 28 Dec 2018 19:47:42 +0000 (22:47 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Fri, 28 Dec 2018 19:47:42 +0000 (22:47 +0300)
lib/pleroma/web/mastodon_api/views/account_view.ex
lib/pleroma/web/twitter_api/views/user_view.ex

index 32cb1ac60a383c2c7e2718676c504df998f71883..555383503605bcbe2eb29308a46db6fe0870bd00 100644 (file)
@@ -18,15 +18,48 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
 
   def render("account.json", %{user: user} = opts) do
     if User.visible_for?(user, opts[:for]),
-      do: render("valid_account.json", opts),
-      else: render("invalid_account.json", opts)
+      do: do_render("account.json", opts),
+      else: %{}
   end
 
-  def render("invalid_account.json", _opts) do
-    %{}
+  def render("mention.json", %{user: user}) do
+    %{
+      id: to_string(user.id),
+      acct: user.nickname,
+      username: username_from_nickname(user.nickname),
+      url: user.ap_id
+    }
+  end
+
+  def render("relationship.json", %{user: user, target: target}) do
+    follow_activity = Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(user, target)
+
+    requested =
+      if follow_activity do
+        follow_activity.data["state"] == "pending"
+      else
+        false
+      end
+
+    %{
+      id: to_string(target.id),
+      following: User.following?(user, target),
+      followed_by: User.following?(target, user),
+      blocking: User.blocks?(user, target),
+      muting: false,
+      muting_notifications: false,
+      requested: requested,
+      domain_blocking: false,
+      showing_reblogs: false,
+      endorsed: false
+    }
   end
 
-  def render("valid_account.json", %{user: user} = opts) do
+  def render("relationships.json", %{user: user, targets: targets}) do
+    render_many(targets, AccountView, "relationship.json", user: user, as: :target)
+  end
+
+  defp do_render("account.json", %{user: user} = opts) do
     image = User.avatar_url(user) |> MediaProxy.url()
     header = User.banner_url(user) |> MediaProxy.url()
     user_info = User.user_info(user)
@@ -84,43 +117,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
     }
   end
 
-  def render("mention.json", %{user: user}) do
-    %{
-      id: to_string(user.id),
-      acct: user.nickname,
-      username: username_from_nickname(user.nickname),
-      url: user.ap_id
-    }
-  end
-
-  def render("relationship.json", %{user: user, target: target}) do
-    follow_activity = Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(user, target)
-
-    requested =
-      if follow_activity do
-        follow_activity.data["state"] == "pending"
-      else
-        false
-      end
-
-    %{
-      id: to_string(target.id),
-      following: User.following?(user, target),
-      followed_by: User.following?(target, user),
-      blocking: User.blocks?(user, target),
-      muting: false,
-      muting_notifications: false,
-      requested: requested,
-      domain_blocking: false,
-      showing_reblogs: false,
-      endorsed: false
-    }
-  end
-
-  def render("relationships.json", %{user: user, targets: targets}) do
-    render_many(targets, AccountView, "relationship.json", user: user, as: :target)
-  end
-
   defp username_from_nickname(string) when is_binary(string) do
     hd(String.split(string, "@"))
   end
index 890d4234e4f78eaa9735ad6120a99747c497b777..ede3c0d2590f1dd2f36ef052abf27de767284a60 100644 (file)
@@ -22,15 +22,28 @@ defmodule Pleroma.Web.TwitterAPI.UserView do
 
   def render("user.json", %{user: user = %User{}} = assigns) do
     if User.visible_for?(user, assigns[:for]),
-      do: render("valid_user.json", assigns),
-      else: render("invalid_user.json", assigns)
+      do: do_render("user.json", assigns),
+      else: %{}
   end
 
-  def render("invalid_user.json", _assigns) do
-    %{}
+  def render("short.json", %{
+        user: %User{
+          nickname: nickname,
+          id: id,
+          ap_id: ap_id,
+          name: name
+        }
+      }) do
+    %{
+      "fullname" => name,
+      "id" => id,
+      "ostatus_uri" => ap_id,
+      "profile_url" => ap_id,
+      "screen_name" => nickname
+    }
   end
 
-  def render("valid_user.json", %{user: user = %User{}} = assigns) do
+  defp do_render("user.json", %{user: user = %User{}} = assigns) do
     for_user = assigns[:for]
     image = User.avatar_url(user) |> MediaProxy.url()
 
@@ -110,23 +123,6 @@ defmodule Pleroma.Web.TwitterAPI.UserView do
     end
   end
 
-  def render("short.json", %{
-        user: %User{
-          nickname: nickname,
-          id: id,
-          ap_id: ap_id,
-          name: name
-        }
-      }) do
-    %{
-      "fullname" => name,
-      "id" => id,
-      "ostatus_uri" => ap_id,
-      "profile_url" => ap_id,
-      "screen_name" => nickname
-    }
-  end
-
   defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
   defp image_url(_), do: nil