Merge remote-tracking branch 'upstream/develop' into aliases
[akkoma] / lib / pleroma / web / activity_pub / views / user_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.Transmogrifier
12 alias Pleroma.Web.ActivityPub.Utils
13 alias Pleroma.Web.Endpoint
14 alias Pleroma.Web.Router.Helpers
15
16 import Ecto.Query
17
18 def render("endpoints.json", %{user: %User{nickname: nil, local: true} = _user}) do
19 %{"sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox)}
20 end
21
22 def render("endpoints.json", %{user: %User{local: true} = _user}) do
23 %{
24 "oauthAuthorizationEndpoint" => Helpers.o_auth_url(Endpoint, :authorize),
25 "oauthRegistrationEndpoint" => Helpers.app_url(Endpoint, :create),
26 "oauthTokenEndpoint" => Helpers.o_auth_url(Endpoint, :token_exchange),
27 "sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox),
28 "uploadMedia" => Helpers.activity_pub_url(Endpoint, :upload_media)
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.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 "invisible" => User.invisible?(user)
60 }
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}) |> Map.put("preferredUsername", user.nickname)
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.keys)
74 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
75 public_key = :public_key.pem_encode([public_key])
76 user = User.sanitize_html(user)
77
78 endpoints = render("endpoints.json", %{user: user})
79
80 emoji_tags = Transmogrifier.take_emoji_tags(user)
81
82 fields = Enum.map(user.fields, &Map.put(&1, "type", "PropertyValue"))
83
84 capabilities =
85 if is_boolean(user.accepts_chat_messages) do
86 %{
87 "acceptsChatMessages" => user.accepts_chat_messages
88 }
89 else
90 %{}
91 end
92
93 %{
94 "id" => user.ap_id,
95 "type" => user.actor_type,
96 "following" => "#{user.ap_id}/following",
97 "followers" => "#{user.ap_id}/followers",
98 "inbox" => "#{user.ap_id}/inbox",
99 "outbox" => "#{user.ap_id}/outbox",
100 "preferredUsername" => user.nickname,
101 "name" => user.name,
102 "summary" => user.bio,
103 "url" => user.ap_id,
104 "manuallyApprovesFollowers" => user.is_locked,
105 "publicKey" => %{
106 "id" => "#{user.ap_id}#main-key",
107 "owner" => user.ap_id,
108 "publicKeyPem" => public_key
109 },
110 "endpoints" => endpoints,
111 "attachment" => fields,
112 "tag" => emoji_tags,
113 # Note: key name is indeed "discoverable" (not an error)
114 "discoverable" => user.is_discoverable,
115 "capabilities" => capabilities
116 }
117 |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
118 |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
119 |> Map.merge(Utils.make_json_ld_header())
120 end
121
122 def render("following.json", %{user: user, page: page} = opts) do
123 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_follows
124 showing_count = showing_items || !user.hide_follows_count
125
126 query = User.get_friends_query(user)
127 query = from(user in query, select: [:ap_id])
128 following = Repo.all(query)
129
130 total =
131 if showing_count do
132 length(following)
133 else
134 0
135 end
136
137 collection(following, "#{user.ap_id}/following", page, showing_items, total)
138 |> Map.merge(Utils.make_json_ld_header())
139 end
140
141 def render("following.json", %{user: user} = opts) do
142 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_follows
143 showing_count = showing_items || !user.hide_follows_count
144
145 query = User.get_friends_query(user)
146 query = from(user in query, select: [:ap_id])
147 following = Repo.all(query)
148
149 total =
150 if showing_count do
151 length(following)
152 else
153 0
154 end
155
156 %{
157 "id" => "#{user.ap_id}/following",
158 "type" => "OrderedCollection",
159 "totalItems" => total,
160 "first" =>
161 if showing_items do
162 collection(following, "#{user.ap_id}/following", 1, !user.hide_follows)
163 else
164 "#{user.ap_id}/following?page=1"
165 end
166 }
167 |> Map.merge(Utils.make_json_ld_header())
168 end
169
170 def render("followers.json", %{user: user, page: page} = opts) do
171 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_followers
172 showing_count = showing_items || !user.hide_followers_count
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_count do
180 length(followers)
181 else
182 0
183 end
184
185 collection(followers, "#{user.ap_id}/followers", page, showing_items, total)
186 |> Map.merge(Utils.make_json_ld_header())
187 end
188
189 def render("followers.json", %{user: user} = opts) do
190 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_followers
191 showing_count = showing_items || !user.hide_followers_count
192
193 query = User.get_followers_query(user)
194 query = from(user in query, select: [:ap_id])
195 followers = Repo.all(query)
196
197 total =
198 if showing_count do
199 length(followers)
200 else
201 0
202 end
203
204 %{
205 "id" => "#{user.ap_id}/followers",
206 "type" => "OrderedCollection",
207 "first" =>
208 if showing_items do
209 collection(followers, "#{user.ap_id}/followers", 1, showing_items, total)
210 else
211 "#{user.ap_id}/followers?page=1"
212 end
213 }
214 |> maybe_put_total_items(showing_count, total)
215 |> Map.merge(Utils.make_json_ld_header())
216 end
217
218 def render("activity_collection.json", %{iri: iri}) do
219 %{
220 "id" => iri,
221 "type" => "OrderedCollection",
222 "first" => "#{iri}?page=true"
223 }
224 |> Map.merge(Utils.make_json_ld_header())
225 end
226
227 def render("activity_collection_page.json", %{
228 activities: activities,
229 iri: iri,
230 pagination: pagination
231 }) do
232 collection =
233 Enum.map(activities, fn activity ->
234 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
235 data
236 end)
237
238 %{
239 "type" => "OrderedCollectionPage",
240 "partOf" => iri,
241 "orderedItems" => collection
242 }
243 |> Map.merge(Utils.make_json_ld_header())
244 |> Map.merge(pagination)
245 end
246
247 defp maybe_put_total_items(map, false, _total), do: map
248
249 defp maybe_put_total_items(map, true, total) do
250 Map.put(map, "totalItems", total)
251 end
252
253 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
254 offset = (page - 1) * 10
255 items = Enum.slice(collection, offset, 10)
256 items = Enum.map(items, fn user -> user.ap_id end)
257 total = total || length(collection)
258
259 map = %{
260 "id" => "#{iri}?page=#{page}",
261 "type" => "OrderedCollectionPage",
262 "partOf" => iri,
263 "totalItems" => total,
264 "orderedItems" => if(show_items, do: items, else: [])
265 }
266
267 if offset < total do
268 Map.put(map, "next", "#{iri}?page=#{page + 1}")
269 else
270 map
271 end
272 end
273
274 defp maybe_make_image(func, key, user) do
275 if image = func.(user, no_default: true) do
276 %{
277 key => %{
278 "type" => "Image",
279 "url" => image
280 }
281 }
282 else
283 %{}
284 end
285 end
286 end