1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.UserView do
8 alias Pleroma.Formatter
9 alias Pleroma.Web.CommonAPI.Utils
10 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
18 render_many(users, Pleroma.Web.TwitterAPI.UserView, "user.json", for: user)
21 def render("user.json", %{user: user = %User{}} = assigns) do
22 image = User.avatar_url(user) |> MediaProxy.url()
24 {following, follows_you, statusnet_blocking} =
27 User.following?(assigns[:for], user),
28 User.following?(user, assigns[:for]),
29 User.blocks?(assigns[:for], user)
35 user_info = User.get_cached_user_info(user)
38 (user.info.source_data["tag"] || [])
39 |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
40 |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
41 {String.trim(name, ":"), url}
44 # ``fields`` is an array of mastodon profile field, containing ``{"name": "…", "value": "…"}``.
45 # For example: [{"name": "Pronoun", "value": "she/her"}, …]
47 (user.info.source_data["attachment"] || [])
48 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
49 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
52 "created_at" => user.inserted_at |> Utils.format_naive_asctime(),
53 "description" => HTML.strip_tags((user.bio || "") |> String.replace("<br>", "\n")),
54 "description_html" => HTML.filter_tags(user.bio, User.html_filter_policy(assigns[:for])),
55 "favourites_count" => 0,
56 "followers_count" => user_info[:follower_count],
57 "following" => following,
58 "follows_you" => follows_you,
59 "statusnet_blocking" => statusnet_blocking,
60 "friends_count" => user_info[:following_count],
62 "name" => user.name || user.nickname,
65 do: HTML.strip_tags(user.name) |> Formatter.emojify(emoji),
68 "profile_image_url" => image,
69 "profile_image_url_https" => image,
70 "profile_image_url_profile_size" => image,
71 "profile_image_url_original" => image,
73 "delete_others_notice" => !!user.info.is_moderator,
74 "admin" => !!user.info.is_admin
76 "screen_name" => user.nickname,
77 "statuses_count" => user_info[:note_count],
78 "statusnet_profile_url" => user.ap_id,
79 "cover_photo" => User.banner_url(user) |> MediaProxy.url(),
80 "background_image" => image_url(user.info.background) |> MediaProxy.url(),
81 "is_local" => user.local,
82 "locked" => user.info.locked,
83 "default_scope" => user.info.default_scope,
84 "no_rich_text" => user.info.no_rich_text,
89 "confirmation_pending" => user_info.confirmation_pending,
95 Map.put(data, "token", token_string(assigns[:token]))
101 def render("short.json", %{
112 "ostatus_uri" => ap_id,
113 "profile_url" => ap_id,
114 "screen_name" => nickname
118 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
119 defp image_url(_), do: nil
121 defp token_string(%Pleroma.Web.OAuth.Token{token: token_str}), do: token_str
122 defp token_string(token), do: token