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 # the instance itself is not a Person, but instead an Application
35 def render("user.json", %{user: %{nickname: nil} = 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",
50 "summary" => "Virtual actor for Pleroma relay",
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 def render("user.json", %{user: user}) do
64 {:ok, user} = User.ensure_keys_present(user)
65 {:ok, _, public_key} = Keys.keys_from_pem(user.info.keys)
66 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
67 public_key = :public_key.pem_encode([public_key])
69 endpoints = render("endpoints.json", %{user: user})
73 |> Transmogrifier.add_emoji_tags()
79 "following" => "#{user.ap_id}/following",
80 "followers" => "#{user.ap_id}/followers",
81 "inbox" => "#{user.ap_id}/inbox",
82 "outbox" => "#{user.ap_id}/outbox",
83 "preferredUsername" => user.nickname,
85 "summary" => user.bio,
87 "manuallyApprovesFollowers" => user.info.locked,
89 "id" => "#{user.ap_id}#main-key",
90 "owner" => user.ap_id,
91 "publicKeyPem" => public_key
93 "endpoints" => endpoints,
94 "tag" => (user.info.source_data["tag"] || []) ++ user_tags
96 |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
97 |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
98 |> Map.merge(Utils.make_json_ld_header())
101 def render("following.json", %{user: user, page: page} = opts) do
102 showing = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
103 query = User.get_friends_query(user)
104 query = from(user in query, select: [:ap_id])
105 following = Repo.all(query)
114 collection(following, "#{user.ap_id}/following", page, showing, total)
115 |> Map.merge(Utils.make_json_ld_header())
118 def render("following.json", %{user: user} = opts) do
119 showing = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
120 query = User.get_friends_query(user)
121 query = from(user in query, select: [:ap_id])
122 following = Repo.all(query)
132 "id" => "#{user.ap_id}/following",
133 "type" => "OrderedCollection",
134 "totalItems" => total,
137 collection(following, "#{user.ap_id}/following", 1, !user.info.hide_follows)
139 "#{user.ap_id}/following?page=1"
142 |> Map.merge(Utils.make_json_ld_header())
145 def render("followers.json", %{user: user, page: page} = opts) do
146 showing = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
148 query = User.get_followers_query(user)
149 query = from(user in query, select: [:ap_id])
150 followers = Repo.all(query)
159 collection(followers, "#{user.ap_id}/followers", page, showing, total)
160 |> Map.merge(Utils.make_json_ld_header())
163 def render("followers.json", %{user: user} = opts) do
164 showing = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
166 query = User.get_followers_query(user)
167 query = from(user in query, select: [:ap_id])
168 followers = Repo.all(query)
178 "id" => "#{user.ap_id}/followers",
179 "type" => "OrderedCollection",
180 "totalItems" => total,
183 collection(followers, "#{user.ap_id}/followers", 1, showing, total)
185 "#{user.ap_id}/followers?page=1"
188 |> Map.merge(Utils.make_json_ld_header())
191 def render("outbox.json", %{user: user, max_id: max_qid}) do
198 Map.put(params, "max_id", max_qid)
203 activities = ActivityPub.fetch_user_activities(user, nil, params)
205 {max_id, min_id, collection} =
206 if length(activities) > 0 do
208 Enum.at(Enum.reverse(activities), 0).id,
209 Enum.at(activities, 0).id,
210 Enum.map(activities, fn act ->
211 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
223 iri = "#{user.ap_id}/outbox"
226 "id" => "#{iri}?max_id=#{max_id}",
227 "type" => "OrderedCollectionPage",
229 "orderedItems" => collection,
230 "next" => "#{iri}?max_id=#{min_id}"
236 "type" => "OrderedCollection",
239 |> Map.merge(Utils.make_json_ld_header())
241 page |> Map.merge(Utils.make_json_ld_header())
245 def render("inbox.json", %{user: user, max_id: max_qid}) do
252 Map.put(params, "max_id", max_qid)
257 activities = ActivityPub.fetch_activities([user.ap_id | user.following], params)
259 min_id = Enum.at(Enum.reverse(activities), 0).id
260 max_id = Enum.at(activities, 0).id
263 Enum.map(activities, fn act ->
264 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
268 iri = "#{user.ap_id}/inbox"
271 "id" => "#{iri}?max_id=#{max_id}",
272 "type" => "OrderedCollectionPage",
274 "orderedItems" => collection,
275 "next" => "#{iri}?max_id=#{min_id}"
281 "type" => "OrderedCollection",
284 |> Map.merge(Utils.make_json_ld_header())
286 page |> Map.merge(Utils.make_json_ld_header())
290 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
291 offset = (page - 1) * 10
292 items = Enum.slice(collection, offset, 10)
293 items = Enum.map(items, fn user -> user.ap_id end)
294 total = total || length(collection)
297 "id" => "#{iri}?page=#{page}",
298 "type" => "OrderedCollectionPage",
300 "totalItems" => total,
301 "orderedItems" => if(show_items, do: items, else: [])
305 Map.put(map, "next", "#{iri}?page=#{page + 1}")
311 defp maybe_make_image(func, key, user) do
312 if image = func.(user, no_default: true) do