Merge branch 'fix/admin-api-user-deletion' into 'develop'
[akkoma] / lib / pleroma / web / activity_pub / 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.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.ActivityPub
12 alias Pleroma.Web.ActivityPub.Transmogrifier
13 alias Pleroma.Web.ActivityPub.Utils
14 alias Pleroma.Web.Endpoint
15 alias Pleroma.Web.Router.Helpers
16
17 import Ecto.Query
18
19 def render("endpoints.json", %{user: %User{nickname: nil, local: true} = _user}) do
20 %{"sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox)}
21 end
22
23 def render("endpoints.json", %{user: %User{local: true} = _user}) do
24 %{
25 "oauthAuthorizationEndpoint" => Helpers.o_auth_url(Endpoint, :authorize),
26 "oauthRegistrationEndpoint" => Helpers.mastodon_api_url(Endpoint, :create_app),
27 "oauthTokenEndpoint" => Helpers.o_auth_url(Endpoint, :token_exchange),
28 "sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox)
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.info.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 }
60 |> Map.merge(Utils.make_json_ld_header())
61 end
62
63 # the instance itself is not a Person, but instead an Application
64 def render("user.json", %{user: %User{nickname: nil} = user}),
65 do: render("service.json", %{user: user})
66
67 def render("user.json", %{user: %User{nickname: "internal." <> _} = user}),
68 do: render("service.json", %{user: user}) |> Map.put("preferredUsername", user.nickname)
69
70 def render("user.json", %{user: user}) do
71 {:ok, user} = User.ensure_keys_present(user)
72 {:ok, _, public_key} = Keys.keys_from_pem(user.info.keys)
73 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
74 public_key = :public_key.pem_encode([public_key])
75
76 endpoints = render("endpoints.json", %{user: user})
77
78 user_tags =
79 user
80 |> Transmogrifier.add_emoji_tags()
81 |> Map.get("tag", [])
82
83 fields =
84 user.info
85 |> User.Info.fields()
86 |> Enum.map(fn %{"name" => name, "value" => value} ->
87 %{
88 "name" => Pleroma.HTML.strip_tags(name),
89 "value" => Pleroma.HTML.filter_tags(value, Pleroma.HTML.Scrubber.LinksOnly)
90 }
91 end)
92 |> Enum.map(&Map.put(&1, "type", "PropertyValue"))
93
94 %{
95 "id" => user.ap_id,
96 "type" => "Person",
97 "following" => "#{user.ap_id}/following",
98 "followers" => "#{user.ap_id}/followers",
99 "inbox" => "#{user.ap_id}/inbox",
100 "outbox" => "#{user.ap_id}/outbox",
101 "preferredUsername" => user.nickname,
102 "name" => user.name,
103 "summary" => user.bio,
104 "url" => user.ap_id,
105 "manuallyApprovesFollowers" => user.info.locked,
106 "publicKey" => %{
107 "id" => "#{user.ap_id}#main-key",
108 "owner" => user.ap_id,
109 "publicKeyPem" => public_key
110 },
111 "endpoints" => endpoints,
112 "attachment" => fields,
113 "tag" => (user.info.source_data["tag"] || []) ++ user_tags
114 }
115 |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
116 |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
117 |> Map.merge(Utils.make_json_ld_header())
118 end
119
120 def render("following.json", %{user: user, page: page} = opts) do
121 showing = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
122 query = User.get_friends_query(user)
123 query = from(user in query, select: [:ap_id])
124 following = Repo.all(query)
125
126 total =
127 if showing do
128 length(following)
129 else
130 0
131 end
132
133 collection(following, "#{user.ap_id}/following", page, showing, total)
134 |> Map.merge(Utils.make_json_ld_header())
135 end
136
137 def render("following.json", %{user: user} = opts) do
138 showing = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
139 query = User.get_friends_query(user)
140 query = from(user in query, select: [:ap_id])
141 following = Repo.all(query)
142
143 total =
144 if showing do
145 length(following)
146 else
147 0
148 end
149
150 %{
151 "id" => "#{user.ap_id}/following",
152 "type" => "OrderedCollection",
153 "totalItems" => total,
154 "first" =>
155 if showing do
156 collection(following, "#{user.ap_id}/following", 1, !user.info.hide_follows)
157 else
158 "#{user.ap_id}/following?page=1"
159 end
160 }
161 |> Map.merge(Utils.make_json_ld_header())
162 end
163
164 def render("followers.json", %{user: user, page: page} = opts) do
165 showing = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
166
167 query = User.get_followers_query(user)
168 query = from(user in query, select: [:ap_id])
169 followers = Repo.all(query)
170
171 total =
172 if showing do
173 length(followers)
174 else
175 0
176 end
177
178 collection(followers, "#{user.ap_id}/followers", page, showing, total)
179 |> Map.merge(Utils.make_json_ld_header())
180 end
181
182 def render("followers.json", %{user: user} = opts) do
183 showing = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
184
185 query = User.get_followers_query(user)
186 query = from(user in query, select: [:ap_id])
187 followers = Repo.all(query)
188
189 total =
190 if showing do
191 length(followers)
192 else
193 0
194 end
195
196 %{
197 "id" => "#{user.ap_id}/followers",
198 "type" => "OrderedCollection",
199 "totalItems" => total,
200 "first" =>
201 if showing do
202 collection(followers, "#{user.ap_id}/followers", 1, showing, total)
203 else
204 "#{user.ap_id}/followers?page=1"
205 end
206 }
207 |> Map.merge(Utils.make_json_ld_header())
208 end
209
210 def render("outbox.json", %{user: user, max_id: max_qid}) do
211 params = %{
212 "limit" => "10"
213 }
214
215 params =
216 if max_qid != nil do
217 Map.put(params, "max_id", max_qid)
218 else
219 params
220 end
221
222 activities = ActivityPub.fetch_user_activities(user, nil, params)
223
224 {max_id, min_id, collection} =
225 if length(activities) > 0 do
226 {
227 Enum.at(Enum.reverse(activities), 0).id,
228 Enum.at(activities, 0).id,
229 Enum.map(activities, fn act ->
230 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
231 data
232 end)
233 }
234 else
235 {
236 0,
237 0,
238 []
239 }
240 end
241
242 iri = "#{user.ap_id}/outbox"
243
244 page = %{
245 "id" => "#{iri}?max_id=#{max_id}",
246 "type" => "OrderedCollectionPage",
247 "partOf" => iri,
248 "orderedItems" => collection,
249 "next" => "#{iri}?max_id=#{min_id}"
250 }
251
252 if max_qid == nil do
253 %{
254 "id" => iri,
255 "type" => "OrderedCollection",
256 "first" => page
257 }
258 |> Map.merge(Utils.make_json_ld_header())
259 else
260 page |> Map.merge(Utils.make_json_ld_header())
261 end
262 end
263
264 def render("inbox.json", %{user: user, max_id: max_qid}) do
265 params = %{
266 "limit" => "10"
267 }
268
269 params =
270 if max_qid != nil do
271 Map.put(params, "max_id", max_qid)
272 else
273 params
274 end
275
276 activities = ActivityPub.fetch_activities([user.ap_id | user.following], params)
277
278 min_id = Enum.at(Enum.reverse(activities), 0).id
279 max_id = Enum.at(activities, 0).id
280
281 collection =
282 Enum.map(activities, fn act ->
283 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
284 data
285 end)
286
287 iri = "#{user.ap_id}/inbox"
288
289 page = %{
290 "id" => "#{iri}?max_id=#{max_id}",
291 "type" => "OrderedCollectionPage",
292 "partOf" => iri,
293 "orderedItems" => collection,
294 "next" => "#{iri}?max_id=#{min_id}"
295 }
296
297 if max_qid == nil do
298 %{
299 "id" => iri,
300 "type" => "OrderedCollection",
301 "first" => page
302 }
303 |> Map.merge(Utils.make_json_ld_header())
304 else
305 page |> Map.merge(Utils.make_json_ld_header())
306 end
307 end
308
309 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
310 offset = (page - 1) * 10
311 items = Enum.slice(collection, offset, 10)
312 items = Enum.map(items, fn user -> user.ap_id end)
313 total = total || length(collection)
314
315 map = %{
316 "id" => "#{iri}?page=#{page}",
317 "type" => "OrderedCollectionPage",
318 "partOf" => iri,
319 "totalItems" => total,
320 "orderedItems" => if(show_items, do: items, else: [])
321 }
322
323 if offset < total do
324 Map.put(map, "next", "#{iri}?page=#{page + 1}")
325 else
326 map
327 end
328 end
329
330 defp maybe_make_image(func, key, user) do
331 if image = func.(user, no_default: true) do
332 %{
333 key => %{
334 "type" => "Image",
335 "url" => image
336 }
337 }
338 else
339 %{}
340 end
341 end
342 end