2017-06-19 21:12:37 +00:00
|
|
|
defmodule Pleroma.Web.TwitterAPI.UserView do
|
|
|
|
use Pleroma.Web, :view
|
2017-03-23 14:51:34 +00:00
|
|
|
alias Pleroma.User
|
2018-08-08 05:38:25 +00:00
|
|
|
alias Pleroma.Formatter
|
2017-09-15 12:17:36 +00:00
|
|
|
alias Pleroma.Web.CommonAPI.Utils
|
2017-11-22 18:06:07 +00:00
|
|
|
alias Pleroma.Web.MediaProxy
|
2017-03-23 14:51:34 +00:00
|
|
|
|
2017-06-19 21:12:37 +00:00
|
|
|
def render("show.json", %{user: user = %User{}} = assigns) do
|
2017-07-20 18:29:15 +00:00
|
|
|
render_one(user, Pleroma.Web.TwitterAPI.UserView, "user.json", assigns)
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("index.json", %{users: users, for: user}) do
|
|
|
|
render_many(users, Pleroma.Web.TwitterAPI.UserView, "user.json", for: user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("user.json", %{user: user = %User{}} = assigns) do
|
2017-11-22 18:06:07 +00:00
|
|
|
image = User.avatar_url(user) |> MediaProxy.url()
|
2018-03-30 13:01:53 +00:00
|
|
|
|
|
|
|
{following, follows_you, statusnet_blocking} =
|
|
|
|
if assigns[:for] do
|
|
|
|
{
|
|
|
|
User.following?(assigns[:for], user),
|
|
|
|
User.following?(user, assigns[:for]),
|
|
|
|
User.blocks?(assigns[:for], user)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{false, false, false}
|
|
|
|
end
|
2017-03-23 14:51:34 +00:00
|
|
|
|
2017-04-30 08:04:54 +00:00
|
|
|
user_info = User.get_cached_user_info(user)
|
2017-04-20 22:51:09 +00:00
|
|
|
|
2018-08-08 05:38:25 +00:00
|
|
|
emoji =
|
|
|
|
(user.info["source_data"]["tag"] || [])
|
|
|
|
|> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
|
|
|
|
|> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
|
|
|
|
{String.trim(name, ":"), url}
|
|
|
|
end)
|
|
|
|
|
2017-12-04 18:10:15 +00:00
|
|
|
data = %{
|
2018-03-30 13:01:53 +00:00
|
|
|
"created_at" => user.inserted_at |> Utils.format_naive_asctime(),
|
2018-08-21 23:47:25 +00:00
|
|
|
"description" => HtmlSanitizeEx.strip_tags((user.bio || "") |> String.replace("<br>", "\n")),
|
2018-08-21 13:03:36 +00:00
|
|
|
"description_html" => HtmlSanitizeEx.basic_html(user.bio),
|
2017-03-20 20:30:18 +00:00
|
|
|
"favourites_count" => 0,
|
2017-04-20 22:51:09 +00:00
|
|
|
"followers_count" => user_info[:follower_count],
|
2017-06-19 21:12:37 +00:00
|
|
|
"following" => following,
|
2017-11-08 17:13:03 +00:00
|
|
|
"follows_you" => follows_you,
|
2017-11-08 11:02:00 +00:00
|
|
|
"statusnet_blocking" => statusnet_blocking,
|
2017-06-19 21:12:37 +00:00
|
|
|
"friends_count" => user_info[:following_count],
|
|
|
|
"id" => user.id,
|
|
|
|
"name" => user.name,
|
2018-08-09 10:07:03 +00:00
|
|
|
"name_html" => HtmlSanitizeEx.strip_tags(user.name) |> Formatter.emojify(emoji),
|
2017-03-20 20:30:18 +00:00
|
|
|
"profile_image_url" => image,
|
|
|
|
"profile_image_url_https" => image,
|
|
|
|
"profile_image_url_profile_size" => image,
|
|
|
|
"profile_image_url_original" => image,
|
2018-02-20 17:44:50 +00:00
|
|
|
"rights" => %{
|
|
|
|
"delete_others_notice" => !!user.info["is_moderator"]
|
|
|
|
},
|
2017-06-19 21:12:37 +00:00
|
|
|
"screen_name" => user.nickname,
|
|
|
|
"statuses_count" => user_info[:note_count],
|
2017-08-29 15:18:33 +00:00
|
|
|
"statusnet_profile_url" => user.ap_id,
|
2018-01-15 20:18:17 +00:00
|
|
|
"cover_photo" => User.banner_url(user) |> MediaProxy.url(),
|
2017-11-22 18:06:07 +00:00
|
|
|
"background_image" => image_url(user.info["background"]) |> MediaProxy.url(),
|
2018-05-29 14:13:34 +00:00
|
|
|
"is_local" => user.local,
|
2018-06-23 06:55:32 +00:00
|
|
|
"locked" => !!user.info["locked"],
|
|
|
|
"default_scope" => user.info["default_scope"] || "public"
|
2017-03-20 20:30:18 +00:00
|
|
|
}
|
2017-12-04 18:10:15 +00:00
|
|
|
|
|
|
|
if assigns[:token] do
|
|
|
|
Map.put(data, "token", assigns[:token])
|
|
|
|
else
|
|
|
|
data
|
|
|
|
end
|
2017-06-19 21:12:37 +00:00
|
|
|
end
|
2017-03-20 20:30:18 +00:00
|
|
|
|
2018-03-30 13:01:53 +00:00
|
|
|
def render("short.json", %{
|
|
|
|
user: %User{
|
|
|
|
nickname: nickname,
|
|
|
|
id: id,
|
|
|
|
ap_id: ap_id,
|
|
|
|
name: name
|
|
|
|
}
|
|
|
|
}) do
|
2017-06-19 21:12:37 +00:00
|
|
|
%{
|
|
|
|
"fullname" => name,
|
|
|
|
"id" => id,
|
|
|
|
"ostatus_uri" => ap_id,
|
|
|
|
"profile_url" => ap_id,
|
|
|
|
"screen_name" => nickname
|
|
|
|
}
|
2017-03-20 20:30:18 +00:00
|
|
|
end
|
2017-11-19 01:22:07 +00:00
|
|
|
|
2018-03-30 13:01:53 +00:00
|
|
|
defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
|
2017-11-19 01:22:07 +00:00
|
|
|
defp image_url(_), do: nil
|
2017-03-20 20:30:18 +00:00
|
|
|
end
|