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