1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.UserView do
7 alias Pleroma.Formatter
10 alias Pleroma.Web.CommonAPI.Utils
11 alias Pleroma.Web.MediaProxy
13 def render("show.json", %{user: user = %User{}} = assigns) do
14 render_one(user, Pleroma.Web.TwitterAPI.UserView, "user.json", assigns)
17 def render("index.json", %{users: users, for: user}) do
19 |> render_many(Pleroma.Web.TwitterAPI.UserView, "user.json", for: user)
20 |> Enum.filter(&Enum.any?/1)
23 def render("user.json", %{user: user = %User{}} = assigns) do
24 if User.visible_for?(user, assigns[:for]),
25 do: do_render("user.json", assigns),
29 def render("short.json", %{
40 "ostatus_uri" => ap_id,
41 "profile_url" => ap_id,
42 "screen_name" => nickname
46 defp do_render("user.json", %{user: user = %User{}} = assigns) do
47 for_user = assigns[:for]
48 image = User.avatar_url(user) |> MediaProxy.url()
50 {following, follows_you, statusnet_blocking} =
53 User.following?(for_user, user),
54 User.following?(user, for_user),
55 User.blocks?(for_user, user)
61 user_info = User.get_cached_user_info(user)
64 (user.info.source_data["tag"] || [])
65 |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
66 |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
67 {String.trim(name, ":"), url}
70 emoji = Enum.dedup(emoji ++ user.info.emoji)
74 |> HTML.filter_tags(User.html_filter_policy(for_user))
75 |> Formatter.emojify(emoji)
77 # ``fields`` is an array of mastodon profile field, containing ``{"name": "…", "value": "…"}``.
78 # For example: [{"name": "Pronoun", "value": "she/her"}, …]
80 (user.info.source_data["attachment"] || [])
81 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
82 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
86 "created_at" => user.inserted_at |> Utils.format_naive_asctime(),
87 "description" => HTML.strip_tags((user.bio || "") |> String.replace("<br>", "\n")),
88 "description_html" => description_html,
89 "favourites_count" => 0,
90 "followers_count" => user_info[:follower_count],
91 "following" => following,
92 "follows_you" => follows_you,
93 "statusnet_blocking" => statusnet_blocking,
94 "friends_count" => user_info[:following_count],
96 "name" => user.name || user.nickname,
99 do: HTML.strip_tags(user.name) |> Formatter.emojify(emoji),
102 "profile_image_url" => image,
103 "profile_image_url_https" => image,
104 "profile_image_url_profile_size" => image,
105 "profile_image_url_original" => image,
106 "screen_name" => user.nickname,
107 "statuses_count" => user_info[:note_count],
108 "statusnet_profile_url" => user.ap_id,
109 "cover_photo" => User.banner_url(user) |> MediaProxy.url(),
110 "background_image" => image_url(user.info.background) |> MediaProxy.url(),
111 "is_local" => user.local,
112 "locked" => user.info.locked,
113 "hide_followers" => user.info.hide_followers,
114 "hide_follows" => user.info.hide_follows,
120 "confirmation_pending" => user_info.confirmation_pending,
123 |> maybe_with_activation_status(user, for_user)
125 |> maybe_with_user_settings(user, for_user)
126 |> maybe_with_role(user, for_user)
128 if assigns[:token] do
129 Map.put(data, "token", token_string(assigns[:token]))
135 defp maybe_with_activation_status(data, user, %User{info: %{is_admin: true}}) do
136 Map.put(data, "deactivated", user.info.deactivated)
139 defp maybe_with_activation_status(data, _, _), do: data
141 defp maybe_with_role(data, %User{id: id} = user, %User{id: id}) do
143 "role" => role(user),
144 "show_role" => user.info.show_role,
146 "delete_others_notice" => !!user.info.is_moderator,
147 "admin" => !!user.info.is_admin
152 defp maybe_with_role(data, %User{info: %{show_role: true}} = user, _user) do
154 "role" => role(user),
156 "delete_others_notice" => !!user.info.is_moderator,
157 "admin" => !!user.info.is_admin
162 defp maybe_with_role(data, _, _), do: data
164 defp maybe_with_user_settings(data, %User{info: info, id: id} = _user, %User{id: id}) do
166 |> Kernel.put_in(["default_scope"], info.default_scope)
167 |> Kernel.put_in(["no_rich_text"], info.no_rich_text)
170 defp maybe_with_user_settings(data, _, _), do: data
171 defp role(%User{info: %{:is_admin => true}}), do: "admin"
172 defp role(%User{info: %{:is_moderator => true}}), do: "moderator"
173 defp role(_), do: "member"
175 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
176 defp image_url(_), do: nil
178 defp token_string(%Pleroma.Web.OAuth.Token{token: token_str}), do: token_str
179 defp token_string(token), do: token