[#467] Refactored valid / invalid user rendering.

This commit is contained in:
Ivan Tashkinov 2018-12-28 22:47:42 +03:00
parent 0d1788ce44
commit 0a41786624
2 changed files with 56 additions and 64 deletions

View file

@ -18,15 +18,48 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
def render("account.json", %{user: user} = opts) do def render("account.json", %{user: user} = opts) do
if User.visible_for?(user, opts[:for]), if User.visible_for?(user, opts[:for]),
do: render("valid_account.json", opts), do: do_render("account.json", opts),
else: render("invalid_account.json", opts) else: %{}
end 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 end
def render("valid_account.json", %{user: user} = opts) do 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 do_render("account.json", %{user: user} = opts) do
image = User.avatar_url(user) |> MediaProxy.url() image = User.avatar_url(user) |> MediaProxy.url()
header = User.banner_url(user) |> MediaProxy.url() header = User.banner_url(user) |> MediaProxy.url()
user_info = User.user_info(user) user_info = User.user_info(user)
@ -84,43 +117,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
} }
end 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 defp username_from_nickname(string) when is_binary(string) do
hd(String.split(string, "@")) hd(String.split(string, "@"))
end end

View file

@ -22,15 +22,28 @@ defmodule Pleroma.Web.TwitterAPI.UserView do
def render("user.json", %{user: user = %User{}} = assigns) do def render("user.json", %{user: user = %User{}} = assigns) do
if User.visible_for?(user, assigns[:for]), if User.visible_for?(user, assigns[:for]),
do: render("valid_user.json", assigns), do: do_render("user.json", assigns),
else: render("invalid_user.json", assigns) else: %{}
end 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 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] for_user = assigns[:for]
image = User.avatar_url(user) |> MediaProxy.url() image = User.avatar_url(user) |> MediaProxy.url()
@ -110,23 +123,6 @@ defmodule Pleroma.Web.TwitterAPI.UserView do
end end
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(%{"url" => [%{"href" => href} | _]}), do: href
defp image_url(_), do: nil defp image_url(_), do: nil