Group def renders here as well
[akkoma] / lib / pleroma / web / activity_pub / views / user_view.ex
1 defmodule Pleroma.Web.ActivityPub.UserView do
2 use Pleroma.Web, :view
3 alias Pleroma.Web.Salmon
4 alias Pleroma.Web.WebFinger
5 alias Pleroma.User
6 alias Pleroma.Repo
7 alias Pleroma.Web.ActivityPub.ActivityPub
8 alias Pleroma.Web.ActivityPub.Transmogrifier
9 alias Pleroma.Web.ActivityPub.Utils
10 import Ecto.Query
11
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])
17
18 %{
19 "id" => user.ap_id,
20 "type" => "Person",
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,
26 "name" => user.name,
27 "summary" => user.bio,
28 "url" => user.ap_id,
29 "manuallyApprovesFollowers" => false,
30 "publicKey" => %{
31 "id" => "#{user.ap_id}#main-key",
32 "owner" => user.ap_id,
33 "publicKeyPem" => public_key
34 },
35 "endpoints" => %{
36 "sharedInbox" => "#{Pleroma.Web.Endpoint.url()}/inbox"
37 },
38 "icon" => %{
39 "type" => "Image",
40 "url" => User.avatar_url(user)
41 },
42 "image" => %{
43 "type" => "Image",
44 "url" => User.banner_url(user)
45 }
46 }
47 |> Map.merge(Utils.make_json_ld_header())
48 end
49
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)
54
55 collection(following, "#{user.ap_id}/following", page)
56 |> Map.merge(Utils.make_json_ld_header())
57 end
58
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)
63
64 %{
65 "id" => "#{user.ap_id}/following",
66 "type" => "OrderedCollection",
67 "totalItems" => length(following),
68 "first" => collection(following, "#{user.ap_id}/following", 1)
69 }
70 |> Map.merge(Utils.make_json_ld_header())
71 end
72
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)
77
78 collection(followers, "#{user.ap_id}/followers", page)
79 |> Map.merge(Utils.make_json_ld_header())
80 end
81
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)
86
87 %{
88 "id" => "#{user.ap_id}/followers",
89 "type" => "OrderedCollection",
90 "totalItems" => length(followers),
91 "first" => collection(followers, "#{user.ap_id}/followers", 1)
92 }
93 |> Map.merge(Utils.make_json_ld_header())
94 end
95
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)
99
100 params = %{
101 "type" => ["Create", "Announce"],
102 "actor_id" => user.ap_id,
103 "whole_db" => true,
104 "limit" => "10"
105 }
106
107 if max_qid != nil do
108 params = Map.put(params, "max_id", max_qid)
109 end
110
111 activities = ActivityPub.fetch_public_activities(params)
112 min_id = Enum.at(activities, 0).id
113
114 activities = Enum.reverse(activities)
115 max_id = Enum.at(activities, 0).id
116
117 collection =
118 Enum.map(activities, fn act ->
119 {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
120 data
121 end)
122
123 iri = "#{user.ap_id}/outbox"
124
125 page = %{
126 "id" => "#{iri}?max_id=#{max_id}",
127 "type" => "OrderedCollectionPage",
128 "partOf" => iri,
129 "totalItems" => info.note_count,
130 "orderedItems" => collection,
131 "next" => "#{iri}?max_id=#{min_id - 1}"
132 }
133
134 if max_qid == nil do
135 %{
136 "id" => iri,
137 "type" => "OrderedCollection",
138 "totalItems" => info.note_count,
139 "first" => page
140 }
141 |> Map.merge(Utils.make_json_ld_header())
142 else
143 page |> Map.merge(Utils.make_json_ld_header())
144 end
145 end
146
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)
152
153 map = %{
154 "id" => "#{iri}?page=#{page}",
155 "type" => "OrderedCollectionPage",
156 "partOf" => iri,
157 "totalItems" => length(collection),
158 "orderedItems" => items
159 }
160
161 if offset < length(collection) do
162 Map.put(map, "next", "#{iri}?page=#{page + 1}")
163 end
164 end
165 end