2018-12-23 21:04:54 +01:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-06-19 23:12:37 +02:00
|
|
|
defmodule Pleroma.Web.TwitterAPI.UserView do
|
|
|
|
use Pleroma.Web, :view
|
2017-03-23 15:51:34 +01:00
|
|
|
alias Pleroma.User
|
2018-08-08 07:38:25 +02:00
|
|
|
alias Pleroma.Formatter
|
2017-09-15 14:17:36 +02:00
|
|
|
alias Pleroma.Web.CommonAPI.Utils
|
2017-11-22 19:06:07 +01:00
|
|
|
alias Pleroma.Web.MediaProxy
|
2018-09-10 01:40:24 +02:00
|
|
|
alias Pleroma.HTML
|
2017-03-23 15:51:34 +01:00
|
|
|
|
2017-06-19 23:12:37 +02:00
|
|
|
def render("show.json", %{user: user = %User{}} = assigns) do
|
2017-07-20 20:29:15 +02: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 19:06:07 +01:00
|
|
|
image = User.avatar_url(user) |> MediaProxy.url()
|
2018-03-30 15:01:53 +02: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 15:51:34 +01:00
|
|
|
|
2017-04-30 10:04:54 +02:00
|
|
|
user_info = User.get_cached_user_info(user)
|
2017-04-21 00:51:09 +02:00
|
|
|
|
2018-08-08 07:38:25 +02:00
|
|
|
emoji =
|
2018-11-18 22:36:47 +01:00
|
|
|
(user.info.source_data["tag"] || [])
|
2018-08-08 07:38:25 +02:00
|
|
|
|> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
|
|
|
|
|> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
|
|
|
|
{String.trim(name, ":"), url}
|
|
|
|
end)
|
|
|
|
|
2018-09-27 20:17:44 +02:00
|
|
|
# ``fields`` is an array of mastodon profile field, containing ``{"name": "…", "value": "…"}``.
|
|
|
|
# For example: [{"name": "Pronoun", "value": "she/her"}, …]
|
|
|
|
fields =
|
2018-11-18 22:36:47 +01:00
|
|
|
(user.info.source_data["attachment"] || [])
|
2018-09-27 20:17:44 +02:00
|
|
|
|> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
|
|
|
|
|> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
|
|
|
|
|
2017-12-04 19:10:15 +01:00
|
|
|
data = %{
|
2018-03-30 15:01:53 +02:00
|
|
|
"created_at" => user.inserted_at |> Utils.format_naive_asctime(),
|
2018-09-10 02:28:40 +02:00
|
|
|
"description" => HTML.strip_tags((user.bio || "") |> String.replace("<br>", "\n")),
|
2018-09-22 04:13:54 +02:00
|
|
|
"description_html" => HTML.filter_tags(user.bio, User.html_filter_policy(assigns[:for])),
|
2017-03-20 21:30:18 +01:00
|
|
|
"favourites_count" => 0,
|
2017-04-21 00:51:09 +02:00
|
|
|
"followers_count" => user_info[:follower_count],
|
2017-06-19 23:12:37 +02:00
|
|
|
"following" => following,
|
2017-11-08 18:13:03 +01:00
|
|
|
"follows_you" => follows_you,
|
2017-11-08 12:02:00 +01:00
|
|
|
"statusnet_blocking" => statusnet_blocking,
|
2017-06-19 23:12:37 +02:00
|
|
|
"friends_count" => user_info[:following_count],
|
|
|
|
"id" => user.id,
|
2018-11-12 16:38:39 +01:00
|
|
|
"name" => user.name || user.nickname,
|
|
|
|
"name_html" =>
|
|
|
|
if(user.name,
|
|
|
|
do: HTML.strip_tags(user.name) |> Formatter.emojify(emoji),
|
|
|
|
else: user.nickname
|
|
|
|
),
|
2017-03-20 21:30:18 +01:00
|
|
|
"profile_image_url" => image,
|
|
|
|
"profile_image_url_https" => image,
|
|
|
|
"profile_image_url_profile_size" => image,
|
|
|
|
"profile_image_url_original" => image,
|
2018-02-20 18:44:50 +01:00
|
|
|
"rights" => %{
|
2018-11-18 22:36:47 +01:00
|
|
|
"delete_others_notice" => !!user.info.is_moderator
|
2018-02-20 18:44:50 +01:00
|
|
|
},
|
2017-06-19 23:12:37 +02:00
|
|
|
"screen_name" => user.nickname,
|
|
|
|
"statuses_count" => user_info[:note_count],
|
2017-08-29 17:18:33 +02:00
|
|
|
"statusnet_profile_url" => user.ap_id,
|
2018-01-15 21:18:17 +01:00
|
|
|
"cover_photo" => User.banner_url(user) |> MediaProxy.url(),
|
2018-11-18 22:36:47 +01:00
|
|
|
"background_image" => image_url(user.info.background) |> MediaProxy.url(),
|
2018-05-29 16:13:34 +02:00
|
|
|
"is_local" => user.local,
|
2018-11-18 22:36:47 +01:00
|
|
|
"locked" => user.info.locked,
|
|
|
|
"default_scope" => user.info.default_scope,
|
|
|
|
"no_rich_text" => user.info.no_rich_text,
|
2018-12-06 18:06:50 +01:00
|
|
|
"fields" => fields,
|
2018-12-06 20:26:25 +01:00
|
|
|
|
2018-12-06 18:23:16 +01:00
|
|
|
# Pleroma extension
|
2018-12-06 20:26:25 +01:00
|
|
|
"pleroma" => %{
|
2018-12-18 12:07:05 +01:00
|
|
|
"confirmation_pending" => user_info.confirmation_pending,
|
2018-12-06 20:26:25 +01:00
|
|
|
"tags" => user.tags
|
|
|
|
}
|
2017-03-20 21:30:18 +01:00
|
|
|
}
|
2017-12-04 19:10:15 +01:00
|
|
|
|
|
|
|
if assigns[:token] do
|
2018-12-17 16:02:26 +01:00
|
|
|
Map.put(data, "token", token_string(assigns[:token]))
|
2017-12-04 19:10:15 +01:00
|
|
|
else
|
|
|
|
data
|
|
|
|
end
|
2017-06-19 23:12:37 +02:00
|
|
|
end
|
2017-03-20 21:30:18 +01:00
|
|
|
|
2018-03-30 15:01:53 +02:00
|
|
|
def render("short.json", %{
|
|
|
|
user: %User{
|
|
|
|
nickname: nickname,
|
|
|
|
id: id,
|
|
|
|
ap_id: ap_id,
|
|
|
|
name: name
|
|
|
|
}
|
|
|
|
}) do
|
2017-06-19 23:12:37 +02:00
|
|
|
%{
|
|
|
|
"fullname" => name,
|
|
|
|
"id" => id,
|
|
|
|
"ostatus_uri" => ap_id,
|
|
|
|
"profile_url" => ap_id,
|
|
|
|
"screen_name" => nickname
|
|
|
|
}
|
2017-03-20 21:30:18 +01:00
|
|
|
end
|
2017-11-19 02:22:07 +01:00
|
|
|
|
2018-03-30 15:01:53 +02:00
|
|
|
defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
|
2017-11-19 02:22:07 +01:00
|
|
|
defp image_url(_), do: nil
|
2018-12-17 16:02:26 +01:00
|
|
|
|
|
|
|
defp token_string(%Pleroma.Web.OAuth.Token{token: token_str}), do: token_str
|
|
|
|
defp token_string(token), do: token
|
2017-03-20 21:30:18 +01:00
|
|
|
end
|