1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.TimelineController do
6 use Pleroma.Web, :controller
8 import Pleroma.Web.ControllerHelper,
9 only: [add_link_headers: 2, add_link_headers: 3]
12 alias Pleroma.Pagination
14 alias Pleroma.Web.ActivityPub.ActivityPub
15 alias Pleroma.Web.Plugs.OAuthScopesPlug
16 alias Pleroma.Web.Plugs.RateLimiter
18 plug(Pleroma.Web.ApiSpec.CastAndValidate)
19 plug(:skip_public_check when action in [:public, :hashtag, :bubble])
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
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)
31 plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action in [:home, :direct])
32 plug(OAuthScopesPlug, %{scopes: ["read:lists"]} when action == :list)
36 %{scopes: ["read:statuses"], fallback: :proceed_unauthenticated}
37 when action in [:public, :hashtag, :bubble]
42 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TimelineOperation
44 # GET /api/v1/timelines/home
45 def home(%{assigns: %{user: user}} = conn, params) do
46 %{nickname: nickname} = user
48 Logger.debug("TimelineController.home: #{nickname}")
52 |> User.followed_hashtags()
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)
67 Logger.debug("TimelineController.home: #{nickname} - fetching activities")
70 [user.ap_id | User.following(user)]
71 |> ActivityPub.fetch_activities(params)
74 Logger.debug("TimelineController.home: #{nickname} - rendering")
77 |> add_link_headers(activities)
78 |> render("index.json",
79 activities: activities,
82 with_muted: Map.get(params, :with_muted, false)
86 # GET /api/v1/timelines/direct
87 def direct(%{assigns: %{user: user}} = conn, params) do
88 Logger.debug("TimelineController.direct: #{user.nickname}")
92 |> Map.put(:type, "Create")
93 |> Map.put(:blocking_user, user)
94 |> Map.put(:user, user)
95 |> Map.put(:visibility, "direct")
97 Logger.debug("TimelineController.direct: #{user.nickname} - fetching activities")
101 |> ActivityPub.fetch_activities_query(params)
102 |> Pagination.fetch_paginated(params)
104 Logger.debug("TimelineController.direct: #{user.nickname} - rendering")
107 |> add_link_headers(activities)
108 |> render("index.json",
109 activities: activities,
115 defp restrict_unauthenticated?(type) do
116 Config.restrict_unauthenticated_access?(:timelines, type)
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
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")
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()
143 Logger.debug("TimelineController.public: rendering")
146 |> add_link_headers(activities, %{"local" => local_only})
147 |> render("index.json",
148 activities: activities,
151 with_muted: Map.get(params, :with_muted, false)
157 |> json(%{error: "Federated timeline is disabled"})
159 {:authenticated, false} ->
160 fail_on_bad_auth(conn)
164 # GET /api/v1/timelines/bubble
165 def bubble(%{assigns: %{user: user}} = conn, params) do
166 Logger.debug("TimelineController.bubble")
168 if is_nil(user) and restrict_unauthenticated?(:bubble) do
169 fail_on_bad_auth(conn)
173 Config.get([:instance, :local_bubble], []) ++
174 [Pleroma.Web.Endpoint.host()]
177 Logger.debug("TimelineController.bubble: fetching activities")
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()
188 Logger.debug("TimelineController.bubble: rendering")
191 |> add_link_headers(activities)
192 |> render("index.json",
193 activities: activities,
196 with_muted: Map.get(params, :with_muted, false)
201 defp fail_on_bad_auth(conn) do
202 render_error(conn, :unauthorized, "authorization required for timeline view")
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.)
208 [params[:tag], params[:any]]
212 tag_all = Map.get(params, :all, [])
213 tag_reject = Map.get(params, :none, [])
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()
227 # GET /api/v1/timelines/tag/:tag
228 def hashtag(%{assigns: %{user: user}} = conn, params) do
229 local_only = params[:local]
231 if is_nil(user) and restrict_unauthenticated?(if local_only, do: :local, else: :federated) do
232 fail_on_bad_auth(conn)
234 activities = hashtag_fetching(params, user, local_only)
237 |> add_link_headers(activities, %{"local" => local_only})
238 |> render("index.json",
239 activities: activities,
242 with_muted: Map.get(params, :with_muted, false)
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
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])
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).
261 user_following = User.following(user)
265 |> Enum.filter(fn x -> x in user_following end)
266 |> ActivityPub.fetch_activities_bounded(following, params)
270 |> add_link_headers(activities)
271 |> render("index.json",
272 activities: activities,
275 with_muted: Map.get(params, :with_muted, false)
278 _e -> render_error(conn, :forbidden, "Error.")