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