1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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, _, public_key} = Keys.keys_from_pem(user.keys)
38 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
39 public_key = :public_key.pem_encode([public_key])
41 endpoints = render("endpoints.json", %{user: user})
45 "type" => "Application",
46 "following" => "#{user.ap_id}/following",
47 "followers" => "#{user.ap_id}/followers",
48 "inbox" => "#{user.ap_id}/inbox",
51 "An internal service actor for this Pleroma instance. No user-serviceable parts inside.",
53 "manuallyApprovesFollowers" => false,
55 "id" => "#{user.ap_id}#main-key",
56 "owner" => user.ap_id,
57 "publicKeyPem" => public_key
59 "endpoints" => endpoints,
60 "invisible" => User.invisible?(user)
62 |> Map.merge(Utils.make_json_ld_header())
65 # the instance itself is not a Person, but instead an Application
66 def render("user.json", %{user: %User{nickname: nil} = user}),
67 do: render("service.json", %{user: user})
69 def render("user.json", %{user: %User{nickname: "internal." <> _} = user}),
70 do: render("service.json", %{user: user}) |> Map.put("preferredUsername", user.nickname)
72 def render("user.json", %{user: user}) do
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
94 if user.show_birthday && user.birthday,
95 do: Date.to_iso8601(user.birthday),
100 "type" => user.actor_type,
101 "following" => "#{user.ap_id}/following",
102 "followers" => "#{user.ap_id}/followers",
103 "inbox" => "#{user.ap_id}/inbox",
104 "outbox" => "#{user.ap_id}/outbox",
105 "featured" => "#{user.ap_id}/collections/featured",
106 "preferredUsername" => user.nickname,
108 "summary" => user.bio,
110 "manuallyApprovesFollowers" => user.is_locked,
112 "id" => "#{user.ap_id}#main-key",
113 "owner" => user.ap_id,
114 "publicKeyPem" => public_key
116 "endpoints" => endpoints,
117 "attachment" => fields,
119 # Note: key name is indeed "discoverable" (not an error)
120 "discoverable" => user.is_discoverable,
121 "capabilities" => capabilities,
122 "alsoKnownAs" => user.also_known_as,
123 "vcard:bday" => birthday
125 |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
126 |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
127 |> Map.merge(Utils.make_json_ld_header())
130 def render("following.json", %{user: user, page: page} = opts) do
131 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_follows
132 showing_count = showing_items || !user.hide_follows_count
134 query = User.get_friends_query(user)
135 query = from(user in query, select: [:ap_id])
136 following = Repo.all(query)
145 collection(following, "#{user.ap_id}/following", page, showing_items, total)
146 |> Map.merge(Utils.make_json_ld_header())
149 def render("following.json", %{user: user} = opts) do
150 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_follows
151 showing_count = showing_items || !user.hide_follows_count
153 query = User.get_friends_query(user)
154 query = from(user in query, select: [:ap_id])
155 following = Repo.all(query)
165 "id" => "#{user.ap_id}/following",
166 "type" => "OrderedCollection",
167 "totalItems" => total,
170 collection(following, "#{user.ap_id}/following", 1, !user.hide_follows)
172 "#{user.ap_id}/following?page=1"
175 |> Map.merge(Utils.make_json_ld_header())
178 def render("followers.json", %{user: user, page: page} = opts) do
179 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_followers
180 showing_count = showing_items || !user.hide_followers_count
182 query = User.get_followers_query(user)
183 query = from(user in query, select: [:ap_id])
184 followers = Repo.all(query)
193 collection(followers, "#{user.ap_id}/followers", page, showing_items, total)
194 |> Map.merge(Utils.make_json_ld_header())
197 def render("followers.json", %{user: user} = opts) do
198 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_followers
199 showing_count = showing_items || !user.hide_followers_count
201 query = User.get_followers_query(user)
202 query = from(user in query, select: [:ap_id])
203 followers = Repo.all(query)
213 "id" => "#{user.ap_id}/followers",
214 "type" => "OrderedCollection",
217 collection(followers, "#{user.ap_id}/followers", 1, showing_items, total)
219 "#{user.ap_id}/followers?page=1"
222 |> maybe_put_total_items(showing_count, total)
223 |> Map.merge(Utils.make_json_ld_header())
226 def render("activity_collection.json", %{iri: iri}) do
229 "type" => "OrderedCollection",
230 "first" => "#{iri}?page=true"
232 |> Map.merge(Utils.make_json_ld_header())
235 def render("activity_collection_page.json", %{
236 activities: activities,
238 pagination: pagination
241 Enum.map(activities, fn activity ->
242 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
247 "type" => "OrderedCollectionPage",
249 "orderedItems" => collection
251 |> Map.merge(Utils.make_json_ld_header())
252 |> Map.merge(pagination)
255 def render("featured.json", %{
256 user: %{featured_address: featured_address, pinned_objects: pinned_objects}
260 |> Enum.sort_by(fn {_, pinned_at} -> pinned_at end, &>=/2)
261 |> Enum.map(fn {id, _} ->
262 ObjectView.render("object.json", %{object: Object.get_cached_by_ap_id(id)})
266 "id" => featured_address,
267 "type" => "OrderedCollection",
268 "orderedItems" => objects,
269 "totalItems" => length(objects)
271 |> Map.merge(Utils.make_json_ld_header())
274 defp maybe_put_total_items(map, false, _total), do: map
276 defp maybe_put_total_items(map, true, total) do
277 Map.put(map, "totalItems", total)
280 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
281 offset = (page - 1) * 10
282 items = Enum.slice(collection, offset, 10)
283 items = Enum.map(items, fn user -> user.ap_id end)
284 total = total || length(collection)
287 "id" => "#{iri}?page=#{page}",
288 "type" => "OrderedCollectionPage",
290 "totalItems" => total,
291 "orderedItems" => if(show_items, do: items, else: [])
295 Map.put(map, "next", "#{iri}?page=#{page + 1}")
301 defp maybe_make_image(func, key, user) do
302 if image = func.(user, no_default: true) do