Merge branch 'develop' into tests/mastodon_api_controller.ex
[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_items = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
122 showing_count = showing_items || !user.info.hide_follows_count
123
124 query = User.get_friends_query(user)
125 query = from(user in query, select: [:ap_id])
126 following = Repo.all(query)
127
128 total =
129 if showing_count do
130 length(following)
131 else
132 0
133 end
134
135 collection(following, "#{user.ap_id}/following", page, showing_items, total)
136 |> Map.merge(Utils.make_json_ld_header())
137 end
138
139 def render("following.json", %{user: user} = opts) do
140 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
141 showing_count = showing_items || !user.info.hide_follows_count
142
143 query = User.get_friends_query(user)
144 query = from(user in query, select: [:ap_id])
145 following = Repo.all(query)
146
147 total =
148 if showing_count do
149 length(following)
150 else
151 0
152 end
153
154 %{
155 "id" => "#{user.ap_id}/following",
156 "type" => "OrderedCollection",
157 "totalItems" => total,
158 "first" =>
159 if showing_items do
160 collection(following, "#{user.ap_id}/following", 1, !user.info.hide_follows)
161 else
162 "#{user.ap_id}/following?page=1"
163 end
164 }
165 |> Map.merge(Utils.make_json_ld_header())
166 end
167
168 def render("followers.json", %{user: user, page: page} = opts) do
169 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
170 showing_count = showing_items || !user.info.hide_followers_count
171
172 query = User.get_followers_query(user)
173 query = from(user in query, select: [:ap_id])
174 followers = Repo.all(query)
175
176 total =
177 if showing_count do
178 length(followers)
179 else
180 0
181 end
182
183 collection(followers, "#{user.ap_id}/followers", page, showing_items, total)
184 |> Map.merge(Utils.make_json_ld_header())
185 end
186
187 def render("followers.json", %{user: user} = opts) do
188 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
189 showing_count = showing_items || !user.info.hide_followers_count
190
191 query = User.get_followers_query(user)
192 query = from(user in query, select: [:ap_id])
193 followers = Repo.all(query)
194
195 total =
196 if showing_count do
197 length(followers)
198 else
199 0
200 end
201
202 %{
203 "id" => "#{user.ap_id}/followers",
204 "type" => "OrderedCollection",
205 "totalItems" => total,
206 "first" =>
207 if showing_items do
208 collection(followers, "#{user.ap_id}/followers", 1, showing_items, total)
209 else
210 "#{user.ap_id}/followers?page=1"
211 end
212 }
213 |> Map.merge(Utils.make_json_ld_header())
214 end
215
216 def render("outbox.json", %{user: user, max_id: max_qid}) do
217 params = %{
218 "limit" => "10"
219 }
220
221 params =
222 if max_qid != nil do
223 Map.put(params, "max_id", max_qid)
224 else
225 params
226 end
227
228 activities = ActivityPub.fetch_user_activities(user, nil, params)
229
230 # this is sorted chronologically, so first activity is the newest (max)
231 {max_id, min_id, collection} =
232 if length(activities) > 0 do
233 {
234 Enum.at(activities, 0).id,
235 Enum.at(Enum.reverse(activities), 0).id,
236 Enum.map(activities, fn act ->
237 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
238 data
239 end)
240 }
241 else
242 {
243 0,
244 0,
245 []
246 }
247 end
248
249 iri = "#{user.ap_id}/outbox"
250
251 page = %{
252 "id" => "#{iri}?max_id=#{max_id}",
253 "type" => "OrderedCollectionPage",
254 "partOf" => iri,
255 "orderedItems" => collection,
256 "next" => "#{iri}?max_id=#{min_id}"
257 }
258
259 if max_qid == nil do
260 %{
261 "id" => iri,
262 "type" => "OrderedCollection",
263 "first" => page
264 }
265 |> Map.merge(Utils.make_json_ld_header())
266 else
267 page |> Map.merge(Utils.make_json_ld_header())
268 end
269 end
270
271 def render("inbox.json", %{user: user, max_id: max_qid}) do
272 params = %{
273 "limit" => "10"
274 }
275
276 params =
277 if max_qid != nil do
278 Map.put(params, "max_id", max_qid)
279 else
280 params
281 end
282
283 activities = ActivityPub.fetch_activities([user.ap_id | user.following], params)
284
285 min_id = Enum.at(Enum.reverse(activities), 0).id
286 max_id = Enum.at(activities, 0).id
287
288 collection =
289 Enum.map(activities, fn act ->
290 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
291 data
292 end)
293
294 iri = "#{user.ap_id}/inbox"
295
296 page = %{
297 "id" => "#{iri}?max_id=#{max_id}",
298 "type" => "OrderedCollectionPage",
299 "partOf" => iri,
300 "orderedItems" => collection,
301 "next" => "#{iri}?max_id=#{min_id}"
302 }
303
304 if max_qid == nil do
305 %{
306 "id" => iri,
307 "type" => "OrderedCollection",
308 "first" => page
309 }
310 |> Map.merge(Utils.make_json_ld_header())
311 else
312 page |> Map.merge(Utils.make_json_ld_header())
313 end
314 end
315
316 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
317 offset = (page - 1) * 10
318 items = Enum.slice(collection, offset, 10)
319 items = Enum.map(items, fn user -> user.ap_id end)
320 total = total || length(collection)
321
322 map = %{
323 "id" => "#{iri}?page=#{page}",
324 "type" => "OrderedCollectionPage",
325 "partOf" => iri,
326 "totalItems" => total,
327 "orderedItems" => if(show_items, do: items, else: [])
328 }
329
330 if offset < total do
331 Map.put(map, "next", "#{iri}?page=#{page + 1}")
332 else
333 map
334 end
335 end
336
337 defp maybe_make_image(func, key, user) do
338 if image = func.(user, no_default: true) do
339 %{
340 key => %{
341 "type" => "Image",
342 "url" => image
343 }
344 }
345 else
346 %{}
347 end
348 end
349 end