Merge remote-tracking branch 'pleroma/develop' into feature/addressable-lists
[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}) do
102 query = User.get_friends_query(user)
103 query = from(user in query, select: [:ap_id])
104 following = Repo.all(query)
105
106 total =
107 if !user.info.hide_follows do
108 length(following)
109 else
110 0
111 end
112
113 collection(following, "#{user.ap_id}/following", page, !user.info.hide_follows, total)
114 |> Map.merge(Utils.make_json_ld_header())
115 end
116
117 def render("following.json", %{user: user}) do
118 query = User.get_friends_query(user)
119 query = from(user in query, select: [:ap_id])
120 following = Repo.all(query)
121
122 total =
123 if !user.info.hide_follows do
124 length(following)
125 else
126 0
127 end
128
129 %{
130 "id" => "#{user.ap_id}/following",
131 "type" => "OrderedCollection",
132 "totalItems" => total,
133 "first" => collection(following, "#{user.ap_id}/following", 1, !user.info.hide_follows)
134 }
135 |> Map.merge(Utils.make_json_ld_header())
136 end
137
138 def render("followers.json", %{user: user, page: page}) do
139 query = User.get_followers_query(user)
140 query = from(user in query, select: [:ap_id])
141 followers = Repo.all(query)
142
143 total =
144 if !user.info.hide_followers do
145 length(followers)
146 else
147 0
148 end
149
150 collection(followers, "#{user.ap_id}/followers", page, !user.info.hide_followers, total)
151 |> Map.merge(Utils.make_json_ld_header())
152 end
153
154 def render("followers.json", %{user: user}) do
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 !user.info.hide_followers do
161 length(followers)
162 else
163 0
164 end
165
166 %{
167 "id" => "#{user.ap_id}/followers",
168 "type" => "OrderedCollection",
169 "totalItems" => total,
170 "first" =>
171 collection(followers, "#{user.ap_id}/followers", 1, !user.info.hide_followers, total)
172 }
173 |> Map.merge(Utils.make_json_ld_header())
174 end
175
176 def render("outbox.json", %{user: user, max_id: max_qid}) do
177 params = %{
178 "limit" => "10"
179 }
180
181 params =
182 if max_qid != nil do
183 Map.put(params, "max_id", max_qid)
184 else
185 params
186 end
187
188 activities = ActivityPub.fetch_user_activities(user, nil, params)
189
190 {max_id, min_id, collection} =
191 if length(activities) > 0 do
192 {
193 Enum.at(Enum.reverse(activities), 0).id,
194 Enum.at(activities, 0).id,
195 Enum.map(activities, fn act ->
196 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
197 data
198 end)
199 }
200 else
201 {
202 0,
203 0,
204 []
205 }
206 end
207
208 iri = "#{user.ap_id}/outbox"
209
210 page = %{
211 "id" => "#{iri}?max_id=#{max_id}",
212 "type" => "OrderedCollectionPage",
213 "partOf" => iri,
214 "orderedItems" => collection,
215 "next" => "#{iri}?max_id=#{min_id}"
216 }
217
218 if max_qid == nil do
219 %{
220 "id" => iri,
221 "type" => "OrderedCollection",
222 "first" => page
223 }
224 |> Map.merge(Utils.make_json_ld_header())
225 else
226 page |> Map.merge(Utils.make_json_ld_header())
227 end
228 end
229
230 def render("inbox.json", %{user: user, max_id: max_qid}) do
231 params = %{
232 "limit" => "10"
233 }
234
235 params =
236 if max_qid != nil do
237 Map.put(params, "max_id", max_qid)
238 else
239 params
240 end
241
242 activities = ActivityPub.fetch_activities([user.ap_id | user.following], params)
243
244 min_id = Enum.at(Enum.reverse(activities), 0).id
245 max_id = Enum.at(activities, 0).id
246
247 collection =
248 Enum.map(activities, fn act ->
249 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
250 data
251 end)
252
253 iri = "#{user.ap_id}/inbox"
254
255 page = %{
256 "id" => "#{iri}?max_id=#{max_id}",
257 "type" => "OrderedCollectionPage",
258 "partOf" => iri,
259 "orderedItems" => collection,
260 "next" => "#{iri}?max_id=#{min_id}"
261 }
262
263 if max_qid == nil do
264 %{
265 "id" => iri,
266 "type" => "OrderedCollection",
267 "first" => page
268 }
269 |> Map.merge(Utils.make_json_ld_header())
270 else
271 page |> Map.merge(Utils.make_json_ld_header())
272 end
273 end
274
275 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
276 offset = (page - 1) * 10
277 items = Enum.slice(collection, offset, 10)
278 items = Enum.map(items, fn user -> user.ap_id end)
279 total = total || length(collection)
280
281 map = %{
282 "id" => "#{iri}?page=#{page}",
283 "type" => "OrderedCollectionPage",
284 "partOf" => iri,
285 "totalItems" => total,
286 "orderedItems" => if(show_items, do: items, else: [])
287 }
288
289 if offset < total do
290 Map.put(map, "next", "#{iri}?page=#{page + 1}")
291 else
292 map
293 end
294 end
295
296 defp maybe_make_image(func, key, user) do
297 if image = func.(user, no_default: true) do
298 %{
299 key => %{
300 "type" => "Image",
301 "url" => image
302 }
303 }
304 else
305 %{}
306 end
307 end
308 end