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}) do
102 query = User.get_friends_query(user)
103 query = from(user in query, select: [:ap_id])
104 following = Repo.all(query)
107 if !user.info.hide_follows do
113 collection(following, "#{user.ap_id}/following", page, !user.info.hide_follows, total)
114 |> Map.merge(Utils.make_json_ld_header())
117 def render("following.json", %{user: user}) do
118 query = User.get_friends_query(user)
119 query = from(user in query, select: [:ap_id])
120 following = Repo.all(query)
123 if !user.info.hide_follows do
130 "id" => "#{user.ap_id}/following",
131 "type" => "OrderedCollection",
132 "totalItems" => total,
133 "first" => collection(following, "#{user.ap_id}/following", 1, !user.info.hide_follows)
135 |> Map.merge(Utils.make_json_ld_header())
138 def render("followers.json", %{user: user, page: page}) do
139 query = User.get_followers_query(user)
140 query = from(user in query, select: [:ap_id])
141 followers = Repo.all(query)
144 if !user.info.hide_followers do
150 collection(followers, "#{user.ap_id}/followers", page, !user.info.hide_followers, total)
151 |> Map.merge(Utils.make_json_ld_header())
154 def render("followers.json", %{user: user}) do
155 query = User.get_followers_query(user)
156 query = from(user in query, select: [:ap_id])
157 followers = Repo.all(query)
160 if !user.info.hide_followers do
167 "id" => "#{user.ap_id}/followers",
168 "type" => "OrderedCollection",
169 "totalItems" => total,
171 collection(followers, "#{user.ap_id}/followers", 1, !user.info.hide_followers, total)
173 |> Map.merge(Utils.make_json_ld_header())
176 def render("outbox.json", %{user: user, max_id: max_qid}) do
183 Map.put(params, "max_id", max_qid)
188 activities = ActivityPub.fetch_user_activities(user, nil, params)
190 {max_id, min_id, collection} =
191 if length(activities) > 0 do
193 Enum.at(Enum.reverse(activities), 0).id,
194 Enum.at(activities, 0).id,
195 Enum.map(activities, fn act ->
196 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
208 iri = "#{user.ap_id}/outbox"
211 "id" => "#{iri}?max_id=#{max_id}",
212 "type" => "OrderedCollectionPage",
214 "orderedItems" => collection,
215 "next" => "#{iri}?max_id=#{min_id}"
221 "type" => "OrderedCollection",
224 |> Map.merge(Utils.make_json_ld_header())
226 page |> Map.merge(Utils.make_json_ld_header())
230 def render("inbox.json", %{user: user, max_id: max_qid}) do
237 Map.put(params, "max_id", max_qid)
242 activities = ActivityPub.fetch_activities([user.ap_id | user.following], params)
244 min_id = Enum.at(Enum.reverse(activities), 0).id
245 max_id = Enum.at(activities, 0).id
248 Enum.map(activities, fn act ->
249 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
253 iri = "#{user.ap_id}/inbox"
256 "id" => "#{iri}?max_id=#{max_id}",
257 "type" => "OrderedCollectionPage",
259 "orderedItems" => collection,
260 "next" => "#{iri}?max_id=#{min_id}"
266 "type" => "OrderedCollection",
269 |> Map.merge(Utils.make_json_ld_header())
271 page |> Map.merge(Utils.make_json_ld_header())
275 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
276 offset = (page - 1) * 10
277 items = Enum.slice(collection, offset, 10)
278 items = Enum.map(items, fn user -> user.ap_id end)
279 total = total || length(collection)
282 "id" => "#{iri}?page=#{page}",
283 "type" => "OrderedCollectionPage",
285 "totalItems" => total,
286 "orderedItems" => if(show_items, do: items, else: [])
290 Map.put(map, "next", "#{iri}?page=#{page + 1}")
296 defp maybe_make_image(func, key, user) do
297 if image = func.(user, no_default: true) do