rework to use properties instead of compound typing, per SocialCG
[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.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
77 endpoints = render("endpoints.json", %{user: user})
78
79 emoji_tags = Transmogrifier.take_emoji_tags(user)
80
81 fields =
82 user.info
83 |> User.Info.fields()
84 |> Enum.map(fn %{"name" => name, "value" => value} ->
85 %{
86 "name" => Pleroma.HTML.strip_tags(name),
87 "value" => Pleroma.HTML.filter_tags(value, Pleroma.HTML.Scrubber.LinksOnly)
88 }
89 end)
90 |> Enum.map(&Map.put(&1, "type", "PropertyValue"))
91
92 %{
93 "id" => user.ap_id,
94 "type" => "Person",
95 "following" => "#{user.ap_id}/following",
96 "followers" => "#{user.ap_id}/followers",
97 "inbox" => "#{user.ap_id}/inbox",
98 "outbox" => "#{user.ap_id}/outbox",
99 "preferredUsername" => user.nickname,
100 "name" => user.name,
101 "summary" => user.bio,
102 "url" => user.ap_id,
103 "manuallyApprovesFollowers" => user.info.locked,
104 "publicKey" => %{
105 "id" => "#{user.ap_id}#main-key",
106 "owner" => user.ap_id,
107 "publicKeyPem" => public_key
108 },
109 "endpoints" => endpoints,
110 "attachment" => fields,
111 "tag" => (user.info.source_data["tag"] || []) ++ emoji_tags,
112 "discoverable" => user.info.discoverable
113 }
114 |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
115 |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
116 |> Map.merge(Utils.make_json_ld_header())
117 end
118
119 def render("following.json", %{user: user, page: page} = opts) do
120 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
121 showing_count = showing_items || !user.info.hide_follows_count
122
123 query = User.get_friends_query(user)
124 query = from(user in query, select: [:ap_id])
125 following = Repo.all(query)
126
127 total =
128 if showing_count do
129 length(following)
130 else
131 0
132 end
133
134 collection(following, "#{user.ap_id}/following", page, showing_items, total)
135 |> Map.merge(Utils.make_json_ld_header())
136 end
137
138 def render("following.json", %{user: user} = opts) do
139 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
140 showing_count = showing_items || !user.info.hide_follows_count
141
142 query = User.get_friends_query(user)
143 query = from(user in query, select: [:ap_id])
144 following = Repo.all(query)
145
146 total =
147 if showing_count do
148 length(following)
149 else
150 0
151 end
152
153 %{
154 "id" => "#{user.ap_id}/following",
155 "type" => "OrderedCollection",
156 "totalItems" => total,
157 "first" =>
158 if showing_items do
159 collection(following, "#{user.ap_id}/following", 1, !user.info.hide_follows)
160 else
161 "#{user.ap_id}/following?page=1"
162 end
163 }
164 |> Map.merge(Utils.make_json_ld_header())
165 end
166
167 def render("followers.json", %{user: user, page: page} = opts) do
168 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
169 showing_count = showing_items || !user.info.hide_followers_count
170
171 query = User.get_followers_query(user)
172 query = from(user in query, select: [:ap_id])
173 followers = Repo.all(query)
174
175 total =
176 if showing_count do
177 length(followers)
178 else
179 0
180 end
181
182 collection(followers, "#{user.ap_id}/followers", page, showing_items, total)
183 |> Map.merge(Utils.make_json_ld_header())
184 end
185
186 def render("followers.json", %{user: user} = opts) do
187 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
188 showing_count = showing_items || !user.info.hide_followers_count
189
190 query = User.get_followers_query(user)
191 query = from(user in query, select: [:ap_id])
192 followers = Repo.all(query)
193
194 total =
195 if showing_count do
196 length(followers)
197 else
198 0
199 end
200
201 %{
202 "id" => "#{user.ap_id}/followers",
203 "type" => "OrderedCollection",
204 "totalItems" => total,
205 "first" =>
206 if showing_items do
207 collection(followers, "#{user.ap_id}/followers", 1, showing_items, total)
208 else
209 "#{user.ap_id}/followers?page=1"
210 end
211 }
212 |> Map.merge(Utils.make_json_ld_header())
213 end
214
215 def render("activity_collection.json", %{iri: iri}) do
216 %{
217 "id" => iri,
218 "type" => "OrderedCollection",
219 "first" => "#{iri}?page=true"
220 }
221 |> Map.merge(Utils.make_json_ld_header())
222 end
223
224 def render("activity_collection_page.json", %{activities: activities, iri: iri}) do
225 # this is sorted chronologically, so first activity is the newest (max)
226 {max_id, min_id, collection} =
227 if length(activities) > 0 do
228 {
229 Enum.at(activities, 0).id,
230 Enum.at(Enum.reverse(activities), 0).id,
231 Enum.map(activities, fn act ->
232 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
233 data
234 end)
235 }
236 else
237 {
238 0,
239 0,
240 []
241 }
242 end
243
244 %{
245 "id" => "#{iri}?max_id=#{max_id}&page=true",
246 "type" => "OrderedCollectionPage",
247 "partOf" => iri,
248 "orderedItems" => collection,
249 "next" => "#{iri}?max_id=#{min_id}&page=true"
250 }
251 |> Map.merge(Utils.make_json_ld_header())
252 end
253
254 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
255 offset = (page - 1) * 10
256 items = Enum.slice(collection, offset, 10)
257 items = Enum.map(items, fn user -> user.ap_id end)
258 total = total || length(collection)
259
260 map = %{
261 "id" => "#{iri}?page=#{page}",
262 "type" => "OrderedCollectionPage",
263 "partOf" => iri,
264 "totalItems" => total,
265 "orderedItems" => if(show_items, do: items, else: [])
266 }
267
268 if offset < total do
269 Map.put(map, "next", "#{iri}?page=#{page + 1}")
270 else
271 map
272 end
273 end
274
275 defp maybe_make_image(func, key, user) do
276 if image = func.(user, no_default: true) do
277 %{
278 key => %{
279 "type" => "Image",
280 "url" => image
281 }
282 }
283 else
284 %{}
285 end
286 end
287 end