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