1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.UserView do
12 alias Pleroma.Web.ActivityPub.ObjectView
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.ActivityPub.Utils
15 alias Pleroma.Web.Endpoint
16 alias Pleroma.Web.Router.Helpers
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.app_url(Endpoint, :create),
28 "oauthTokenEndpoint" => Helpers.o_auth_url(Endpoint, :token_exchange),
29 "sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox),
30 "uploadMedia" => Helpers.activity_pub_url(Endpoint, :upload_media)
34 def render("endpoints.json", _), do: %{}
36 def render("service.json", %{user: user}) do
37 {:ok, user} = User.ensure_keys_present(user)
38 {:ok, _, public_key} = Keys.keys_from_pem(user.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",
52 "An internal service actor for this Pleroma instance. No user-serviceable parts inside.",
54 "manuallyApprovesFollowers" => false,
56 "id" => "#{user.ap_id}#main-key",
57 "owner" => user.ap_id,
58 "publicKeyPem" => public_key
60 "endpoints" => endpoints,
61 "invisible" => User.invisible?(user)
63 |> Map.merge(Utils.make_json_ld_header())
66 # the instance itself is not a Person, but instead an Application
67 def render("user.json", %{user: %User{nickname: nil} = user}),
68 do: render("service.json", %{user: user})
70 def render("user.json", %{user: %User{nickname: "internal." <> _} = user}),
71 do: render("service.json", %{user: user}) |> Map.put("preferredUsername", user.nickname)
73 def render("user.json", %{user: user}) do
74 {:ok, user} = User.ensure_keys_present(user)
75 {:ok, _, public_key} = Keys.keys_from_pem(user.keys)
76 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
77 public_key = :public_key.pem_encode([public_key])
78 user = User.sanitize_html(user)
80 endpoints = render("endpoints.json", %{user: user})
82 emoji_tags = Transmogrifier.take_emoji_tags(user)
84 fields = Enum.map(user.fields, &Map.put(&1, "type", "PropertyValue"))
87 if is_boolean(user.accepts_chat_messages) do
89 "acceptsChatMessages" => user.accepts_chat_messages
97 "type" => user.actor_type,
98 "following" => "#{user.ap_id}/following",
99 "followers" => "#{user.ap_id}/followers",
100 "inbox" => "#{user.ap_id}/inbox",
101 "outbox" => "#{user.ap_id}/outbox",
102 "featured" => "#{user.ap_id}/collections/featured",
103 "preferredUsername" => user.nickname,
105 "summary" => user.bio,
107 "manuallyApprovesFollowers" => user.is_locked,
109 "id" => "#{user.ap_id}#main-key",
110 "owner" => user.ap_id,
111 "publicKeyPem" => public_key
113 "endpoints" => endpoints,
114 "attachment" => fields,
116 # Note: key name is indeed "discoverable" (not an error)
117 "discoverable" => user.is_discoverable,
118 "capabilities" => capabilities,
119 "alsoKnownAs" => user.also_known_as
121 |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
122 |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
123 |> Map.merge(Utils.make_json_ld_header())
126 def render("following.json", %{user: user, page: page} = opts) do
127 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_follows
128 showing_count = showing_items || !user.hide_follows_count
130 query = User.get_friends_query(user)
131 query = from(user in query, select: [:ap_id])
132 following = Repo.all(query)
141 collection(following, "#{user.ap_id}/following", page, showing_items, total)
142 |> Map.merge(Utils.make_json_ld_header())
145 def render("following.json", %{user: user} = opts) do
146 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_follows
147 showing_count = showing_items || !user.hide_follows_count
149 query = User.get_friends_query(user)
150 query = from(user in query, select: [:ap_id])
151 following = Repo.all(query)
161 "id" => "#{user.ap_id}/following",
162 "type" => "OrderedCollection",
163 "totalItems" => total,
166 collection(following, "#{user.ap_id}/following", 1, !user.hide_follows)
168 "#{user.ap_id}/following?page=1"
171 |> Map.merge(Utils.make_json_ld_header())
174 def render("followers.json", %{user: user, page: page} = opts) do
175 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_followers
176 showing_count = showing_items || !user.hide_followers_count
178 query = User.get_followers_query(user)
179 query = from(user in query, select: [:ap_id])
180 followers = Repo.all(query)
189 collection(followers, "#{user.ap_id}/followers", page, showing_items, total)
190 |> Map.merge(Utils.make_json_ld_header())
193 def render("followers.json", %{user: user} = opts) do
194 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_followers
195 showing_count = showing_items || !user.hide_followers_count
197 query = User.get_followers_query(user)
198 query = from(user in query, select: [:ap_id])
199 followers = Repo.all(query)
209 "id" => "#{user.ap_id}/followers",
210 "type" => "OrderedCollection",
213 collection(followers, "#{user.ap_id}/followers", 1, showing_items, total)
215 "#{user.ap_id}/followers?page=1"
218 |> maybe_put_total_items(showing_count, total)
219 |> Map.merge(Utils.make_json_ld_header())
222 def render("activity_collection.json", %{iri: iri}) do
225 "type" => "OrderedCollection",
226 "first" => "#{iri}?page=true"
228 |> Map.merge(Utils.make_json_ld_header())
231 def render("activity_collection_page.json", %{
232 activities: activities,
234 pagination: pagination
237 Enum.map(activities, fn activity ->
238 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
243 "type" => "OrderedCollectionPage",
245 "orderedItems" => collection
247 |> Map.merge(Utils.make_json_ld_header())
248 |> Map.merge(pagination)
251 def render("featured.json", %{
252 user: %{featured_address: featured_address, pinned_objects: pinned_objects}
256 |> Enum.sort_by(fn {_, pinned_at} -> pinned_at end, &>=/2)
257 |> Enum.map(fn {id, _} ->
258 ObjectView.render("object.json", %{object: Object.get_cached_by_ap_id(id)})
262 "id" => featured_address,
263 "type" => "OrderedCollection",
264 "orderedItems" => objects
266 |> Map.merge(Utils.make_json_ld_header())
269 defp maybe_put_total_items(map, false, _total), do: map
271 defp maybe_put_total_items(map, true, total) do
272 Map.put(map, "totalItems", total)
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