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)
32 def render("endpoints.json", _), do: %{}
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.info.keys)
37 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
38 public_key = :public_key.pem_encode([public_key])
40 endpoints = render("endpoints.json", %{user: user})
44 "type" => "Application",
45 "following" => "#{user.ap_id}/following",
46 "followers" => "#{user.ap_id}/followers",
47 "inbox" => "#{user.ap_id}/inbox",
50 "An internal service actor for this Pleroma instance. No user-serviceable parts inside.",
52 "manuallyApprovesFollowers" => false,
54 "id" => "#{user.ap_id}#main-key",
55 "owner" => user.ap_id,
56 "publicKeyPem" => public_key
58 "endpoints" => endpoints
60 |> Map.merge(Utils.make_json_ld_header())
63 # the instance itself is not a Person, but instead an Application
64 def render("user.json", %{user: %User{nickname: nil} = user}),
65 do: render("service.json", %{user: user})
67 def render("user.json", %{user: %User{nickname: "internal." <> _} = user}),
68 do: render("service.json", %{user: user})
70 def render("user.json", %{user: user}) do
71 {:ok, user} = User.ensure_keys_present(user)
72 {:ok, _, public_key} = Keys.keys_from_pem(user.info.keys)
73 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
74 public_key = :public_key.pem_encode([public_key])
76 endpoints = render("endpoints.json", %{user: user})
80 |> Transmogrifier.add_emoji_tags()
86 "following" => "#{user.ap_id}/following",
87 "followers" => "#{user.ap_id}/followers",
88 "inbox" => "#{user.ap_id}/inbox",
89 "outbox" => "#{user.ap_id}/outbox",
90 "preferredUsername" => user.nickname,
92 "summary" => user.bio,
94 "manuallyApprovesFollowers" => user.info.locked,
96 "id" => "#{user.ap_id}#main-key",
97 "owner" => user.ap_id,
98 "publicKeyPem" => public_key
100 "endpoints" => endpoints,
101 "tag" => (user.info.source_data["tag"] || []) ++ user_tags
103 |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
104 |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
105 |> Map.merge(Utils.make_json_ld_header())
108 def render("following.json", %{user: user, page: page} = opts) do
109 showing = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
110 query = User.get_friends_query(user)
111 query = from(user in query, select: [:ap_id])
112 following = Repo.all(query)
121 collection(following, "#{user.ap_id}/following", page, showing, total)
122 |> Map.merge(Utils.make_json_ld_header())
125 def render("following.json", %{user: user} = opts) do
126 showing = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
127 query = User.get_friends_query(user)
128 query = from(user in query, select: [:ap_id])
129 following = Repo.all(query)
139 "id" => "#{user.ap_id}/following",
140 "type" => "OrderedCollection",
141 "totalItems" => total,
144 collection(following, "#{user.ap_id}/following", 1, !user.info.hide_follows)
146 "#{user.ap_id}/following?page=1"
149 |> Map.merge(Utils.make_json_ld_header())
152 def render("followers.json", %{user: user, page: page} = opts) do
153 showing = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
155 query = User.get_followers_query(user)
156 query = from(user in query, select: [:ap_id])
157 followers = Repo.all(query)
166 collection(followers, "#{user.ap_id}/followers", page, showing, total)
167 |> Map.merge(Utils.make_json_ld_header())
170 def render("followers.json", %{user: user} = opts) do
171 showing = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
173 query = User.get_followers_query(user)
174 query = from(user in query, select: [:ap_id])
175 followers = Repo.all(query)
185 "id" => "#{user.ap_id}/followers",
186 "type" => "OrderedCollection",
187 "totalItems" => total,
190 collection(followers, "#{user.ap_id}/followers", 1, showing, total)
192 "#{user.ap_id}/followers?page=1"
195 |> Map.merge(Utils.make_json_ld_header())
198 def render("outbox.json", %{user: user, max_id: max_qid}) do
205 Map.put(params, "max_id", max_qid)
210 activities = ActivityPub.fetch_user_activities(user, nil, params)
212 {max_id, min_id, collection} =
213 if length(activities) > 0 do
215 Enum.at(Enum.reverse(activities), 0).id,
216 Enum.at(activities, 0).id,
217 Enum.map(activities, fn act ->
218 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
230 iri = "#{user.ap_id}/outbox"
233 "id" => "#{iri}?max_id=#{max_id}",
234 "type" => "OrderedCollectionPage",
236 "orderedItems" => collection,
237 "next" => "#{iri}?max_id=#{min_id}"
243 "type" => "OrderedCollection",
246 |> Map.merge(Utils.make_json_ld_header())
248 page |> Map.merge(Utils.make_json_ld_header())
252 def render("inbox.json", %{user: user, max_id: max_qid}) do
259 Map.put(params, "max_id", max_qid)
264 activities = ActivityPub.fetch_activities([user.ap_id | user.following], params)
266 min_id = Enum.at(Enum.reverse(activities), 0).id
267 max_id = Enum.at(activities, 0).id
270 Enum.map(activities, fn act ->
271 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
275 iri = "#{user.ap_id}/inbox"
278 "id" => "#{iri}?max_id=#{max_id}",
279 "type" => "OrderedCollectionPage",
281 "orderedItems" => collection,
282 "next" => "#{iri}?max_id=#{min_id}"
288 "type" => "OrderedCollection",
291 |> Map.merge(Utils.make_json_ld_header())
293 page |> Map.merge(Utils.make_json_ld_header())
297 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
298 offset = (page - 1) * 10
299 items = Enum.slice(collection, offset, 10)
300 items = Enum.map(items, fn user -> user.ap_id end)
301 total = total || length(collection)
304 "id" => "#{iri}?page=#{page}",
305 "type" => "OrderedCollectionPage",
307 "totalItems" => total,
308 "orderedItems" => if(show_items, do: items, else: [])
312 Map.put(map, "next", "#{iri}?page=#{page + 1}")
318 defp maybe_make_image(func, key, user) do
319 if image = func.(user, no_default: true) do