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",
49 "outbox" => "#{user.ap_id}/outbox",
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}) do
71 render("service.json", %{user: user})
73 "preferredUsername" => user.nickname,
74 "webfinger" => "acct:#{User.full_nickname(user)}"
78 def render("user.json", %{user: user}) do
79 {:ok, _, public_key} = Keys.keys_from_pem(user.keys)
80 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
81 public_key = :public_key.pem_encode([public_key])
82 user = User.sanitize_html(user)
84 endpoints = render("endpoints.json", %{user: user})
86 emoji_tags = Transmogrifier.take_emoji_tags(user)
88 fields = Enum.map(user.fields, &Map.put(&1, "type", "PropertyValue"))
91 if is_boolean(user.accepts_chat_messages) do
93 "acceptsChatMessages" => user.accepts_chat_messages
100 if user.show_birthday && user.birthday,
101 do: Date.to_iso8601(user.birthday),
106 "type" => user.actor_type,
107 "following" => "#{user.ap_id}/following",
108 "followers" => "#{user.ap_id}/followers",
109 "inbox" => "#{user.ap_id}/inbox",
110 "outbox" => "#{user.ap_id}/outbox",
111 "featured" => "#{user.ap_id}/collections/featured",
112 "preferredUsername" => user.nickname,
114 "summary" => user.bio,
116 "manuallyApprovesFollowers" => user.is_locked,
118 "id" => "#{user.ap_id}#main-key",
119 "owner" => user.ap_id,
120 "publicKeyPem" => public_key
122 "endpoints" => endpoints,
123 "attachment" => fields,
125 # Note: key name is indeed "discoverable" (not an error)
126 "discoverable" => user.is_discoverable,
127 "capabilities" => capabilities,
128 "alsoKnownAs" => user.also_known_as,
129 "vcard:bday" => birthday,
130 "webfinger" => "acct:#{User.full_nickname(user)}"
132 |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
133 |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
134 |> Map.merge(Utils.make_json_ld_header())
137 def render("following.json", %{user: user, page: page} = opts) do
138 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_follows
139 showing_count = showing_items || !user.hide_follows_count
141 query = User.get_friends_query(user)
142 query = from(user in query, select: [:ap_id])
143 following = Repo.all(query)
152 collection(following, "#{user.ap_id}/following", page, showing_items, total)
153 |> Map.merge(Utils.make_json_ld_header())
156 def render("following.json", %{user: user} = opts) do
157 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_follows
158 showing_count = showing_items || !user.hide_follows_count
160 query = User.get_friends_query(user)
161 query = from(user in query, select: [:ap_id])
162 following = Repo.all(query)
172 "id" => "#{user.ap_id}/following",
173 "type" => "OrderedCollection",
174 "totalItems" => total,
177 collection(following, "#{user.ap_id}/following", 1, !user.hide_follows)
179 "#{user.ap_id}/following?page=1"
182 |> Map.merge(Utils.make_json_ld_header())
185 def render("followers.json", %{user: user, page: page} = opts) do
186 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_followers
187 showing_count = showing_items || !user.hide_followers_count
189 query = User.get_followers_query(user)
190 query = from(user in query, select: [:ap_id])
191 followers = Repo.all(query)
200 collection(followers, "#{user.ap_id}/followers", page, showing_items, total)
201 |> Map.merge(Utils.make_json_ld_header())
204 def render("followers.json", %{user: user} = opts) do
205 showing_items = (opts[:for] && opts[:for] == user) || !user.hide_followers
206 showing_count = showing_items || !user.hide_followers_count
208 query = User.get_followers_query(user)
209 query = from(user in query, select: [:ap_id])
210 followers = Repo.all(query)
220 "id" => "#{user.ap_id}/followers",
221 "type" => "OrderedCollection",
224 collection(followers, "#{user.ap_id}/followers", 1, showing_items, total)
226 "#{user.ap_id}/followers?page=1"
229 |> maybe_put_total_items(showing_count, total)
230 |> Map.merge(Utils.make_json_ld_header())
233 def render("activity_collection.json", %{iri: iri}) do
236 "type" => "OrderedCollection",
237 "first" => "#{iri}?page=true"
239 |> Map.merge(Utils.make_json_ld_header())
242 def render("activity_collection_page.json", %{
243 activities: activities,
245 pagination: pagination
248 Enum.map(activities, fn activity ->
249 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
254 "type" => "OrderedCollectionPage",
256 "orderedItems" => collection
258 |> Map.merge(Utils.make_json_ld_header())
259 |> Map.merge(pagination)
262 def render("featured.json", %{
263 user: %{featured_address: featured_address, pinned_objects: pinned_objects}
267 |> Enum.sort_by(fn {_, pinned_at} -> pinned_at end, &>=/2)
268 |> Enum.map(fn {id, _} ->
269 ObjectView.render("object.json", %{object: Object.get_cached_by_ap_id(id)})
273 "id" => featured_address,
274 "type" => "OrderedCollection",
275 "orderedItems" => objects,
276 "totalItems" => length(objects)
278 |> Map.merge(Utils.make_json_ld_header())
281 defp maybe_put_total_items(map, false, _total), do: map
283 defp maybe_put_total_items(map, true, total) do
284 Map.put(map, "totalItems", total)
287 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
288 offset = (page - 1) * 10
289 items = Enum.slice(collection, offset, 10)
290 items = Enum.map(items, fn user -> user.ap_id end)
291 total = total || length(collection)
294 "id" => "#{iri}?page=#{page}",
295 "type" => "OrderedCollectionPage",
297 "totalItems" => total,
298 "orderedItems" => if(show_items, do: items, else: [])
302 Map.put(map, "next", "#{iri}?page=#{page + 1}")
308 defp maybe_make_image(func, key, user) do
309 if image = func.(user, no_default: true) do