1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.Transmogrifier
12 alias Pleroma.Web.ActivityPub.Utils
13 alias Pleroma.Web.Endpoint
14 alias Pleroma.Web.Router.Helpers
18 def render("endpoints.json", %{user: %User{nickname: nil, local: true} = _user}) do
19 %{"sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox)}
22 def render("endpoints.json", %{user: %User{local: true} = _user}) do
24 "oauthAuthorizationEndpoint" => Helpers.o_auth_url(Endpoint, :authorize),
25 "oauthRegistrationEndpoint" => Helpers.app_url(Endpoint, :create),
26 "oauthTokenEndpoint" => Helpers.o_auth_url(Endpoint, :token_exchange),
27 "sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox),
28 "uploadMedia" => Helpers.activity_pub_url(Endpoint, :upload_media)
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.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,
59 "invisible" => User.invisible?(user)
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}) |> Map.put("preferredUsername", user.nickname)
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.keys)
74 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
75 public_key = :public_key.pem_encode([public_key])
76 user = User.sanitize_html(user)
78 endpoints = render("endpoints.json", %{user: user})
80 emoji_tags = Transmogrifier.take_emoji_tags(user)
82 fields = Enum.map(user.fields, &Map.put(&1, "type", "PropertyValue"))
84 chat_message_acceptance =
85 if is_boolean(user.accepts_chat_messages) do
87 "acceptsChatMessages" => user.accepts_chat_messages
95 "type" => user.actor_type,
96 "following" => "#{user.ap_id}/following",
97 "followers" => "#{user.ap_id}/followers",
98 "inbox" => "#{user.ap_id}/inbox",
99 "outbox" => "#{user.ap_id}/outbox",
100 "preferredUsername" => user.nickname,
102 "summary" => user.bio,
104 "manuallyApprovesFollowers" => user.locked,
106 "id" => "#{user.ap_id}#main-key",
107 "owner" => user.ap_id,
108 "publicKeyPem" => public_key
110 "endpoints" => endpoints,
111 "attachment" => fields,
113 "discoverable" => user.discoverable
115 |> Map.merge(chat_message_acceptance)
116 |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
117 |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
118 |> Map.merge(Utils.make_json_ld_header())
121 def render("following.json", %{user: user, page: page} = opts) do
122 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_follows
123 showing_count = showing_items || !user.hide_follows_count
125 query = User.get_friends_query(user)
126 query = from(user in query, select: [:ap_id])
127 following = Repo.all(query)
136 collection(following, "#{user.ap_id}/following", page, showing_items, total)
137 |> Map.merge(Utils.make_json_ld_header())
140 def render("following.json", %{user: user} = opts) do
141 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_follows
142 showing_count = showing_items || !user.hide_follows_count
144 query = User.get_friends_query(user)
145 query = from(user in query, select: [:ap_id])
146 following = Repo.all(query)
156 "id" => "#{user.ap_id}/following",
157 "type" => "OrderedCollection",
158 "totalItems" => total,
161 collection(following, "#{user.ap_id}/following", 1, !user.hide_follows)
163 "#{user.ap_id}/following?page=1"
166 |> Map.merge(Utils.make_json_ld_header())
169 def render("followers.json", %{user: user, page: page} = opts) do
170 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_followers
171 showing_count = showing_items || !user.hide_followers_count
173 query = User.get_followers_query(user)
174 query = from(user in query, select: [:ap_id])
175 followers = Repo.all(query)
184 collection(followers, "#{user.ap_id}/followers", page, showing_items, total)
185 |> Map.merge(Utils.make_json_ld_header())
188 def render("followers.json", %{user: user} = opts) do
189 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_followers
190 showing_count = showing_items || !user.hide_followers_count
192 query = User.get_followers_query(user)
193 query = from(user in query, select: [:ap_id])
194 followers = Repo.all(query)
204 "id" => "#{user.ap_id}/followers",
205 "type" => "OrderedCollection",
208 collection(followers, "#{user.ap_id}/followers", 1, showing_items, total)
210 "#{user.ap_id}/followers?page=1"
213 |> maybe_put_total_items(showing_count, total)
214 |> Map.merge(Utils.make_json_ld_header())
217 def render("activity_collection.json", %{iri: iri}) do
220 "type" => "OrderedCollection",
221 "first" => "#{iri}?page=true"
223 |> Map.merge(Utils.make_json_ld_header())
226 def render("activity_collection_page.json", %{
227 activities: activities,
229 pagination: pagination
232 Enum.map(activities, fn activity ->
233 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
238 "type" => "OrderedCollectionPage",
240 "orderedItems" => collection
242 |> Map.merge(Utils.make_json_ld_header())
243 |> Map.merge(pagination)
246 defp maybe_put_total_items(map, false, _total), do: map
248 defp maybe_put_total_items(map, true, total) do
249 Map.put(map, "totalItems", total)
252 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
253 offset = (page - 1) * 10
254 items = Enum.slice(collection, offset, 10)
255 items = Enum.map(items, fn user -> user.ap_id end)
256 total = total || length(collection)
259 "id" => "#{iri}?page=#{page}",
260 "type" => "OrderedCollectionPage",
262 "totalItems" => total,
263 "orderedItems" => if(show_items, do: items, else: [])
267 Map.put(map, "next", "#{iri}?page=#{page + 1}")
273 defp maybe_make_image(func, key, user) do
274 if image = func.(user, no_default: true) do