revert 4a94c9a31ef11f63ea71ad9c1f085c18cf8ef083
[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])
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, :bubble])
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]
38 )
39
40 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TimelineOperation
41
42 # GET /api/v1/timelines/home
43 def home(%{assigns: %{user: user}} = conn, params) do
44 params =
45 params
46 |> Map.put(:type, ["Create", "Announce"])
47 |> Map.put(:blocking_user, user)
48 |> Map.put(:muting_user, user)
49 |> Map.put(:reply_filtering_user, user)
50 |> Map.put(:announce_filtering_user, user)
51 |> Map.put(:user, user)
52 |> Map.put(:local_only, params[:local])
53 |> Map.delete(:local)
54
55 activities =
56 [user.ap_id | User.following(user)]
57 |> ActivityPub.fetch_activities(params)
58 |> Enum.reverse()
59
60 conn
61 |> add_link_headers(activities)
62 |> render("index.json",
63 activities: activities,
64 for: user,
65 as: :activity,
66 with_muted: Map.get(params, :with_muted, false)
67 )
68 end
69
70 # GET /api/v1/timelines/direct
71 def direct(%{assigns: %{user: user}} = conn, params) do
72 params =
73 params
74 |> Map.put(:type, "Create")
75 |> Map.put(:blocking_user, user)
76 |> Map.put(:user, user)
77 |> Map.put(:visibility, "direct")
78
79 activities =
80 [user.ap_id]
81 |> ActivityPub.fetch_activities_query(params)
82 |> Pagination.fetch_paginated(params)
83
84 conn
85 |> add_link_headers(activities)
86 |> render("index.json",
87 activities: activities,
88 for: user,
89 as: :activity
90 )
91 end
92
93 defp restrict_unauthenticated?(true = _local_only) do
94 Config.restrict_unauthenticated_access?(:timelines, :local)
95 end
96
97 defp restrict_unauthenticated?(_) do
98 Config.restrict_unauthenticated_access?(:timelines, :federated)
99 end
100
101 # GET /api/v1/timelines/public
102 def public(%{assigns: %{user: user}} = conn, params) do
103 local_only = params[:local]
104
105 if is_nil(user) and restrict_unauthenticated?(local_only) do
106 fail_on_bad_auth(conn)
107 else
108 activities =
109 params
110 |> Map.put(:type, ["Create"])
111 |> Map.put(:local_only, local_only)
112 |> Map.put(:blocking_user, user)
113 |> Map.put(:muting_user, user)
114 |> Map.put(:reply_filtering_user, user)
115 |> Map.put(:instance, params[:instance])
116 # Restricts unfederated content to authenticated users
117 |> Map.put(:includes_local_public, not is_nil(user))
118 |> ActivityPub.fetch_public_activities()
119
120 conn
121 |> add_link_headers(activities, %{"local" => local_only})
122 |> render("index.json",
123 activities: activities,
124 for: user,
125 as: :activity,
126 with_muted: Map.get(params, :with_muted, false)
127 )
128 end
129 end
130
131 # GET /api/v1/timelines/bubble
132 def bubble(%{assigns: %{user: user}} = conn, params) do
133 bubble_instances =
134 Enum.uniq(
135 Config.get([:instance, :local_bubble], []) ++
136 [Pleroma.Web.Endpoint.host()]
137 )
138
139 if is_nil(user) do
140 fail_on_bad_auth(conn)
141 else
142 activities =
143 params
144 |> Map.put(:type, ["Create"])
145 |> Map.put(:blocking_user, user)
146 |> Map.put(:muting_user, user)
147 |> Map.put(:reply_filtering_user, user)
148 |> Map.put(:instance, bubble_instances)
149 |> ActivityPub.fetch_public_activities()
150
151 conn
152 |> add_link_headers(activities)
153 |> render("index.json",
154 activities: activities,
155 for: user,
156 as: :activity,
157 with_muted: Map.get(params, :with_muted, false)
158 )
159 end
160 end
161
162 defp fail_on_bad_auth(conn) do
163 render_error(conn, :unauthorized, "authorization required for timeline view")
164 end
165
166 defp hashtag_fetching(params, user, local_only) do
167 # Note: not sanitizing tag options at this stage (may be mix-cased, have duplicates etc.)
168 tags_any =
169 [params[:tag], params[:any]]
170 |> List.flatten()
171 |> Enum.filter(& &1)
172
173 tag_all = Map.get(params, :all, [])
174 tag_reject = Map.get(params, :none, [])
175
176 params
177 |> Map.put(:type, "Create")
178 |> Map.put(:local_only, local_only)
179 |> Map.put(:blocking_user, user)
180 |> Map.put(:muting_user, user)
181 |> Map.put(:user, user)
182 |> Map.put(:tag, tags_any)
183 |> Map.put(:tag_all, tag_all)
184 |> Map.put(:tag_reject, tag_reject)
185 |> ActivityPub.fetch_public_activities()
186 end
187
188 # GET /api/v1/timelines/tag/:tag
189 def hashtag(%{assigns: %{user: user}} = conn, params) do
190 local_only = params[:local]
191
192 if is_nil(user) and restrict_unauthenticated?(local_only) do
193 fail_on_bad_auth(conn)
194 else
195 activities = hashtag_fetching(params, user, local_only)
196
197 conn
198 |> add_link_headers(activities, %{"local" => local_only})
199 |> render("index.json",
200 activities: activities,
201 for: user,
202 as: :activity,
203 with_muted: Map.get(params, :with_muted, false)
204 )
205 end
206 end
207
208 # GET /api/v1/timelines/list/:list_id
209 def list(%{assigns: %{user: user}} = conn, %{list_id: id} = params) do
210 with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
211 params =
212 params
213 |> Map.put(:type, "Create")
214 |> Map.put(:blocking_user, user)
215 |> Map.put(:user, user)
216 |> Map.put(:muting_user, user)
217 |> Map.put(:local_only, params[:local])
218
219 # we must filter the following list for the user to avoid leaking statuses the user
220 # does not actually have permission to see (for more info, peruse security issue #270).
221
222 user_following = User.following(user)
223
224 activities =
225 following
226 |> Enum.filter(fn x -> x in user_following end)
227 |> ActivityPub.fetch_activities_bounded(following, params)
228 |> Enum.reverse()
229
230 conn
231 |> add_link_headers(activities)
232 |> render("index.json",
233 activities: activities,
234 for: user,
235 as: :activity,
236 with_muted: Map.get(params, :with_muted, false)
237 )
238 else
239 _e -> render_error(conn, :forbidden, "Error.")
240 end
241 end
242 end