2d0e36420f7a90c6007acd255e73fb467ddedda8
[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 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?(true = _local_only) do
100 Config.restrict_unauthenticated_access?(:timelines, :local)
101 end
102
103 defp restrict_unauthenticated?(_) do
104 Config.restrict_unauthenticated_access?(:timelines, :federated)
105 end
106
107 # GET /api/v1/timelines/public
108 def public(%{assigns: %{user: user}} = conn, params) do
109 local_only = params[:local]
110
111 if is_nil(user) and restrict_unauthenticated?(local_only) do
112 fail_on_bad_auth(conn)
113 else
114 activities =
115 params
116 |> Map.put(:type, ["Create"])
117 |> Map.put(:local_only, local_only)
118 |> Map.put(:blocking_user, user)
119 |> Map.put(:muting_user, user)
120 |> Map.put(:reply_filtering_user, user)
121 |> Map.put(:instance, params[:instance])
122 # Restricts unfederated content to authenticated users
123 |> Map.put(:includes_local_public, not is_nil(user))
124 |> ActivityPub.fetch_public_activities()
125
126 conn
127 |> add_link_headers(activities, %{"local" => local_only})
128 |> render("index.json",
129 activities: activities,
130 for: user,
131 as: :activity,
132 with_muted: Map.get(params, :with_muted, false)
133 )
134 end
135 end
136
137 # GET /api/v1/timelines/bubble
138 def bubble(%{assigns: %{user: user}} = conn, params) do
139 bubble_instances =
140 Enum.uniq(
141 Config.get([:instance, :local_bubble], []) ++
142 [Pleroma.Web.Endpoint.host()]
143 )
144
145 if is_nil(user) do
146 fail_on_bad_auth(conn)
147 else
148 activities =
149 params
150 |> Map.put(:type, ["Create"])
151 |> Map.put(:blocking_user, user)
152 |> Map.put(:muting_user, user)
153 |> Map.put(:reply_filtering_user, user)
154 |> Map.put(:instance, bubble_instances)
155 |> ActivityPub.fetch_public_activities()
156
157 conn
158 |> add_link_headers(activities)
159 |> render("index.json",
160 activities: activities,
161 for: user,
162 as: :activity,
163 with_muted: Map.get(params, :with_muted, false)
164 )
165 end
166 end
167
168 defp fail_on_bad_auth(conn) do
169 render_error(conn, :unauthorized, "authorization required for timeline view")
170 end
171
172 defp hashtag_fetching(params, user, local_only) do
173 # Note: not sanitizing tag options at this stage (may be mix-cased, have duplicates etc.)
174 tags_any =
175 [params[:tag], params[:any]]
176 |> List.flatten()
177 |> Enum.filter(& &1)
178
179 tag_all = Map.get(params, :all, [])
180 tag_reject = Map.get(params, :none, [])
181
182 params
183 |> Map.put(:type, "Create")
184 |> Map.put(:local_only, local_only)
185 |> Map.put(:blocking_user, user)
186 |> Map.put(:muting_user, user)
187 |> Map.put(:user, user)
188 |> Map.put(:tag, tags_any)
189 |> Map.put(:tag_all, tag_all)
190 |> Map.put(:tag_reject, tag_reject)
191 |> ActivityPub.fetch_public_activities()
192 end
193
194 # GET /api/v1/timelines/tag/:tag
195 def hashtag(%{assigns: %{user: user}} = conn, params) do
196 local_only = params[:local]
197
198 if is_nil(user) and restrict_unauthenticated?(local_only) do
199 fail_on_bad_auth(conn)
200 else
201 activities = hashtag_fetching(params, user, local_only)
202
203 conn
204 |> add_link_headers(activities, %{"local" => local_only})
205 |> render("index.json",
206 activities: activities,
207 for: user,
208 as: :activity,
209 with_muted: Map.get(params, :with_muted, false)
210 )
211 end
212 end
213
214 # GET /api/v1/timelines/list/:list_id
215 def list(%{assigns: %{user: user}} = conn, %{list_id: id} = params) do
216 with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
217 params =
218 params
219 |> Map.put(:type, "Create")
220 |> Map.put(:blocking_user, user)
221 |> Map.put(:user, user)
222 |> Map.put(:muting_user, user)
223 |> Map.put(:local_only, params[:local])
224
225 # we must filter the following list for the user to avoid leaking statuses the user
226 # does not actually have permission to see (for more info, peruse security issue #270).
227
228 user_following = User.following(user)
229
230 activities =
231 following
232 |> Enum.filter(fn x -> x in user_following end)
233 |> ActivityPub.fetch_activities_bounded(following, params)
234 |> Enum.reverse()
235
236 conn
237 |> add_link_headers(activities)
238 |> render("index.json",
239 activities: activities,
240 for: user,
241 as: :activity,
242 with_muted: Map.get(params, :with_muted, false)
243 )
244 else
245 _e -> render_error(conn, :forbidden, "Error.")
246 end
247 end
248 end