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