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]
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 |> User.followed_hashtags()
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)
62 [user.ap_id | User.following(user)]
63 |> ActivityPub.fetch_activities(params)
67 |> add_link_headers(activities)
68 |> render("index.json",
69 activities: activities,
72 with_muted: Map.get(params, :with_muted, false)
76 # GET /api/v1/timelines/direct
77 def direct(%{assigns: %{user: user}} = conn, params) do
80 |> Map.put(:type, "Create")
81 |> Map.put(:blocking_user, user)
82 |> Map.put(:user, user)
83 |> Map.put(:visibility, "direct")
87 |> ActivityPub.fetch_activities_query(params)
88 |> Pagination.fetch_paginated(params)
91 |> add_link_headers(activities)
92 |> render("index.json",
93 activities: activities,
99 defp restrict_unauthenticated?(type) do
100 Config.restrict_unauthenticated_access?(:timelines, type)
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
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
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()
125 |> add_link_headers(activities, %{"local" => local_only})
126 |> render("index.json",
127 activities: activities,
130 with_muted: Map.get(params, :with_muted, false)
136 |> json(%{error: "Federated timeline is disabled"})
138 {:authenticated, false} ->
139 fail_on_bad_auth(conn)
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)
150 Config.get([:instance, :local_bubble], []) ++
151 [Pleroma.Web.Endpoint.host()]
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()
164 |> add_link_headers(activities)
165 |> render("index.json",
166 activities: activities,
169 with_muted: Map.get(params, :with_muted, false)
174 defp fail_on_bad_auth(conn) do
175 render_error(conn, :unauthorized, "authorization required for timeline view")
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.)
181 [params[:tag], params[:any]]
185 tag_all = Map.get(params, :all, [])
186 tag_reject = Map.get(params, :none, [])
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()
200 # GET /api/v1/timelines/tag/:tag
201 def hashtag(%{assigns: %{user: user}} = conn, params) do
202 local_only = params[:local]
204 if is_nil(user) and restrict_unauthenticated?(if local_only, do: :local, else: :federated) do
205 fail_on_bad_auth(conn)
207 activities = hashtag_fetching(params, user, local_only)
210 |> add_link_headers(activities, %{"local" => local_only})
211 |> render("index.json",
212 activities: activities,
215 with_muted: Map.get(params, :with_muted, false)
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
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])
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).
234 user_following = User.following(user)
238 |> Enum.filter(fn x -> x in user_following end)
239 |> ActivityPub.fetch_activities_bounded(following, params)
243 |> add_link_headers(activities)
244 |> render("index.json",
245 activities: activities,
248 with_muted: Map.get(params, :with_muted, false)
251 _e -> render_error(conn, :forbidden, "Error.")