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