1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.mastodon_api_url(Endpoint, :create_app),
26 "oauthTokenEndpoint" => Helpers.o_auth_url(Endpoint, :token_exchange),
27 "sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox)
31 def render("endpoints.json", _), do: %{}
33 def render("service.json", %{user: user}) do
34 {:ok, user} = User.ensure_keys_present(user)
35 {:ok, _, public_key} = Keys.keys_from_pem(user.info.keys)
36 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
37 public_key = :public_key.pem_encode([public_key])
39 endpoints = render("endpoints.json", %{user: user})
43 "type" => "Application",
44 "following" => "#{user.ap_id}/following",
45 "followers" => "#{user.ap_id}/followers",
46 "inbox" => "#{user.ap_id}/inbox",
49 "An internal service actor for this Pleroma instance. No user-serviceable parts inside.",
51 "manuallyApprovesFollowers" => false,
53 "id" => "#{user.ap_id}#main-key",
54 "owner" => user.ap_id,
55 "publicKeyPem" => public_key
57 "endpoints" => endpoints
59 |> Map.merge(Utils.make_json_ld_header())
62 # the instance itself is not a Person, but instead an Application
63 def render("user.json", %{user: %User{nickname: nil} = user}),
64 do: render("service.json", %{user: user})
66 def render("user.json", %{user: %User{nickname: "internal." <> _} = user}),
67 do: render("service.json", %{user: user}) |> Map.put("preferredUsername", user.nickname)
69 def render("user.json", %{user: user}) do
70 {:ok, user} = User.ensure_keys_present(user)
71 {:ok, _, public_key} = Keys.keys_from_pem(user.info.keys)
72 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
73 public_key = :public_key.pem_encode([public_key])
75 endpoints = render("endpoints.json", %{user: user})
77 emoji_tags = Transmogrifier.take_emoji_tags(user)
82 |> Enum.map(fn %{"name" => name, "value" => value} ->
84 "name" => Pleroma.HTML.strip_tags(name),
85 "value" => Pleroma.HTML.filter_tags(value, Pleroma.HTML.Scrubber.LinksOnly)
88 |> Enum.map(&Map.put(&1, "type", "PropertyValue"))
93 "following" => "#{user.ap_id}/following",
94 "followers" => "#{user.ap_id}/followers",
95 "inbox" => "#{user.ap_id}/inbox",
96 "outbox" => "#{user.ap_id}/outbox",
97 "preferredUsername" => user.nickname,
99 "summary" => user.bio,
101 "manuallyApprovesFollowers" => user.info.locked,
103 "id" => "#{user.ap_id}#main-key",
104 "owner" => user.ap_id,
105 "publicKeyPem" => public_key
107 "endpoints" => endpoints,
108 "attachment" => fields,
109 "tag" => (user.info.source_data["tag"] || []) ++ emoji_tags
111 |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
112 |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
113 |> Map.merge(Utils.make_json_ld_header())
116 def render("following.json", %{user: user, page: page} = opts) do
117 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
118 showing_count = showing_items || !user.info.hide_follows_count
120 query = User.get_friends_query(user)
121 query = from(user in query, select: [:ap_id])
122 following = Repo.all(query)
131 collection(following, "#{user.ap_id}/following", page, showing_items, total)
132 |> Map.merge(Utils.make_json_ld_header())
135 def render("following.json", %{user: user} = opts) do
136 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
137 showing_count = showing_items || !user.info.hide_follows_count
139 query = User.get_friends_query(user)
140 query = from(user in query, select: [:ap_id])
141 following = Repo.all(query)
151 "id" => "#{user.ap_id}/following",
152 "type" => "OrderedCollection",
153 "totalItems" => total,
156 collection(following, "#{user.ap_id}/following", 1, !user.info.hide_follows)
158 "#{user.ap_id}/following?page=1"
161 |> Map.merge(Utils.make_json_ld_header())
164 def render("followers.json", %{user: user, page: page} = opts) do
165 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
166 showing_count = showing_items || !user.info.hide_followers_count
168 query = User.get_followers_query(user)
169 query = from(user in query, select: [:ap_id])
170 followers = Repo.all(query)
179 collection(followers, "#{user.ap_id}/followers", page, showing_items, total)
180 |> Map.merge(Utils.make_json_ld_header())
183 def render("followers.json", %{user: user} = opts) do
184 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
185 showing_count = showing_items || !user.info.hide_followers_count
187 query = User.get_followers_query(user)
188 query = from(user in query, select: [:ap_id])
189 followers = Repo.all(query)
199 "id" => "#{user.ap_id}/followers",
200 "type" => "OrderedCollection",
201 "totalItems" => total,
204 collection(followers, "#{user.ap_id}/followers", 1, showing_items, total)
206 "#{user.ap_id}/followers?page=1"
209 |> Map.merge(Utils.make_json_ld_header())
212 def render("activity_collection.json", %{iri: iri}) do
215 "type" => "OrderedCollection",
216 "first" => "#{iri}?page=true"
218 |> Map.merge(Utils.make_json_ld_header())
221 def render("activity_collection_page.json", %{activities: activities, iri: iri}) do
222 # this is sorted chronologically, so first activity is the newest (max)
223 {max_id, min_id, collection} =
224 if length(activities) > 0 do
226 Enum.at(activities, 0).id,
227 Enum.at(Enum.reverse(activities), 0).id,
228 Enum.map(activities, fn act ->
229 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
242 "id" => "#{iri}?max_id=#{max_id}&page=true",
243 "type" => "OrderedCollectionPage",
245 "orderedItems" => collection,
246 "next" => "#{iri}?max_id=#{min_id}&page=true"
248 |> Map.merge(Utils.make_json_ld_header())
251 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
252 offset = (page - 1) * 10
253 items = Enum.slice(collection, offset, 10)
254 items = Enum.map(items, fn user -> user.ap_id end)
255 total = total || length(collection)
258 "id" => "#{iri}?page=#{page}",
259 "type" => "OrderedCollectionPage",
261 "totalItems" => total,
262 "orderedItems" => if(show_items, do: items, else: [])
266 Map.put(map, "next", "#{iri}?page=#{page + 1}")
272 defp maybe_make_image(func, key, user) do
273 if image = func.(user, no_default: true) do