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