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])
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, :bubble])
32 plug(OAuthScopesPlug, %{scopes: ["read:lists"]} when action == :list)
36 %{scopes: ["read:statuses"], fallback: :proceed_unauthenticated}
37 when action in [:public, :hashtag]
40 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TimelineOperation
42 # GET /api/v1/timelines/home
43 def home(%{assigns: %{user: user}} = conn, params) do
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])
56 [user.ap_id | User.following(user)]
57 |> ActivityPub.fetch_activities(params)
61 |> add_link_headers(activities)
62 |> render("index.json",
63 activities: activities,
66 with_muted: Map.get(params, :with_muted, false)
70 # GET /api/v1/timelines/direct
71 def direct(%{assigns: %{user: user}} = conn, params) do
74 |> Map.put(:type, "Create")
75 |> Map.put(:blocking_user, user)
76 |> Map.put(:user, user)
77 |> Map.put(:visibility, "direct")
81 |> ActivityPub.fetch_activities_query(params)
82 |> Pagination.fetch_paginated(params)
85 |> add_link_headers(activities)
86 |> render("index.json",
87 activities: activities,
93 defp restrict_unauthenticated?(true = _local_only) do
94 Config.restrict_unauthenticated_access?(:timelines, :local)
97 defp restrict_unauthenticated?(_) do
98 Config.restrict_unauthenticated_access?(:timelines, :federated)
101 # GET /api/v1/timelines/public
102 def public(%{assigns: %{user: user}} = conn, params) do
103 local_only = params[:local]
105 if is_nil(user) and restrict_unauthenticated?(local_only) do
106 fail_on_bad_auth(conn)
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 |> ActivityPub.fetch_public_activities()
119 |> add_link_headers(activities, %{"local" => local_only})
120 |> render("index.json",
121 activities: activities,
124 with_muted: Map.get(params, :with_muted, false)
129 # GET /api/v1/timelines/bubble
130 def bubble(%{assigns: %{user: user}} = conn, params) do
131 bubble_instances = Config.get([:instance, :local_bubble], [])
134 fail_on_bad_auth(conn)
138 |> Map.put(:type, ["Create"])
139 |> Map.put(:blocking_user, user)
140 |> Map.put(:muting_user, user)
141 |> Map.put(:reply_filtering_user, user)
142 |> Map.put(:instance, bubble_instances)
143 |> ActivityPub.fetch_public_activities()
146 |> add_link_headers(activities)
147 |> render("index.json",
148 activities: activities,
151 with_muted: Map.get(params, :with_muted, false)
156 defp fail_on_bad_auth(conn) do
157 render_error(conn, :unauthorized, "authorization required for timeline view")
160 defp hashtag_fetching(params, user, local_only) do
161 # Note: not sanitizing tag options at this stage (may be mix-cased, have duplicates etc.)
163 [params[:tag], params[:any]]
167 tag_all = Map.get(params, :all, [])
168 tag_reject = Map.get(params, :none, [])
171 |> Map.put(:type, "Create")
172 |> Map.put(:local_only, local_only)
173 |> Map.put(:blocking_user, user)
174 |> Map.put(:muting_user, user)
175 |> Map.put(:user, user)
176 |> Map.put(:tag, tags_any)
177 |> Map.put(:tag_all, tag_all)
178 |> Map.put(:tag_reject, tag_reject)
179 |> ActivityPub.fetch_public_activities()
182 # GET /api/v1/timelines/tag/:tag
183 def hashtag(%{assigns: %{user: user}} = conn, params) do
184 local_only = params[:local]
186 if is_nil(user) and restrict_unauthenticated?(local_only) do
187 fail_on_bad_auth(conn)
189 activities = hashtag_fetching(params, user, local_only)
192 |> add_link_headers(activities, %{"local" => local_only})
193 |> render("index.json",
194 activities: activities,
197 with_muted: Map.get(params, :with_muted, false)
202 # GET /api/v1/timelines/list/:list_id
203 def list(%{assigns: %{user: user}} = conn, %{list_id: id} = params) do
204 with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
207 |> Map.put(:type, "Create")
208 |> Map.put(:blocking_user, user)
209 |> Map.put(:user, user)
210 |> Map.put(:muting_user, user)
211 |> Map.put(:local_only, params[:local])
213 # we must filter the following list for the user to avoid leaking statuses the user
214 # does not actually have permission to see (for more info, peruse security issue #270).
216 user_following = User.following(user)
220 |> Enum.filter(fn x -> x in user_following end)
221 |> ActivityPub.fetch_activities_bounded(following, params)
225 |> add_link_headers(activities)
226 |> render("index.json",
227 activities: activities,
230 with_muted: Map.get(params, :with_muted, false)
233 _e -> render_error(conn, :forbidden, "Error.")