1 defmodule Pleroma.Web.ActivityPub.UserView do
3 alias Pleroma.Web.Salmon
4 alias Pleroma.Web.WebFinger
7 alias Pleroma.Web.ActivityPub.ActivityPub
8 alias Pleroma.Web.ActivityPub.Transmogrifier
9 alias Pleroma.Web.ActivityPub.Utils
12 def render("user.json", %{user: user}) do
13 {:ok, user} = WebFinger.ensure_keys_present(user)
14 {:ok, _, public_key} = Salmon.keys_from_pem(user.info["keys"])
15 public_key = :public_key.pem_entry_encode(:RSAPublicKey, public_key)
16 public_key = :public_key.pem_encode([public_key])
21 "following" => "#{user.ap_id}/following",
22 "followers" => "#{user.ap_id}/followers",
23 "inbox" => "#{user.ap_id}/inbox",
24 "outbox" => "#{user.ap_id}/outbox",
25 "preferredUsername" => user.nickname,
27 "summary" => user.bio,
29 "manuallyApprovesFollowers" => false,
31 "id" => "#{user.ap_id}#main-key",
32 "owner" => user.ap_id,
33 "publicKeyPem" => public_key
36 "sharedInbox" => "#{Pleroma.Web.Endpoint.url()}/inbox"
40 "url" => User.avatar_url(user)
44 "url" => User.banner_url(user)
47 |> Map.merge(Utils.make_json_ld_header())
50 def render("following.json", %{user: user, page: page}) do
51 query = User.get_friends_query(user)
52 query = from(user in query, select: [:ap_id])
53 following = Repo.all(query)
55 collection(following, "#{user.ap_id}/following", page)
56 |> Map.merge(Utils.make_json_ld_header())
59 def render("following.json", %{user: user}) do
60 query = User.get_friends_query(user)
61 query = from(user in query, select: [:ap_id])
62 following = Repo.all(query)
65 "id" => "#{user.ap_id}/following",
66 "type" => "OrderedCollection",
67 "totalItems" => length(following),
68 "first" => collection(following, "#{user.ap_id}/following", 1)
70 |> Map.merge(Utils.make_json_ld_header())
73 def render("followers.json", %{user: user, page: page}) do
74 query = User.get_followers_query(user)
75 query = from(user in query, select: [:ap_id])
76 followers = Repo.all(query)
78 collection(followers, "#{user.ap_id}/followers", page)
79 |> Map.merge(Utils.make_json_ld_header())
82 def render("followers.json", %{user: user}) do
83 query = User.get_followers_query(user)
84 query = from(user in query, select: [:ap_id])
85 followers = Repo.all(query)
88 "id" => "#{user.ap_id}/followers",
89 "type" => "OrderedCollection",
90 "totalItems" => length(followers),
91 "first" => collection(followers, "#{user.ap_id}/followers", 1)
93 |> Map.merge(Utils.make_json_ld_header())
96 def render("outbox.json", %{user: user, max_id: max_qid}) do
97 # XXX: technically note_count is wrong for this, but it's better than nothing
98 info = User.user_info(user)
101 "type" => ["Create", "Announce"],
102 "actor_id" => user.ap_id,
108 params = Map.put(params, "max_id", max_qid)
111 activities = ActivityPub.fetch_public_activities(params)
112 min_id = Enum.at(activities, 0).id
114 activities = Enum.reverse(activities)
115 max_id = Enum.at(activities, 0).id
118 Enum.map(activities, fn act ->
119 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
123 iri = "#{user.ap_id}/outbox"
126 "id" => "#{iri}?max_id=#{max_id}",
127 "type" => "OrderedCollectionPage",
129 "totalItems" => info.note_count,
130 "orderedItems" => collection,
131 "next" => "#{iri}?max_id=#{min_id - 1}"
137 "type" => "OrderedCollection",
138 "totalItems" => info.note_count,
141 |> Map.merge(Utils.make_json_ld_header())
143 page |> Map.merge(Utils.make_json_ld_header())
147 def collection(collection, iri, page, _total \\ nil) do
148 offset = (page - 1) * 10
149 items = Enum.slice(collection, offset, 10)
150 items = Enum.map(items, fn user -> user.ap_id end)
151 total = _total || length(collection)
154 "id" => "#{iri}?page=#{page}",
155 "type" => "OrderedCollectionPage",
157 "totalItems" => length(collection),
158 "orderedItems" => items
161 if offset < length(collection) do
162 Map.put(map, "next", "#{iri}?page=#{page + 1}")