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"))
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.is_locked,
106 "id" => "#{user.ap_id}#main-key",
107 "owner" => user.ap_id,
108 "publicKeyPem" => public_key
110 "endpoints" => endpoints,
111 "attachment" => fields,
113 # Note: key name is indeed "discoverable" (not an error)
114 "discoverable" => user.is_discoverable,
115 "capabilities" => capabilities,
116 "alsoKnownAs" => user.also_known_as
118 |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
119 |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
120 |> Map.merge(Utils.make_json_ld_header())
123 def render("following.json", %{user: user, page: page} = opts) do
124 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_follows
125 showing_count = showing_items || !user.hide_follows_count
127 query = User.get_friends_query(user)
128 query = from(user in query, select: [:ap_id])
129 following = Repo.all(query)
138 collection(following, "#{user.ap_id}/following", page, showing_items, total)
139 |> Map.merge(Utils.make_json_ld_header())
142 def render("following.json", %{user: user} = opts) do
143 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_follows
144 showing_count = showing_items || !user.hide_follows_count
146 query = User.get_friends_query(user)
147 query = from(user in query, select: [:ap_id])
148 following = Repo.all(query)
158 "id" => "#{user.ap_id}/following",
159 "type" => "OrderedCollection",
160 "totalItems" => total,
163 collection(following, "#{user.ap_id}/following", 1, !user.hide_follows)
165 "#{user.ap_id}/following?page=1"
168 |> Map.merge(Utils.make_json_ld_header())
171 def render("followers.json", %{user: user, page: page} = opts) do
172 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_followers
173 showing_count = showing_items || !user.hide_followers_count
175 query = User.get_followers_query(user)
176 query = from(user in query, select: [:ap_id])
177 followers = Repo.all(query)
186 collection(followers, "#{user.ap_id}/followers", page, showing_items, total)
187 |> Map.merge(Utils.make_json_ld_header())
190 def render("followers.json", %{user: user} = opts) do
191 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_followers
192 showing_count = showing_items || !user.hide_followers_count
194 query = User.get_followers_query(user)
195 query = from(user in query, select: [:ap_id])
196 followers = Repo.all(query)
206 "id" => "#{user.ap_id}/followers",
207 "type" => "OrderedCollection",
210 collection(followers, "#{user.ap_id}/followers", 1, showing_items, total)
212 "#{user.ap_id}/followers?page=1"
215 |> maybe_put_total_items(showing_count, total)
216 |> Map.merge(Utils.make_json_ld_header())
219 def render("activity_collection.json", %{iri: iri}) do
222 "type" => "OrderedCollection",
223 "first" => "#{iri}?page=true"
225 |> Map.merge(Utils.make_json_ld_header())
228 def render("activity_collection_page.json", %{
229 activities: activities,
231 pagination: pagination
234 Enum.map(activities, fn activity ->
235 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
240 "type" => "OrderedCollectionPage",
242 "orderedItems" => collection
244 |> Map.merge(Utils.make_json_ld_header())
245 |> Map.merge(pagination)
248 defp maybe_put_total_items(map, false, _total), do: map
250 defp maybe_put_total_items(map, true, total) do
251 Map.put(map, "totalItems", total)
254 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
255 offset = (page - 1) * 10
256 items = Enum.slice(collection, offset, 10)
257 items = Enum.map(items, fn user -> user.ap_id end)
258 total = total || length(collection)
261 "id" => "#{iri}?page=#{page}",
262 "type" => "OrderedCollectionPage",
264 "totalItems" => total,
265 "orderedItems" => if(show_items, do: items, else: [])
269 Map.put(map, "next", "#{iri}?page=#{page + 1}")
275 defp maybe_make_image(func, key, user) do
276 if image = func.(user, no_default: true) do