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