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(if user.nickname == nil do %{} else %{ "preferredUsername" => user.nickname})
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})
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 "following" => "#{user.ap_id}/following",
88 "followers" => "#{user.ap_id}/followers",
89 "inbox" => "#{user.ap_id}/inbox",
90 "outbox" => "#{user.ap_id}/outbox",
91 "preferredUsername" => user.nickname,
93 "summary" => user.bio,
95 "manuallyApprovesFollowers" => user.info.locked,
97 "id" => "#{user.ap_id}#main-key",
98 "owner" => user.ap_id,
99 "publicKeyPem" => public_key
101 "endpoints" => endpoints,
102 "tag" => (user.info.source_data["tag"] || []) ++ user_tags
104 |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
105 |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
106 |> Map.merge(Utils.make_json_ld_header())
109 def render("following.json", %{user: user, page: page} = opts) do
110 showing = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
111 query = User.get_friends_query(user)
112 query = from(user in query, select: [:ap_id])
113 following = Repo.all(query)
122 collection(following, "#{user.ap_id}/following", page, showing, total)
123 |> Map.merge(Utils.make_json_ld_header())
126 def render("following.json", %{user: user} = opts) do
127 showing = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
128 query = User.get_friends_query(user)
129 query = from(user in query, select: [:ap_id])
130 following = Repo.all(query)
140 "id" => "#{user.ap_id}/following",
141 "type" => "OrderedCollection",
142 "totalItems" => total,
145 collection(following, "#{user.ap_id}/following", 1, !user.info.hide_follows)
147 "#{user.ap_id}/following?page=1"
150 |> Map.merge(Utils.make_json_ld_header())
153 def render("followers.json", %{user: user, page: page} = opts) do
154 showing = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
156 query = User.get_followers_query(user)
157 query = from(user in query, select: [:ap_id])
158 followers = Repo.all(query)
167 collection(followers, "#{user.ap_id}/followers", page, showing, total)
168 |> Map.merge(Utils.make_json_ld_header())
171 def render("followers.json", %{user: user} = opts) do
172 showing = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
174 query = User.get_followers_query(user)
175 query = from(user in query, select: [:ap_id])
176 followers = Repo.all(query)
186 "id" => "#{user.ap_id}/followers",
187 "type" => "OrderedCollection",
188 "totalItems" => total,
191 collection(followers, "#{user.ap_id}/followers", 1, showing, total)
193 "#{user.ap_id}/followers?page=1"
196 |> Map.merge(Utils.make_json_ld_header())
199 def render("outbox.json", %{user: user, max_id: max_qid}) do
206 Map.put(params, "max_id", max_qid)
211 activities = ActivityPub.fetch_user_activities(user, nil, params)
213 {max_id, min_id, collection} =
214 if length(activities) > 0 do
216 Enum.at(Enum.reverse(activities), 0).id,
217 Enum.at(activities, 0).id,
218 Enum.map(activities, fn act ->
219 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
231 iri = "#{user.ap_id}/outbox"
234 "id" => "#{iri}?max_id=#{max_id}",
235 "type" => "OrderedCollectionPage",
237 "orderedItems" => collection,
238 "next" => "#{iri}?max_id=#{min_id}"
244 "type" => "OrderedCollection",
247 |> Map.merge(Utils.make_json_ld_header())
249 page |> Map.merge(Utils.make_json_ld_header())
253 def render("inbox.json", %{user: user, max_id: max_qid}) do
260 Map.put(params, "max_id", max_qid)
265 activities = ActivityPub.fetch_activities([user.ap_id | user.following], params)
267 min_id = Enum.at(Enum.reverse(activities), 0).id
268 max_id = Enum.at(activities, 0).id
271 Enum.map(activities, fn act ->
272 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
276 iri = "#{user.ap_id}/inbox"
279 "id" => "#{iri}?max_id=#{max_id}",
280 "type" => "OrderedCollectionPage",
282 "orderedItems" => collection,
283 "next" => "#{iri}?max_id=#{min_id}"
289 "type" => "OrderedCollection",
292 |> Map.merge(Utils.make_json_ld_header())
294 page |> Map.merge(Utils.make_json_ld_header())
298 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
299 offset = (page - 1) * 10
300 items = Enum.slice(collection, offset, 10)
301 items = Enum.map(items, fn user -> user.ap_id end)
302 total = total || length(collection)
305 "id" => "#{iri}?page=#{page}",
306 "type" => "OrderedCollectionPage",
308 "totalItems" => total,
309 "orderedItems" => if(show_items, do: items, else: [])
313 Map.put(map, "next", "#{iri}?page=#{page + 1}")
319 defp maybe_make_image(func, key, user) do
320 if image = func.(user, no_default: true) do