Revert "add _discoverable_ keyword into ActivityPub @context"
[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.mastodon_api_url(Endpoint, :create_app),
26 "oauthTokenEndpoint" => Helpers.o_auth_url(Endpoint, :token_exchange),
27 "sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox)
28 }
29 end
30
31 def render("endpoints.json", _), do: %{}
32
33 def render("service.json", %{user: user}) do
34 {:ok, user} = User.ensure_keys_present(user)
35 {:ok, _, public_key} = Keys.keys_from_pem(user.info.keys)
36 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
37 public_key = :public_key.pem_encode([public_key])
38
39 endpoints = render("endpoints.json", %{user: user})
40
41 %{
42 "id" => user.ap_id,
43 "type" => "Application",
44 "following" => "#{user.ap_id}/following",
45 "followers" => "#{user.ap_id}/followers",
46 "inbox" => "#{user.ap_id}/inbox",
47 "name" => "Pleroma",
48 "summary" =>
49 "An internal service actor for this Pleroma instance. No user-serviceable parts inside.",
50 "url" => user.ap_id,
51 "manuallyApprovesFollowers" => false,
52 "publicKey" => %{
53 "id" => "#{user.ap_id}#main-key",
54 "owner" => user.ap_id,
55 "publicKeyPem" => public_key
56 },
57 "endpoints" => endpoints
58 }
59 |> Map.merge(Utils.make_json_ld_header())
60 end
61
62 # the instance itself is not a Person, but instead an Application
63 def render("user.json", %{user: %User{nickname: nil} = user}),
64 do: render("service.json", %{user: user})
65
66 def render("user.json", %{user: %User{nickname: "internal." <> _} = user}),
67 do: render("service.json", %{user: user}) |> Map.put("preferredUsername", user.nickname)
68
69 def render("user.json", %{user: user}) do
70 {:ok, user} = User.ensure_keys_present(user)
71 {:ok, _, public_key} = Keys.keys_from_pem(user.info.keys)
72 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
73 public_key = :public_key.pem_encode([public_key])
74
75 endpoints = render("endpoints.json", %{user: user})
76
77 emoji_tags = Transmogrifier.take_emoji_tags(user)
78
79 fields =
80 user.info
81 |> User.Info.fields()
82 |> Enum.map(fn %{"name" => name, "value" => value} ->
83 %{
84 "name" => Pleroma.HTML.strip_tags(name),
85 "value" => Pleroma.HTML.filter_tags(value, Pleroma.HTML.Scrubber.LinksOnly)
86 }
87 end)
88 |> Enum.map(&Map.put(&1, "type", "PropertyValue"))
89
90 %{
91 "id" => user.ap_id,
92 "type" => "Person",
93 "following" => "#{user.ap_id}/following",
94 "followers" => "#{user.ap_id}/followers",
95 "inbox" => "#{user.ap_id}/inbox",
96 "outbox" => "#{user.ap_id}/outbox",
97 "preferredUsername" => user.nickname,
98 "name" => user.name,
99 "summary" => user.bio,
100 "url" => user.ap_id,
101 "manuallyApprovesFollowers" => user.info.locked,
102 "publicKey" => %{
103 "id" => "#{user.ap_id}#main-key",
104 "owner" => user.ap_id,
105 "publicKeyPem" => public_key
106 },
107 "endpoints" => endpoints,
108 "attachment" => fields,
109 "tag" => (user.info.source_data["tag"] || []) ++ emoji_tags,
110 "discoverable" => user.info.discoverable
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("activity_collection.json", %{iri: iri}) do
214 %{
215 "id" => iri,
216 "type" => "OrderedCollection",
217 "first" => "#{iri}?page=true"
218 }
219 |> Map.merge(Utils.make_json_ld_header())
220 end
221
222 def render("activity_collection_page.json", %{activities: activities, iri: iri}) do
223 # this is sorted chronologically, so first activity is the newest (max)
224 {max_id, min_id, collection} =
225 if length(activities) > 0 do
226 {
227 Enum.at(activities, 0).id,
228 Enum.at(Enum.reverse(activities), 0).id,
229 Enum.map(activities, fn act ->
230 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
231 data
232 end)
233 }
234 else
235 {
236 0,
237 0,
238 []
239 }
240 end
241
242 %{
243 "id" => "#{iri}?max_id=#{max_id}&page=true",
244 "type" => "OrderedCollectionPage",
245 "partOf" => iri,
246 "orderedItems" => collection,
247 "next" => "#{iri}?max_id=#{min_id}&page=true"
248 }
249 |> Map.merge(Utils.make_json_ld_header())
250 end
251
252 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
253 offset = (page - 1) * 10
254 items = Enum.slice(collection, offset, 10)
255 items = Enum.map(items, fn user -> user.ap_id end)
256 total = total || length(collection)
257
258 map = %{
259 "id" => "#{iri}?page=#{page}",
260 "type" => "OrderedCollectionPage",
261 "partOf" => iri,
262 "totalItems" => total,
263 "orderedItems" => if(show_items, do: items, else: [])
264 }
265
266 if offset < total do
267 Map.put(map, "next", "#{iri}?page=#{page + 1}")
268 else
269 map
270 end
271 end
272
273 defp maybe_make_image(func, key, user) do
274 if image = func.(user, no_default: true) do
275 %{
276 key => %{
277 "type" => "Image",
278 "url" => image
279 }
280 }
281 else
282 %{}
283 end
284 end
285 end