Add debug logs to timeline rendering to assist debugging
[akkoma] / lib / pleroma / web / mastodon_api / controllers / timeline_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.TimelineController do
6 use Pleroma.Web, :controller
7
8 import Pleroma.Web.ControllerHelper,
9 only: [add_link_headers: 2, add_link_headers: 3]
10
11 alias Pleroma.Config
12 alias Pleroma.Pagination
13 alias Pleroma.User
14 alias Pleroma.Web.ActivityPub.ActivityPub
15 alias Pleroma.Web.Plugs.OAuthScopesPlug
16 alias Pleroma.Web.Plugs.RateLimiter
17
18 plug(Pleroma.Web.ApiSpec.CastAndValidate)
19 plug(:skip_public_check when action in [:public, :hashtag, :bubble])
20
21 # TODO: Replace with a macro when there is a Phoenix release with the following commit in it:
22 # https://github.com/phoenixframework/phoenix/commit/2e8c63c01fec4dde5467dbbbf9705ff9e780735e
23
24 plug(RateLimiter, [name: :timeline, bucket_name: :direct_timeline] when action == :direct)
25 plug(RateLimiter, [name: :timeline, bucket_name: :public_timeline] when action == :public)
26 plug(RateLimiter, [name: :timeline, bucket_name: :home_timeline] when action == :home)
27 plug(RateLimiter, [name: :timeline, bucket_name: :hashtag_timeline] when action == :hashtag)
28 plug(RateLimiter, [name: :timeline, bucket_name: :list_timeline] when action == :list)
29 plug(RateLimiter, [name: :timeline, bucket_name: :bubble_timeline] when action == :bubble)
30
31 plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action in [:home, :direct])
32 plug(OAuthScopesPlug, %{scopes: ["read:lists"]} when action == :list)
33
34 plug(
35 OAuthScopesPlug,
36 %{scopes: ["read:statuses"], fallback: :proceed_unauthenticated}
37 when action in [:public, :hashtag, :bubble]
38 )
39
40 require Logger
41
42 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TimelineOperation
43
44 # GET /api/v1/timelines/home
45 def home(%{assigns: %{user: user}} = conn, params) do
46 %{nickname: nickname} = user
47
48 Logger.debug("TimelineController.home: #{nickname}")
49
50 followed_hashtags =
51 user
52 |> User.followed_hashtags()
53 |> Enum.map(& &1.id)
54
55 params =
56 params
57 |> Map.put(:type, ["Create", "Announce"])
58 |> Map.put(:blocking_user, user)
59 |> Map.put(:muting_user, user)
60 |> Map.put(:reply_filtering_user, user)
61 |> Map.put(:announce_filtering_user, user)
62 |> Map.put(:user, user)
63 |> Map.put(:local_only, params[:local])
64 |> Map.put(:followed_hashtags, followed_hashtags)
65 |> Map.delete(:local)
66
67 Logger.debug("TimelineController.home: #{nickname} - fetching activities")
68
69 activities =
70 [user.ap_id | User.following(user)]
71 |> ActivityPub.fetch_activities(params)
72 |> Enum.reverse()
73
74 Logger.debug("TimelineController.home: #{nickname} - rendering")
75
76 conn
77 |> add_link_headers(activities)
78 |> render("index.json",
79 activities: activities,
80 for: user,
81 as: :activity,
82 with_muted: Map.get(params, :with_muted, false)
83 )
84 end
85
86 # GET /api/v1/timelines/direct
87 def direct(%{assigns: %{user: user}} = conn, params) do
88 Logger.debug("TimelineController.direct: #{user.nickname}")
89
90 params =
91 params
92 |> Map.put(:type, "Create")
93 |> Map.put(:blocking_user, user)
94 |> Map.put(:user, user)
95 |> Map.put(:visibility, "direct")
96
97 Logger.debug("TimelineController.direct: #{user.nickname} - fetching activities")
98
99 activities =
100 [user.ap_id]
101 |> ActivityPub.fetch_activities_query(params)
102 |> Pagination.fetch_paginated(params)
103
104 Logger.debug("TimelineController.direct: #{user.nickname} - rendering")
105
106 conn
107 |> add_link_headers(activities)
108 |> render("index.json",
109 activities: activities,
110 for: user,
111 as: :activity
112 )
113 end
114
115 defp restrict_unauthenticated?(type) do
116 Config.restrict_unauthenticated_access?(:timelines, type)
117 end
118
119 # GET /api/v1/timelines/public
120 def public(%{assigns: %{user: user}} = conn, params) do
121 Logger.debug("TimelineController.public")
122 local_only = params[:local]
123 timeline_type = if local_only, do: :local, else: :federated
124
125 with {:enabled, true} <-
126 {:enabled, local_only || Config.get([:instance, :federated_timeline_available], true)},
127 {:authenticated, true} <-
128 {:authenticated, !(is_nil(user) and restrict_unauthenticated?(timeline_type))} do
129 Logger.debug("TimelineController.public: fetching activities")
130
131 activities =
132 params
133 |> Map.put(:type, ["Create"])
134 |> Map.put(:local_only, local_only)
135 |> Map.put(:blocking_user, user)
136 |> Map.put(:muting_user, user)
137 |> Map.put(:reply_filtering_user, user)
138 |> Map.put(:instance, params[:instance])
139 # Restricts unfederated content to authenticated users
140 |> Map.put(:includes_local_public, not is_nil(user))
141 |> ActivityPub.fetch_public_activities()
142
143 Logger.debug("TimelineController.public: rendering")
144
145 conn
146 |> add_link_headers(activities, %{"local" => local_only})
147 |> render("index.json",
148 activities: activities,
149 for: user,
150 as: :activity,
151 with_muted: Map.get(params, :with_muted, false)
152 )
153 else
154 {:enabled, false} ->
155 conn
156 |> put_status(404)
157 |> json(%{error: "Federated timeline is disabled"})
158
159 {:authenticated, false} ->
160 fail_on_bad_auth(conn)
161 end
162 end
163
164 # GET /api/v1/timelines/bubble
165 def bubble(%{assigns: %{user: user}} = conn, params) do
166 Logger.debug("TimelineController.bubble")
167
168 if is_nil(user) and restrict_unauthenticated?(:bubble) do
169 fail_on_bad_auth(conn)
170 else
171 bubble_instances =
172 Enum.uniq(
173 Config.get([:instance, :local_bubble], []) ++
174 [Pleroma.Web.Endpoint.host()]
175 )
176
177 Logger.debug("TimelineController.bubble: fetching activities")
178
179 activities =
180 params
181 |> Map.put(:type, ["Create"])
182 |> Map.put(:blocking_user, user)
183 |> Map.put(:muting_user, user)
184 |> Map.put(:reply_filtering_user, user)
185 |> Map.put(:instance, bubble_instances)
186 |> ActivityPub.fetch_public_activities()
187
188 Logger.debug("TimelineController.bubble: rendering")
189
190 conn
191 |> add_link_headers(activities)
192 |> render("index.json",
193 activities: activities,
194 for: user,
195 as: :activity,
196 with_muted: Map.get(params, :with_muted, false)
197 )
198 end
199 end
200
201 defp fail_on_bad_auth(conn) do
202 render_error(conn, :unauthorized, "authorization required for timeline view")
203 end
204
205 defp hashtag_fetching(params, user, local_only) do
206 # Note: not sanitizing tag options at this stage (may be mix-cased, have duplicates etc.)
207 tags_any =
208 [params[:tag], params[:any]]
209 |> List.flatten()
210 |> Enum.filter(& &1)
211
212 tag_all = Map.get(params, :all, [])
213 tag_reject = Map.get(params, :none, [])
214
215 params
216 |> Map.put(:type, "Create")
217 |> Map.put(:local_only, local_only)
218 |> Map.put(:blocking_user, user)
219 |> Map.put(:muting_user, user)
220 |> Map.put(:user, user)
221 |> Map.put(:tag, tags_any)
222 |> Map.put(:tag_all, tag_all)
223 |> Map.put(:tag_reject, tag_reject)
224 |> ActivityPub.fetch_public_activities()
225 end
226
227 # GET /api/v1/timelines/tag/:tag
228 def hashtag(%{assigns: %{user: user}} = conn, params) do
229 local_only = params[:local]
230
231 if is_nil(user) and restrict_unauthenticated?(if local_only, do: :local, else: :federated) do
232 fail_on_bad_auth(conn)
233 else
234 activities = hashtag_fetching(params, user, local_only)
235
236 conn
237 |> add_link_headers(activities, %{"local" => local_only})
238 |> render("index.json",
239 activities: activities,
240 for: user,
241 as: :activity,
242 with_muted: Map.get(params, :with_muted, false)
243 )
244 end
245 end
246
247 # GET /api/v1/timelines/list/:list_id
248 def list(%{assigns: %{user: user}} = conn, %{list_id: id} = params) do
249 with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
250 params =
251 params
252 |> Map.put(:type, "Create")
253 |> Map.put(:blocking_user, user)
254 |> Map.put(:user, user)
255 |> Map.put(:muting_user, user)
256 |> Map.put(:local_only, params[:local])
257
258 # we must filter the following list for the user to avoid leaking statuses the user
259 # does not actually have permission to see (for more info, peruse security issue #270).
260
261 user_following = User.following(user)
262
263 activities =
264 following
265 |> Enum.filter(fn x -> x in user_following end)
266 |> ActivityPub.fetch_activities_bounded(following, params)
267 |> Enum.reverse()
268
269 conn
270 |> add_link_headers(activities)
271 |> render("index.json",
272 activities: activities,
273 for: user,
274 as: :activity,
275 with_muted: Map.get(params, :with_muted, false)
276 )
277 else
278 _e -> render_error(conn, :forbidden, "Error.")
279 end
280 end
281 end