1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.UserView do
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
19 def render("endpoints.json", %{user: %User{nickname: nil, local: true} = _user}) do
20 %{"sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox)}
23 def render("endpoints.json", %{user: %User{local: true} = _user}) do
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 "uploadMedia" => Helpers.activity_pub_url(Endpoint, :upload_media)
33 def render("endpoints.json", _), do: %{}
35 def render("service.json", %{user: 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])
41 endpoints = render("endpoints.json", %{user: user})
45 "type" => "Application",
46 "following" => "#{user.ap_id}/following",
47 "followers" => "#{user.ap_id}/followers",
48 "inbox" => "#{user.ap_id}/inbox",
51 "An internal service actor for this Pleroma instance. No user-serviceable parts inside.",
53 "manuallyApprovesFollowers" => false,
55 "id" => "#{user.ap_id}#main-key",
56 "owner" => user.ap_id,
57 "publicKeyPem" => public_key
59 "endpoints" => endpoints
61 |> Map.merge(Utils.make_json_ld_header())
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})
68 def render("user.json", %{user: %User{nickname: "internal." <> _} = user}),
69 do: render("service.json", %{user: user}) |> Map.put("preferredUsername", user.nickname)
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.info.keys)
74 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
75 public_key = :public_key.pem_encode([public_key])
77 endpoints = render("endpoints.json", %{user: user})
81 |> Transmogrifier.add_emoji_tags()
87 |> Enum.map(fn %{"name" => name, "value" => value} ->
89 "name" => Pleroma.HTML.strip_tags(name),
90 "value" => Pleroma.HTML.filter_tags(value, Pleroma.HTML.Scrubber.LinksOnly)
93 |> Enum.map(&Map.put(&1, "type", "PropertyValue"))
98 "following" => "#{user.ap_id}/following",
99 "followers" => "#{user.ap_id}/followers",
100 "inbox" => "#{user.ap_id}/inbox",
101 "outbox" => "#{user.ap_id}/outbox",
102 "preferredUsername" => user.nickname,
104 "summary" => user.bio,
106 "manuallyApprovesFollowers" => user.info.locked,
108 "id" => "#{user.ap_id}#main-key",
109 "owner" => user.ap_id,
110 "publicKeyPem" => public_key
112 "endpoints" => endpoints,
113 "attachment" => fields,
114 "tag" => (user.info.source_data["tag"] || []) ++ user_tags
116 |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
117 |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
118 |> Map.merge(Utils.make_json_ld_header())
121 def render("following.json", %{user: user, page: page} = opts) do
122 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
123 showing_count = showing_items || !user.info.hide_follows_count
125 query = User.get_friends_query(user)
126 query = from(user in query, select: [:ap_id])
127 following = Repo.all(query)
136 collection(following, "#{user.ap_id}/following", page, showing_items, total)
137 |> Map.merge(Utils.make_json_ld_header())
140 def render("following.json", %{user: user} = opts) do
141 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
142 showing_count = showing_items || !user.info.hide_follows_count
144 query = User.get_friends_query(user)
145 query = from(user in query, select: [:ap_id])
146 following = Repo.all(query)
156 "id" => "#{user.ap_id}/following",
157 "type" => "OrderedCollection",
158 "totalItems" => total,
161 collection(following, "#{user.ap_id}/following", 1, !user.info.hide_follows)
163 "#{user.ap_id}/following?page=1"
166 |> Map.merge(Utils.make_json_ld_header())
169 def render("followers.json", %{user: user, page: page} = opts) do
170 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
171 showing_count = showing_items || !user.info.hide_followers_count
173 query = User.get_followers_query(user)
174 query = from(user in query, select: [:ap_id])
175 followers = Repo.all(query)
184 collection(followers, "#{user.ap_id}/followers", page, showing_items, total)
185 |> Map.merge(Utils.make_json_ld_header())
188 def render("followers.json", %{user: user} = opts) do
189 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
190 showing_count = showing_items || !user.info.hide_followers_count
192 query = User.get_followers_query(user)
193 query = from(user in query, select: [:ap_id])
194 followers = Repo.all(query)
204 "id" => "#{user.ap_id}/followers",
205 "type" => "OrderedCollection",
206 "totalItems" => total,
209 collection(followers, "#{user.ap_id}/followers", 1, showing_items, total)
211 "#{user.ap_id}/followers?page=1"
214 |> Map.merge(Utils.make_json_ld_header())
217 def render("outbox.json", %{user: user, max_id: max_qid}) do
224 Map.put(params, "max_id", max_qid)
229 activities = ActivityPub.fetch_user_activities(user, nil, params)
231 # this is sorted chronologically, so first activity is the newest (max)
232 {max_id, min_id, collection} =
233 if length(activities) > 0 do
235 Enum.at(activities, 0).id,
236 Enum.at(Enum.reverse(activities), 0).id,
237 Enum.map(activities, fn act ->
238 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
250 iri = "#{user.ap_id}/outbox"
253 "id" => "#{iri}?max_id=#{max_id}",
254 "type" => "OrderedCollectionPage",
256 "orderedItems" => collection,
257 "next" => "#{iri}?max_id=#{min_id}"
263 "type" => "OrderedCollection",
266 |> Map.merge(Utils.make_json_ld_header())
268 page |> Map.merge(Utils.make_json_ld_header())
272 def render("inbox.json", %{user: user, max_id: max_qid}) do
279 Map.put(params, "max_id", max_qid)
284 activities = ActivityPub.fetch_activities([user.ap_id | user.following], params)
286 min_id = Enum.at(Enum.reverse(activities), 0).id
287 max_id = Enum.at(activities, 0).id
290 Enum.map(activities, fn act ->
291 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
295 iri = "#{user.ap_id}/inbox"
298 "id" => "#{iri}?max_id=#{max_id}",
299 "type" => "OrderedCollectionPage",
301 "orderedItems" => collection,
302 "next" => "#{iri}?max_id=#{min_id}"
308 "type" => "OrderedCollection",
311 |> Map.merge(Utils.make_json_ld_header())
313 page |> Map.merge(Utils.make_json_ld_header())
317 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
318 offset = (page - 1) * 10
319 items = Enum.slice(collection, offset, 10)
320 items = Enum.map(items, fn user -> user.ap_id end)
321 total = total || length(collection)
324 "id" => "#{iri}?page=#{page}",
325 "type" => "OrderedCollectionPage",
327 "totalItems" => total,
328 "orderedItems" => if(show_items, do: items, else: [])
332 Map.put(map, "next", "#{iri}?page=#{page + 1}")
338 defp maybe_make_image(func, key, user) do
339 if image = func.(user, no_default: true) do