Merge remote-tracking branch 'upstream/develop' into confirm-users
[akkoma] / lib / pleroma / web / activity_pub / views / user_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.UserView do
6 use Pleroma.Web, :view
7
8 alias Pleroma.Keys
9 alias Pleroma.Repo
10 alias Pleroma.User
11 alias Pleroma.Web.ActivityPub.Transmogrifier
12 alias Pleroma.Web.ActivityPub.Utils
13 alias Pleroma.Web.Endpoint
14 alias Pleroma.Web.Router.Helpers
15
16 import Ecto.Query
17
18 def render("endpoints.json", %{user: %User{nickname: nil, local: true} = _user}) do
19 %{"sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox)}
20 end
21
22 def render("endpoints.json", %{user: %User{local: true} = _user}) do
23 %{
24 "oauthAuthorizationEndpoint" => Helpers.o_auth_url(Endpoint, :authorize),
25 "oauthRegistrationEndpoint" => Helpers.app_url(Endpoint, :create),
26 "oauthTokenEndpoint" => Helpers.o_auth_url(Endpoint, :token_exchange),
27 "sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox),
28 "uploadMedia" => Helpers.activity_pub_url(Endpoint, :upload_media)
29 }
30 end
31
32 def render("endpoints.json", _), do: %{}
33
34 def render("service.json", %{user: user}) do
35 {:ok, user} = User.ensure_keys_present(user)
36 {:ok, _, public_key} = Keys.keys_from_pem(user.keys)
37 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
38 public_key = :public_key.pem_encode([public_key])
39
40 endpoints = render("endpoints.json", %{user: user})
41
42 %{
43 "id" => user.ap_id,
44 "type" => "Application",
45 "following" => "#{user.ap_id}/following",
46 "followers" => "#{user.ap_id}/followers",
47 "inbox" => "#{user.ap_id}/inbox",
48 "name" => "Pleroma",
49 "summary" =>
50 "An internal service actor for this Pleroma instance. No user-serviceable parts inside.",
51 "url" => user.ap_id,
52 "manuallyApprovesFollowers" => false,
53 "publicKey" => %{
54 "id" => "#{user.ap_id}#main-key",
55 "owner" => user.ap_id,
56 "publicKeyPem" => public_key
57 },
58 "endpoints" => endpoints,
59 "invisible" => User.invisible?(user)
60 }
61 |> Map.merge(Utils.make_json_ld_header())
62 end
63
64 # the instance itself is not a Person, but instead an Application
65 def render("user.json", %{user: %User{nickname: nil} = user}),
66 do: render("service.json", %{user: user})
67
68 def render("user.json", %{user: %User{nickname: "internal." <> _} = user}),
69 do: render("service.json", %{user: user}) |> Map.put("preferredUsername", user.nickname)
70
71 def render("user.json", %{user: user}) do
72 {:ok, user} = User.ensure_keys_present(user)
73 {:ok, _, public_key} = Keys.keys_from_pem(user.keys)
74 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
75 public_key = :public_key.pem_encode([public_key])
76 user = User.sanitize_html(user)
77
78 endpoints = render("endpoints.json", %{user: user})
79
80 emoji_tags = Transmogrifier.take_emoji_tags(user)
81
82 fields = Enum.map(user.fields, &Map.put(&1, "type", "PropertyValue"))
83
84 capabilities =
85 if is_boolean(user.accepts_chat_messages) do
86 %{
87 "acceptsChatMessages" => user.accepts_chat_messages
88 }
89 else
90 %{}
91 end
92
93 %{
94 "id" => user.ap_id,
95 "type" => user.actor_type,
96 "following" => "#{user.ap_id}/following",
97 "followers" => "#{user.ap_id}/followers",
98 "inbox" => "#{user.ap_id}/inbox",
99 "outbox" => "#{user.ap_id}/outbox",
100 "preferredUsername" => user.nickname,
101 "name" => user.name,
102 "summary" => user.bio,
103 "url" => user.ap_id,
104 "manuallyApprovesFollowers" => user.is_locked,
105 "publicKey" => %{
106 "id" => "#{user.ap_id}#main-key",
107 "owner" => user.ap_id,
108 "publicKeyPem" => public_key
109 },
110 "endpoints" => endpoints,
111 "attachment" => fields,
112 "tag" => emoji_tags,
113 # Note: key name is indeed "discoverable" (not an error)
114 "discoverable" => user.is_discoverable,
115 "capabilities" => capabilities,
116 "alsoKnownAs" => user.also_known_as
117 }
118 |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
119 |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
120 |> Map.merge(Utils.make_json_ld_header())
121 end
122
123 def render("following.json", %{user: user, page: page} = opts) do
124 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_follows
125 showing_count = showing_items || !user.hide_follows_count
126
127 query = User.get_friends_query(user)
128 query = from(user in query, select: [:ap_id])
129 following = Repo.all(query)
130
131 total =
132 if showing_count do
133 length(following)
134 else
135 0
136 end
137
138 collection(following, "#{user.ap_id}/following", page, showing_items, total)
139 |> Map.merge(Utils.make_json_ld_header())
140 end
141
142 def render("following.json", %{user: user} = opts) do
143 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_follows
144 showing_count = showing_items || !user.hide_follows_count
145
146 query = User.get_friends_query(user)
147 query = from(user in query, select: [:ap_id])
148 following = Repo.all(query)
149
150 total =
151 if showing_count do
152 length(following)
153 else
154 0
155 end
156
157 %{
158 "id" => "#{user.ap_id}/following",
159 "type" => "OrderedCollection",
160 "totalItems" => total,
161 "first" =>
162 if showing_items do
163 collection(following, "#{user.ap_id}/following", 1, !user.hide_follows)
164 else
165 "#{user.ap_id}/following?page=1"
166 end
167 }
168 |> Map.merge(Utils.make_json_ld_header())
169 end
170
171 def render("followers.json", %{user: user, page: page} = opts) do
172 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_followers
173 showing_count = showing_items || !user.hide_followers_count
174
175 query = User.get_followers_query(user)
176 query = from(user in query, select: [:ap_id])
177 followers = Repo.all(query)
178
179 total =
180 if showing_count do
181 length(followers)
182 else
183 0
184 end
185
186 collection(followers, "#{user.ap_id}/followers", page, showing_items, total)
187 |> Map.merge(Utils.make_json_ld_header())
188 end
189
190 def render("followers.json", %{user: user} = opts) do
191 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_followers
192 showing_count = showing_items || !user.hide_followers_count
193
194 query = User.get_followers_query(user)
195 query = from(user in query, select: [:ap_id])
196 followers = Repo.all(query)
197
198 total =
199 if showing_count do
200 length(followers)
201 else
202 0
203 end
204
205 %{
206 "id" => "#{user.ap_id}/followers",
207 "type" => "OrderedCollection",
208 "first" =>
209 if showing_items do
210 collection(followers, "#{user.ap_id}/followers", 1, showing_items, total)
211 else
212 "#{user.ap_id}/followers?page=1"
213 end
214 }
215 |> maybe_put_total_items(showing_count, total)
216 |> Map.merge(Utils.make_json_ld_header())
217 end
218
219 def render("activity_collection.json", %{iri: iri}) do
220 %{
221 "id" => iri,
222 "type" => "OrderedCollection",
223 "first" => "#{iri}?page=true"
224 }
225 |> Map.merge(Utils.make_json_ld_header())
226 end
227
228 def render("activity_collection_page.json", %{
229 activities: activities,
230 iri: iri,
231 pagination: pagination
232 }) do
233 collection =
234 Enum.map(activities, fn activity ->
235 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
236 data
237 end)
238
239 %{
240 "type" => "OrderedCollectionPage",
241 "partOf" => iri,
242 "orderedItems" => collection
243 }
244 |> Map.merge(Utils.make_json_ld_header())
245 |> Map.merge(pagination)
246 end
247
248 defp maybe_put_total_items(map, false, _total), do: map
249
250 defp maybe_put_total_items(map, true, total) do
251 Map.put(map, "totalItems", total)
252 end
253
254 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
255 offset = (page - 1) * 10
256 items = Enum.slice(collection, offset, 10)
257 items = Enum.map(items, fn user -> user.ap_id end)
258 total = total || length(collection)
259
260 map = %{
261 "id" => "#{iri}?page=#{page}",
262 "type" => "OrderedCollectionPage",
263 "partOf" => iri,
264 "totalItems" => total,
265 "orderedItems" => if(show_items, do: items, else: [])
266 }
267
268 if offset < total do
269 Map.put(map, "next", "#{iri}?page=#{page + 1}")
270 else
271 map
272 end
273 end
274
275 defp maybe_make_image(func, key, user) do
276 if image = func.(user, no_default: true) do
277 %{
278 key => %{
279 "type" => "Image",
280 "url" => image
281 }
282 }
283 else
284 %{}
285 end
286 end
287 end