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