Add support for AP C2S uploadMedia
[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 "uploadMedia" => Helpers.activity_pub_url(Endpoint, :upload_media)
30 }
31 end
32
33 def render("endpoints.json", _), do: %{}
34
35 def render("service.json", %{user: user}) do
36 {:ok, user} = User.ensure_keys_present(user)
37 {:ok, _, public_key} = Keys.keys_from_pem(user.info.keys)
38 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
39 public_key = :public_key.pem_encode([public_key])
40
41 endpoints = render("endpoints.json", %{user: user})
42
43 %{
44 "id" => user.ap_id,
45 "type" => "Application",
46 "following" => "#{user.ap_id}/following",
47 "followers" => "#{user.ap_id}/followers",
48 "inbox" => "#{user.ap_id}/inbox",
49 "name" => "Pleroma",
50 "summary" =>
51 "An internal service actor for this Pleroma instance. No user-serviceable parts inside.",
52 "url" => user.ap_id,
53 "manuallyApprovesFollowers" => false,
54 "publicKey" => %{
55 "id" => "#{user.ap_id}#main-key",
56 "owner" => user.ap_id,
57 "publicKeyPem" => public_key
58 },
59 "endpoints" => endpoints
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.info.keys)
74 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
75 public_key = :public_key.pem_encode([public_key])
76
77 endpoints = render("endpoints.json", %{user: user})
78
79 user_tags =
80 user
81 |> Transmogrifier.add_emoji_tags()
82 |> Map.get("tag", [])
83
84 fields =
85 user.info
86 |> User.Info.fields()
87 |> Enum.map(fn %{"name" => name, "value" => value} ->
88 %{
89 "name" => Pleroma.HTML.strip_tags(name),
90 "value" => Pleroma.HTML.filter_tags(value, Pleroma.HTML.Scrubber.LinksOnly)
91 }
92 end)
93 |> Enum.map(&Map.put(&1, "type", "PropertyValue"))
94
95 %{
96 "id" => user.ap_id,
97 "type" => "Person",
98 "following" => "#{user.ap_id}/following",
99 "followers" => "#{user.ap_id}/followers",
100 "inbox" => "#{user.ap_id}/inbox",
101 "outbox" => "#{user.ap_id}/outbox",
102 "preferredUsername" => user.nickname,
103 "name" => user.name,
104 "summary" => user.bio,
105 "url" => user.ap_id,
106 "manuallyApprovesFollowers" => user.info.locked,
107 "publicKey" => %{
108 "id" => "#{user.ap_id}#main-key",
109 "owner" => user.ap_id,
110 "publicKeyPem" => public_key
111 },
112 "endpoints" => endpoints,
113 "attachment" => fields,
114 "tag" => (user.info.source_data["tag"] || []) ++ user_tags
115 }
116 |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
117 |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
118 |> Map.merge(Utils.make_json_ld_header())
119 end
120
121 def render("following.json", %{user: user, page: page} = opts) do
122 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
123 showing_count = showing_items || !user.info.hide_follows_count
124
125 query = User.get_friends_query(user)
126 query = from(user in query, select: [:ap_id])
127 following = Repo.all(query)
128
129 total =
130 if showing_count do
131 length(following)
132 else
133 0
134 end
135
136 collection(following, "#{user.ap_id}/following", page, showing_items, total)
137 |> Map.merge(Utils.make_json_ld_header())
138 end
139
140 def render("following.json", %{user: user} = opts) do
141 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
142 showing_count = showing_items || !user.info.hide_follows_count
143
144 query = User.get_friends_query(user)
145 query = from(user in query, select: [:ap_id])
146 following = Repo.all(query)
147
148 total =
149 if showing_count do
150 length(following)
151 else
152 0
153 end
154
155 %{
156 "id" => "#{user.ap_id}/following",
157 "type" => "OrderedCollection",
158 "totalItems" => total,
159 "first" =>
160 if showing_items do
161 collection(following, "#{user.ap_id}/following", 1, !user.info.hide_follows)
162 else
163 "#{user.ap_id}/following?page=1"
164 end
165 }
166 |> Map.merge(Utils.make_json_ld_header())
167 end
168
169 def render("followers.json", %{user: user, page: page} = opts) do
170 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
171 showing_count = showing_items || !user.info.hide_followers_count
172
173 query = User.get_followers_query(user)
174 query = from(user in query, select: [:ap_id])
175 followers = Repo.all(query)
176
177 total =
178 if showing_count do
179 length(followers)
180 else
181 0
182 end
183
184 collection(followers, "#{user.ap_id}/followers", page, showing_items, total)
185 |> Map.merge(Utils.make_json_ld_header())
186 end
187
188 def render("followers.json", %{user: user} = opts) do
189 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
190 showing_count = showing_items || !user.info.hide_followers_count
191
192 query = User.get_followers_query(user)
193 query = from(user in query, select: [:ap_id])
194 followers = Repo.all(query)
195
196 total =
197 if showing_count do
198 length(followers)
199 else
200 0
201 end
202
203 %{
204 "id" => "#{user.ap_id}/followers",
205 "type" => "OrderedCollection",
206 "totalItems" => total,
207 "first" =>
208 if showing_items do
209 collection(followers, "#{user.ap_id}/followers", 1, showing_items, total)
210 else
211 "#{user.ap_id}/followers?page=1"
212 end
213 }
214 |> Map.merge(Utils.make_json_ld_header())
215 end
216
217 def render("outbox.json", %{user: user, max_id: max_qid}) do
218 params = %{
219 "limit" => "10"
220 }
221
222 params =
223 if max_qid != nil do
224 Map.put(params, "max_id", max_qid)
225 else
226 params
227 end
228
229 activities = ActivityPub.fetch_user_activities(user, nil, params)
230
231 # this is sorted chronologically, so first activity is the newest (max)
232 {max_id, min_id, collection} =
233 if length(activities) > 0 do
234 {
235 Enum.at(activities, 0).id,
236 Enum.at(Enum.reverse(activities), 0).id,
237 Enum.map(activities, fn act ->
238 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
239 data
240 end)
241 }
242 else
243 {
244 0,
245 0,
246 []
247 }
248 end
249
250 iri = "#{user.ap_id}/outbox"
251
252 page = %{
253 "id" => "#{iri}?max_id=#{max_id}",
254 "type" => "OrderedCollectionPage",
255 "partOf" => iri,
256 "orderedItems" => collection,
257 "next" => "#{iri}?max_id=#{min_id}"
258 }
259
260 if max_qid == nil do
261 %{
262 "id" => iri,
263 "type" => "OrderedCollection",
264 "first" => page
265 }
266 |> Map.merge(Utils.make_json_ld_header())
267 else
268 page |> Map.merge(Utils.make_json_ld_header())
269 end
270 end
271
272 def render("inbox.json", %{user: user, max_id: max_qid}) do
273 params = %{
274 "limit" => "10"
275 }
276
277 params =
278 if max_qid != nil do
279 Map.put(params, "max_id", max_qid)
280 else
281 params
282 end
283
284 activities = ActivityPub.fetch_activities([user.ap_id | user.following], params)
285
286 min_id = Enum.at(Enum.reverse(activities), 0).id
287 max_id = Enum.at(activities, 0).id
288
289 collection =
290 Enum.map(activities, fn act ->
291 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
292 data
293 end)
294
295 iri = "#{user.ap_id}/inbox"
296
297 page = %{
298 "id" => "#{iri}?max_id=#{max_id}",
299 "type" => "OrderedCollectionPage",
300 "partOf" => iri,
301 "orderedItems" => collection,
302 "next" => "#{iri}?max_id=#{min_id}"
303 }
304
305 if max_qid == nil do
306 %{
307 "id" => iri,
308 "type" => "OrderedCollection",
309 "first" => page
310 }
311 |> Map.merge(Utils.make_json_ld_header())
312 else
313 page |> Map.merge(Utils.make_json_ld_header())
314 end
315 end
316
317 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
318 offset = (page - 1) * 10
319 items = Enum.slice(collection, offset, 10)
320 items = Enum.map(items, fn user -> user.ap_id end)
321 total = total || length(collection)
322
323 map = %{
324 "id" => "#{iri}?page=#{page}",
325 "type" => "OrderedCollectionPage",
326 "partOf" => iri,
327 "totalItems" => total,
328 "orderedItems" => if(show_items, do: items, else: [])
329 }
330
331 if offset < total do
332 Map.put(map, "next", "#{iri}?page=#{page + 1}")
333 else
334 map
335 end
336 end
337
338 defp maybe_make_image(func, key, user) do
339 if image = func.(user, no_default: true) do
340 %{
341 key => %{
342 "type" => "Image",
343 "url" => image
344 }
345 }
346 else
347 %{}
348 end
349 end
350 end