Merge branch 'hex-flake-id' 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.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 }
111 |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
112 |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
113 |> Map.merge(Utils.make_json_ld_header())
114 end
115
116 def render("following.json", %{user: user, page: page} = opts) do
117 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
118 showing_count = showing_items || !user.info.hide_follows_count
119
120 query = User.get_friends_query(user)
121 query = from(user in query, select: [:ap_id])
122 following = Repo.all(query)
123
124 total =
125 if showing_count do
126 length(following)
127 else
128 0
129 end
130
131 collection(following, "#{user.ap_id}/following", page, showing_items, total)
132 |> Map.merge(Utils.make_json_ld_header())
133 end
134
135 def render("following.json", %{user: user} = opts) do
136 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
137 showing_count = showing_items || !user.info.hide_follows_count
138
139 query = User.get_friends_query(user)
140 query = from(user in query, select: [:ap_id])
141 following = Repo.all(query)
142
143 total =
144 if showing_count do
145 length(following)
146 else
147 0
148 end
149
150 %{
151 "id" => "#{user.ap_id}/following",
152 "type" => "OrderedCollection",
153 "totalItems" => total,
154 "first" =>
155 if showing_items do
156 collection(following, "#{user.ap_id}/following", 1, !user.info.hide_follows)
157 else
158 "#{user.ap_id}/following?page=1"
159 end
160 }
161 |> Map.merge(Utils.make_json_ld_header())
162 end
163
164 def render("followers.json", %{user: user, page: page} = opts) do
165 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
166 showing_count = showing_items || !user.info.hide_followers_count
167
168 query = User.get_followers_query(user)
169 query = from(user in query, select: [:ap_id])
170 followers = Repo.all(query)
171
172 total =
173 if showing_count do
174 length(followers)
175 else
176 0
177 end
178
179 collection(followers, "#{user.ap_id}/followers", page, showing_items, total)
180 |> Map.merge(Utils.make_json_ld_header())
181 end
182
183 def render("followers.json", %{user: user} = opts) do
184 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
185 showing_count = showing_items || !user.info.hide_followers_count
186
187 query = User.get_followers_query(user)
188 query = from(user in query, select: [:ap_id])
189 followers = Repo.all(query)
190
191 total =
192 if showing_count do
193 length(followers)
194 else
195 0
196 end
197
198 %{
199 "id" => "#{user.ap_id}/followers",
200 "type" => "OrderedCollection",
201 "totalItems" => total,
202 "first" =>
203 if showing_items do
204 collection(followers, "#{user.ap_id}/followers", 1, showing_items, total)
205 else
206 "#{user.ap_id}/followers?page=1"
207 end
208 }
209 |> Map.merge(Utils.make_json_ld_header())
210 end
211
212 def render("activity_collection.json", %{iri: iri}) do
213 %{
214 "id" => iri,
215 "type" => "OrderedCollection",
216 "first" => "#{iri}?page=true"
217 }
218 |> Map.merge(Utils.make_json_ld_header())
219 end
220
221 def render("activity_collection_page.json", %{activities: activities, iri: iri}) do
222 # this is sorted chronologically, so first activity is the newest (max)
223 {max_id, min_id, collection} =
224 if length(activities) > 0 do
225 {
226 Enum.at(activities, 0).id,
227 Enum.at(Enum.reverse(activities), 0).id,
228 Enum.map(activities, fn act ->
229 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
230 data
231 end)
232 }
233 else
234 {
235 0,
236 0,
237 []
238 }
239 end
240
241 %{
242 "id" => "#{iri}?max_id=#{max_id}&page=true",
243 "type" => "OrderedCollectionPage",
244 "partOf" => iri,
245 "orderedItems" => collection,
246 "next" => "#{iri}?max_id=#{min_id}&page=true"
247 }
248 |> Map.merge(Utils.make_json_ld_header())
249 end
250
251 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
252 offset = (page - 1) * 10
253 items = Enum.slice(collection, offset, 10)
254 items = Enum.map(items, fn user -> user.ap_id end)
255 total = total || length(collection)
256
257 map = %{
258 "id" => "#{iri}?page=#{page}",
259 "type" => "OrderedCollectionPage",
260 "partOf" => iri,
261 "totalItems" => total,
262 "orderedItems" => if(show_items, do: items, else: [])
263 }
264
265 if offset < total do
266 Map.put(map, "next", "#{iri}?page=#{page + 1}")
267 else
268 map
269 end
270 end
271
272 defp maybe_make_image(func, key, user) do
273 if image = func.(user, no_default: true) do
274 %{
275 key => %{
276 "type" => "Image",
277 "url" => image
278 }
279 }
280 else
281 %{}
282 end
283 end
284 end