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})
75 "following" => "#{user.ap_id}/following",
76 "followers" => "#{user.ap_id}/followers",
77 "inbox" => "#{user.ap_id}/inbox",
78 "outbox" => "#{user.ap_id}/outbox",
79 "preferredUsername" => user.nickname,
81 "summary" => user.bio,
83 "manuallyApprovesFollowers" => user.info.locked,
85 "id" => "#{user.ap_id}#main-key",
86 "owner" => user.ap_id,
87 "publicKeyPem" => public_key
89 "endpoints" => endpoints,
90 "tag" => user.info.source_data["tag"] || []
92 |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
93 |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
94 |> Map.merge(Utils.make_json_ld_header())
97 def render("following.json", %{user: user, page: page}) do
98 query = User.get_friends_query(user)
99 query = from(user in query, select: [:ap_id])
100 following = Repo.all(query)
103 if !user.info.hide_follows do
109 collection(following, "#{user.ap_id}/following", page, !user.info.hide_follows, total)
110 |> Map.merge(Utils.make_json_ld_header())
113 def render("following.json", %{user: user}) do
114 query = User.get_friends_query(user)
115 query = from(user in query, select: [:ap_id])
116 following = Repo.all(query)
119 if !user.info.hide_follows do
126 "id" => "#{user.ap_id}/following",
127 "type" => "OrderedCollection",
128 "totalItems" => total,
129 "first" => collection(following, "#{user.ap_id}/following", 1, !user.info.hide_follows)
131 |> Map.merge(Utils.make_json_ld_header())
134 def render("followers.json", %{user: user, page: page}) do
135 query = User.get_followers_query(user)
136 query = from(user in query, select: [:ap_id])
137 followers = Repo.all(query)
140 if !user.info.hide_followers do
146 collection(followers, "#{user.ap_id}/followers", page, !user.info.hide_followers, total)
147 |> Map.merge(Utils.make_json_ld_header())
150 def render("followers.json", %{user: user}) do
151 query = User.get_followers_query(user)
152 query = from(user in query, select: [:ap_id])
153 followers = Repo.all(query)
156 if !user.info.hide_followers do
163 "id" => "#{user.ap_id}/followers",
164 "type" => "OrderedCollection",
165 "totalItems" => total,
167 collection(followers, "#{user.ap_id}/followers", 1, !user.info.hide_followers, total)
169 |> Map.merge(Utils.make_json_ld_header())
172 def render("outbox.json", %{user: user, max_id: max_qid}) do
179 Map.put(params, "max_id", max_qid)
184 activities = ActivityPub.fetch_user_activities(user, nil, params)
186 {max_id, min_id, collection} =
187 if length(activities) > 0 do
189 Enum.at(Enum.reverse(activities), 0).id,
190 Enum.at(activities, 0).id,
191 Enum.map(activities, fn act ->
192 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
204 iri = "#{user.ap_id}/outbox"
207 "id" => "#{iri}?max_id=#{max_id}",
208 "type" => "OrderedCollectionPage",
210 "orderedItems" => collection,
211 "next" => "#{iri}?max_id=#{min_id}"
217 "type" => "OrderedCollection",
220 |> Map.merge(Utils.make_json_ld_header())
222 page |> Map.merge(Utils.make_json_ld_header())
226 def render("inbox.json", %{user: user, max_id: max_qid}) do
233 Map.put(params, "max_id", max_qid)
238 activities = ActivityPub.fetch_activities([user.ap_id | user.following], params)
240 min_id = Enum.at(Enum.reverse(activities), 0).id
241 max_id = Enum.at(activities, 0).id
244 Enum.map(activities, fn act ->
245 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
249 iri = "#{user.ap_id}/inbox"
252 "id" => "#{iri}?max_id=#{max_id}",
253 "type" => "OrderedCollectionPage",
255 "orderedItems" => collection,
256 "next" => "#{iri}?max_id=#{min_id}"
262 "type" => "OrderedCollection",
265 |> Map.merge(Utils.make_json_ld_header())
267 page |> Map.merge(Utils.make_json_ld_header())
271 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
272 offset = (page - 1) * 10
273 items = Enum.slice(collection, offset, 10)
274 items = Enum.map(items, fn user -> user.ap_id end)
275 total = total || length(collection)
278 "id" => "#{iri}?page=#{page}",
279 "type" => "OrderedCollectionPage",
281 "totalItems" => total,
282 "orderedItems" => if(show_items, do: items, else: [])
286 Map.put(map, "next", "#{iri}?page=#{page + 1}")
292 defp maybe_make_image(func, key, user) do
293 if image = func.(user, no_default: true) do