Merge branch 'get-follow-requests-sql' into 'develop'
[akkoma] / lib / pleroma / web / twitter_api / views / user_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.TwitterAPI.UserView do
6 use Pleroma.Web, :view
7 alias Pleroma.Formatter
8 alias Pleroma.HTML
9 alias Pleroma.User
10 alias Pleroma.Web.CommonAPI.Utils
11 alias Pleroma.Web.MediaProxy
12 alias Pleroma.Web.TwitterAPI.UserView
13
14 def render("show.json", %{user: user = %User{}} = assigns) do
15 render_one(user, Pleroma.Web.TwitterAPI.UserView, "user.json", assigns)
16 end
17
18 def render("index.json", %{users: users, for: user}) do
19 users
20 |> render_many(Pleroma.Web.TwitterAPI.UserView, "user.json", for: user)
21 |> Enum.filter(&Enum.any?/1)
22 end
23
24 def render("user.json", %{user: user = %User{}} = assigns) do
25 if User.visible_for?(user, assigns[:for]),
26 do: do_render("user.json", assigns),
27 else: %{}
28 end
29
30 def render("index_for_admin.json", %{users: users} = opts) do
31 users
32 |> render_many(UserView, "show_for_admin.json", opts)
33 end
34
35 def render("show_for_admin.json", %{user: user}) do
36 %{
37 "id" => user.id,
38 "nickname" => user.nickname,
39 "deactivated" => user.info.deactivated
40 }
41 end
42
43 def render("short.json", %{
44 user: %User{
45 nickname: nickname,
46 id: id,
47 ap_id: ap_id,
48 name: name
49 }
50 }) do
51 %{
52 "fullname" => name,
53 "id" => id,
54 "ostatus_uri" => ap_id,
55 "profile_url" => ap_id,
56 "screen_name" => nickname
57 }
58 end
59
60 defp do_render("user.json", %{user: user = %User{}} = assigns) do
61 for_user = assigns[:for]
62 image = User.avatar_url(user) |> MediaProxy.url()
63
64 {following, follows_you, statusnet_blocking} =
65 if for_user do
66 {
67 User.following?(for_user, user),
68 User.following?(user, for_user),
69 User.blocks?(for_user, user)
70 }
71 else
72 {false, false, false}
73 end
74
75 user_info = User.get_cached_user_info(user)
76
77 emoji =
78 (user.info.source_data["tag"] || [])
79 |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
80 |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
81 {String.trim(name, ":"), url}
82 end)
83
84 # ``fields`` is an array of mastodon profile field, containing ``{"name": "…", "value": "…"}``.
85 # For example: [{"name": "Pronoun", "value": "she/her"}, …]
86 fields =
87 (user.info.source_data["attachment"] || [])
88 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
89 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
90
91 data = %{
92 "created_at" => user.inserted_at |> Utils.format_naive_asctime(),
93 "description" => HTML.strip_tags((user.bio || "") |> String.replace("<br>", "\n")),
94 "description_html" => HTML.filter_tags(user.bio, User.html_filter_policy(for_user)),
95 "favourites_count" => 0,
96 "followers_count" => user_info[:follower_count],
97 "following" => following,
98 "follows_you" => follows_you,
99 "statusnet_blocking" => statusnet_blocking,
100 "friends_count" => user_info[:following_count],
101 "id" => user.id,
102 "name" => user.name || user.nickname,
103 "name_html" =>
104 if(user.name,
105 do: HTML.strip_tags(user.name) |> Formatter.emojify(emoji),
106 else: user.nickname
107 ),
108 "profile_image_url" => image,
109 "profile_image_url_https" => image,
110 "profile_image_url_profile_size" => image,
111 "profile_image_url_original" => image,
112 "rights" => %{
113 "delete_others_notice" => !!user.info.is_moderator,
114 "admin" => !!user.info.is_admin
115 },
116 "screen_name" => user.nickname,
117 "statuses_count" => user_info[:note_count],
118 "statusnet_profile_url" => user.ap_id,
119 "cover_photo" => User.banner_url(user) |> MediaProxy.url(),
120 "background_image" => image_url(user.info.background) |> MediaProxy.url(),
121 "is_local" => user.local,
122 "locked" => user.info.locked,
123 "default_scope" => user.info.default_scope,
124 "no_rich_text" => user.info.no_rich_text,
125 "hide_followers" => user.info.hide_followers,
126 "hide_follows" => user.info.hide_follows,
127 "fields" => fields,
128
129 # Pleroma extension
130 "pleroma" =>
131 %{
132 "confirmation_pending" => user_info.confirmation_pending,
133 "tags" => user.tags
134 }
135 |> maybe_with_activation_status(user, for_user)
136 }
137
138 data =
139 if(user.info.is_admin || user.info.is_moderator,
140 do: maybe_with_role(data, user, for_user),
141 else: data
142 )
143
144 if assigns[:token] do
145 Map.put(data, "token", token_string(assigns[:token]))
146 else
147 data
148 end
149 end
150
151 defp maybe_with_activation_status(data, user, %User{info: %{is_admin: true}}) do
152 Map.put(data, "deactivated", user.info.deactivated)
153 end
154
155 defp maybe_with_activation_status(data, _, _), do: data
156
157 defp maybe_with_role(data, %User{id: id} = user, %User{id: id}) do
158 Map.merge(data, %{"role" => role(user), "show_role" => user.info.show_role})
159 end
160
161 defp maybe_with_role(data, %User{info: %{show_role: true}} = user, _user) do
162 Map.merge(data, %{"role" => role(user)})
163 end
164
165 defp maybe_with_role(data, _, _), do: data
166
167 defp role(%User{info: %{:is_admin => true}}), do: "admin"
168 defp role(%User{info: %{:is_moderator => true}}), do: "moderator"
169 defp role(_), do: "member"
170
171 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
172 defp image_url(_), do: nil
173
174 defp token_string(%Pleroma.Web.OAuth.Token{token: token_str}), do: token_str
175 defp token_string(token), do: token
176 end