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
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.ActivityPub.Transmogrifier
12 alias Pleroma.Web.ActivityPub.Utils
13 alias Pleroma.Web.Endpoint
14 alias Pleroma.Web.Router.Helpers
15 alias Pleroma.Web.Salmon
16 alias Pleroma.Web.WebFinger
20 def render("endpoints.json", %{user: %User{nickname: nil, local: true} = _user}) do
21 %{"sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox)}
24 def render("endpoints.json", %{user: %User{local: true} = _user}) do
26 "oauthAuthorizationEndpoint" => Helpers.o_auth_url(Endpoint, :authorize),
27 "oauthRegistrationEndpoint" => Helpers.mastodon_api_url(Endpoint, :create_app),
28 "oauthTokenEndpoint" => Helpers.o_auth_url(Endpoint, :token_exchange),
29 "sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox)
33 def render("endpoints.json", _), do: %{}
35 # the instance itself is not a Person, but instead an Application
36 def render("user.json", %{user: %{nickname: nil} = user}) do
37 {:ok, user} = WebFinger.ensure_keys_present(user)
38 {:ok, _, public_key} = Salmon.keys_from_pem(user.info.keys)
39 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
40 public_key = :public_key.pem_encode([public_key])
42 endpoints = render("endpoints.json", %{user: user})
46 "type" => "Application",
47 "following" => "#{user.ap_id}/following",
48 "followers" => "#{user.ap_id}/followers",
49 "inbox" => "#{user.ap_id}/inbox",
51 "summary" => "Virtual actor for Pleroma relay",
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 def render("user.json", %{user: user}) do
65 {:ok, user} = WebFinger.ensure_keys_present(user)
66 {:ok, _, public_key} = Salmon.keys_from_pem(user.info.keys)
67 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
68 public_key = :public_key.pem_encode([public_key])
70 endpoints = render("endpoints.json", %{user: user})
74 |> Transmogrifier.add_emoji_tags()
80 "following" => "#{user.ap_id}/following",
81 "followers" => "#{user.ap_id}/followers",
82 "inbox" => "#{user.ap_id}/inbox",
83 "outbox" => "#{user.ap_id}/outbox",
84 "preferredUsername" => user.nickname,
86 "summary" => user.bio,
88 "manuallyApprovesFollowers" => user.info.locked,
90 "id" => "#{user.ap_id}#main-key",
91 "owner" => user.ap_id,
92 "publicKeyPem" => public_key
94 "endpoints" => endpoints,
95 "tag" => (user.info.source_data["tag"] || []) ++ user_tags
97 |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
98 |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
99 |> Map.merge(Utils.make_json_ld_header())
102 def render("following.json", %{user: user, page: page}) do
103 query = User.get_friends_query(user)
104 query = from(user in query, select: [:ap_id])
105 following = Repo.all(query)
108 if !user.info.hide_follows do
114 collection(following, "#{user.ap_id}/following", page, !user.info.hide_follows, total)
115 |> Map.merge(Utils.make_json_ld_header())
118 def render("following.json", %{user: user}) do
119 query = User.get_friends_query(user)
120 query = from(user in query, select: [:ap_id])
121 following = Repo.all(query)
124 if !user.info.hide_follows do
131 "id" => "#{user.ap_id}/following",
132 "type" => "OrderedCollection",
133 "totalItems" => total,
134 "first" => collection(following, "#{user.ap_id}/following", 1, !user.info.hide_follows)
136 |> Map.merge(Utils.make_json_ld_header())
139 def render("followers.json", %{user: user, page: page}) do
140 query = User.get_followers_query(user)
141 query = from(user in query, select: [:ap_id])
142 followers = Repo.all(query)
145 if !user.info.hide_followers do
151 collection(followers, "#{user.ap_id}/followers", page, !user.info.hide_followers, total)
152 |> Map.merge(Utils.make_json_ld_header())
155 def render("followers.json", %{user: user}) do
156 query = User.get_followers_query(user)
157 query = from(user in query, select: [:ap_id])
158 followers = Repo.all(query)
161 if !user.info.hide_followers do
168 "id" => "#{user.ap_id}/followers",
169 "type" => "OrderedCollection",
170 "totalItems" => total,
172 collection(followers, "#{user.ap_id}/followers", 1, !user.info.hide_followers, total)
174 |> Map.merge(Utils.make_json_ld_header())
177 def render("outbox.json", %{user: user, max_id: max_qid}) do
184 Map.put(params, "max_id", max_qid)
189 activities = ActivityPub.fetch_user_activities(user, nil, params)
191 {max_id, min_id, collection} =
192 if length(activities) > 0 do
194 Enum.at(Enum.reverse(activities), 0).id,
195 Enum.at(activities, 0).id,
196 Enum.map(activities, fn act ->
197 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
209 iri = "#{user.ap_id}/outbox"
212 "id" => "#{iri}?max_id=#{max_id}",
213 "type" => "OrderedCollectionPage",
215 "orderedItems" => collection,
216 "next" => "#{iri}?max_id=#{min_id}"
222 "type" => "OrderedCollection",
225 |> Map.merge(Utils.make_json_ld_header())
227 page |> Map.merge(Utils.make_json_ld_header())
231 def render("inbox.json", %{user: user, max_id: max_qid}) do
238 Map.put(params, "max_id", max_qid)
243 activities = ActivityPub.fetch_activities([user.ap_id | user.following], params)
245 min_id = Enum.at(Enum.reverse(activities), 0).id
246 max_id = Enum.at(activities, 0).id
249 Enum.map(activities, fn act ->
250 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
254 iri = "#{user.ap_id}/inbox"
257 "id" => "#{iri}?max_id=#{max_id}",
258 "type" => "OrderedCollectionPage",
260 "orderedItems" => collection,
261 "next" => "#{iri}?max_id=#{min_id}"
267 "type" => "OrderedCollection",
270 |> Map.merge(Utils.make_json_ld_header())
272 page |> Map.merge(Utils.make_json_ld_header())
276 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
277 offset = (page - 1) * 10
278 items = Enum.slice(collection, offset, 10)
279 items = Enum.map(items, fn user -> user.ap_id end)
280 total = total || length(collection)
283 "id" => "#{iri}?page=#{page}",
284 "type" => "OrderedCollectionPage",
286 "totalItems" => total,
287 "orderedItems" => if(show_items, do: items, else: [])
291 Map.put(map, "next", "#{iri}?page=#{page + 1}")
297 defp maybe_make_image(func, key, user) do
298 if image = func.(user, no_default: true) do