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,
110 "discoverable" => user.info.discoverable
112 |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
113 |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
114 |> Map.merge(Utils.make_json_ld_header())
117 def render("following.json", %{user: user, page: page} = opts) do
118 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
119 showing_count = showing_items || !user.info.hide_follows_count
121 query = User.get_friends_query(user)
122 query = from(user in query, select: [:ap_id])
123 following = Repo.all(query)
132 collection(following, "#{user.ap_id}/following", page, showing_items, total)
133 |> Map.merge(Utils.make_json_ld_header())
136 def render("following.json", %{user: user} = opts) do
137 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
138 showing_count = showing_items || !user.info.hide_follows_count
140 query = User.get_friends_query(user)
141 query = from(user in query, select: [:ap_id])
142 following = Repo.all(query)
152 "id" => "#{user.ap_id}/following",
153 "type" => "OrderedCollection",
154 "totalItems" => total,
157 collection(following, "#{user.ap_id}/following", 1, !user.info.hide_follows)
159 "#{user.ap_id}/following?page=1"
162 |> Map.merge(Utils.make_json_ld_header())
165 def render("followers.json", %{user: user, page: page} = opts) do
166 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
167 showing_count = showing_items || !user.info.hide_followers_count
169 query = User.get_followers_query(user)
170 query = from(user in query, select: [:ap_id])
171 followers = Repo.all(query)
180 collection(followers, "#{user.ap_id}/followers", page, showing_items, total)
181 |> Map.merge(Utils.make_json_ld_header())
184 def render("followers.json", %{user: user} = opts) do
185 showing_items = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
186 showing_count = showing_items || !user.info.hide_followers_count
188 query = User.get_followers_query(user)
189 query = from(user in query, select: [:ap_id])
190 followers = Repo.all(query)
200 "id" => "#{user.ap_id}/followers",
201 "type" => "OrderedCollection",
202 "totalItems" => total,
205 collection(followers, "#{user.ap_id}/followers", 1, showing_items, total)
207 "#{user.ap_id}/followers?page=1"
210 |> Map.merge(Utils.make_json_ld_header())
213 def render("activity_collection.json", %{iri: iri}) do
216 "type" => "OrderedCollection",
217 "first" => "#{iri}?page=true"
219 |> Map.merge(Utils.make_json_ld_header())
222 def render("activity_collection_page.json", %{activities: activities, iri: iri}) do
223 # this is sorted chronologically, so first activity is the newest (max)
224 {max_id, min_id, collection} =
225 if length(activities) > 0 do
227 Enum.at(activities, 0).id,
228 Enum.at(Enum.reverse(activities), 0).id,
229 Enum.map(activities, fn act ->
230 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
243 "id" => "#{iri}?max_id=#{max_id}&page=true",
244 "type" => "OrderedCollectionPage",
246 "orderedItems" => collection,
247 "next" => "#{iri}?max_id=#{min_id}&page=true"
249 |> Map.merge(Utils.make_json_ld_header())
252 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
253 offset = (page - 1) * 10
254 items = Enum.slice(collection, offset, 10)
255 items = Enum.map(items, fn user -> user.ap_id end)
256 total = total || length(collection)
259 "id" => "#{iri}?page=#{page}",
260 "type" => "OrderedCollectionPage",
262 "totalItems" => total,
263 "orderedItems" => if(show_items, do: items, else: [])
267 Map.put(map, "next", "#{iri}?page=#{page + 1}")
273 defp maybe_make_image(func, key, user) do
274 if image = func.(user, no_default: true) do