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
8 alias Pleroma.Web.WebFinger
9 alias Pleroma.Web.Salmon
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.ActivityPub.Utils
15 alias Pleroma.Web.Router.Helpers
16 alias Pleroma.Web.Endpoint
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.mastodon_api_url(Endpoint, :create_app),
28 "oauthTokenEndpoint" => Helpers.o_auth_url(Endpoint, :token_exchange),
29 "sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox)
33 def render("endpoints.json", _), do: %{}
35 # the instance itself is not a Person, but instead an Application
36 def render("user.json", %{user: %{nickname: nil} = user}) do
37 {:ok, user} = WebFinger.ensure_keys_present(user)
38 {:ok, _, public_key} = Salmon.keys_from_pem(user.info.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",
51 "summary" => "Virtual actor for Pleroma relay",
53 "manuallyApprovesFollowers" => false,
55 "id" => "#{user.ap_id}#main-key",
56 "owner" => user.ap_id,
57 "publicKeyPem" => public_key
59 "endpoints" => endpoints
61 |> Map.merge(Utils.make_json_ld_header())
64 def render("user.json", %{user: user}) do
65 {:ok, user} = WebFinger.ensure_keys_present(user)
66 {:ok, _, public_key} = Salmon.keys_from_pem(user.info.keys)
67 public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
68 public_key = :public_key.pem_encode([public_key])
70 endpoints = render("endpoints.json", %{user: user})
75 "following" => "#{user.ap_id}/following",
76 "followers" => "#{user.ap_id}/followers",
77 "inbox" => "#{user.ap_id}/inbox",
78 "outbox" => "#{user.ap_id}/outbox",
79 "preferredUsername" => user.nickname,
81 "summary" => user.bio,
83 "manuallyApprovesFollowers" => user.info.locked,
85 "id" => "#{user.ap_id}#main-key",
86 "owner" => user.ap_id,
87 "publicKeyPem" => public_key
89 "endpoints" => endpoints,
92 "url" => User.avatar_url(user)
96 "url" => User.banner_url(user)
98 "tag" => user.info.source_data["tag"] || []
100 |> Map.merge(Utils.make_json_ld_header())
103 def render("following.json", %{user: user, page: page}) do
104 query = User.get_friends_query(user)
105 query = from(user in query, select: [:ap_id])
106 following = Repo.all(query)
108 collection(following, "#{user.ap_id}/following", page, !user.info.hide_follows)
109 |> Map.merge(Utils.make_json_ld_header())
112 def render("following.json", %{user: user}) do
113 query = User.get_friends_query(user)
114 query = from(user in query, select: [:ap_id])
115 following = Repo.all(query)
118 "id" => "#{user.ap_id}/following",
119 "type" => "OrderedCollection",
120 "totalItems" => length(following),
121 "first" => collection(following, "#{user.ap_id}/following", 1, !user.info.hide_follows)
123 |> Map.merge(Utils.make_json_ld_header())
126 def render("followers.json", %{user: user, page: page}) do
127 query = User.get_followers_query(user)
128 query = from(user in query, select: [:ap_id])
129 followers = Repo.all(query)
131 collection(followers, "#{user.ap_id}/followers", page, !user.info.hide_followers)
132 |> Map.merge(Utils.make_json_ld_header())
135 def render("followers.json", %{user: user}) do
136 query = User.get_followers_query(user)
137 query = from(user in query, select: [:ap_id])
138 followers = Repo.all(query)
141 "id" => "#{user.ap_id}/followers",
142 "type" => "OrderedCollection",
143 "totalItems" => length(followers),
144 "first" => collection(followers, "#{user.ap_id}/followers", 1, !user.info.hide_followers)
146 |> Map.merge(Utils.make_json_ld_header())
149 def render("outbox.json", %{user: user, max_id: max_qid}) do
150 # XXX: technically note_count is wrong for this, but it's better than nothing
151 info = User.user_info(user)
159 Map.put(params, "max_id", max_qid)
164 activities = ActivityPub.fetch_user_activities(user, nil, params)
165 min_id = Enum.at(Enum.reverse(activities), 0).id
166 max_id = Enum.at(activities, 0).id
169 Enum.map(activities, fn act ->
170 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
174 iri = "#{user.ap_id}/outbox"
177 "id" => "#{iri}?max_id=#{max_id}",
178 "type" => "OrderedCollectionPage",
180 "totalItems" => info.note_count,
181 "orderedItems" => collection,
182 "next" => "#{iri}?max_id=#{min_id}"
188 "type" => "OrderedCollection",
189 "totalItems" => info.note_count,
192 |> Map.merge(Utils.make_json_ld_header())
194 page |> Map.merge(Utils.make_json_ld_header())
198 def render("inbox.json", %{user: user, max_id: max_qid}) do
205 Map.put(params, "max_id", max_qid)
210 activities = ActivityPub.fetch_activities([user.ap_id | user.following], params)
212 min_id = Enum.at(Enum.reverse(activities), 0).id
213 max_id = Enum.at(activities, 0).id
216 Enum.map(activities, fn act ->
217 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
221 iri = "#{user.ap_id}/inbox"
224 "id" => "#{iri}?max_id=#{max_id}",
225 "type" => "OrderedCollectionPage",
227 "orderedItems" => collection,
228 "next" => "#{iri}?max_id=#{min_id}"
234 "type" => "OrderedCollection",
237 |> Map.merge(Utils.make_json_ld_header())
239 page |> Map.merge(Utils.make_json_ld_header())
243 def collection(collection, iri, page, show_items \\ true, total \\ nil) do
244 offset = (page - 1) * 10
245 items = Enum.slice(collection, offset, 10)
246 items = Enum.map(items, fn user -> user.ap_id end)
247 total = total || length(collection)
250 "id" => "#{iri}?page=#{page}",
251 "type" => "OrderedCollectionPage",
253 "totalItems" => total,
254 "orderedItems" => if(show_items, do: items, else: [])
258 Map.put(map, "next", "#{iri}?page=#{page + 1}")