1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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]
11 alias Pleroma.Pagination
12 alias Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug
13 alias Pleroma.Plugs.OAuthScopesPlug
14 alias Pleroma.Plugs.RateLimiter
16 alias Pleroma.Web.ActivityPub.ActivityPub
18 plug(Pleroma.Web.ApiSpec.CastAndValidate)
19 plug(:skip_plug, EnsurePublicOrAuthenticatedPlug 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)
30 plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action in [:home, :direct])
31 plug(OAuthScopesPlug, %{scopes: ["read:lists"]} when action == :list)
35 %{scopes: ["read:statuses"], fallback: :proceed_unauthenticated}
36 when action in [:public, :hashtag]
39 plug(:put_view, Pleroma.Web.MastodonAPI.StatusView)
41 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TimelineOperation
43 # GET /api/v1/timelines/home
44 def home(%{assigns: %{user: user}} = conn, params) do
47 |> Map.put(:type, ["Create", "Announce"])
48 |> Map.put(:blocking_user, user)
49 |> Map.put(:muting_user, user)
50 |> Map.put(:reply_filtering_user, user)
51 |> Map.put(:announce_filtering_user, user)
52 |> Map.put(:user, user)
55 [user.ap_id | User.following(user)]
56 |> ActivityPub.fetch_activities(params)
60 |> add_link_headers(activities)
61 |> render("index.json",
62 activities: activities,
68 # GET /api/v1/timelines/direct
69 def direct(%{assigns: %{user: user}} = conn, params) do
72 |> Map.put(:type, "Create")
73 |> Map.put(:blocking_user, user)
74 |> Map.put(:user, user)
75 |> Map.put(:visibility, "direct")
79 |> ActivityPub.fetch_activities_query(params)
80 |> Pagination.fetch_paginated(params)
83 |> add_link_headers(activities)
84 |> render("index.json",
85 activities: activities,
91 defp restrict_unauthenticated?(local_only) do
99 Pleroma.Config.get([:restrict_unauthenticated, :timelines, config_key])
102 # GET /api/v1/timelines/public
103 def public(%{assigns: %{user: user}} = conn, params) do
104 local_only = params[:local]
106 if is_nil(user) and restrict_unauthenticated?(local_only) do
107 fail_on_bad_auth(conn)
111 |> Map.put(:type, ["Create"])
112 |> Map.put(:local_only, local_only)
113 |> Map.put(:blocking_user, user)
114 |> Map.put(:muting_user, user)
115 |> Map.put(:reply_filtering_user, user)
116 |> ActivityPub.fetch_public_activities()
119 |> add_link_headers(activities, %{"local" => local_only})
120 |> render("index.json",
121 activities: activities,
128 defp fail_on_bad_auth(conn) do
129 render_error(conn, :unauthorized, "authorization required for timeline view")
132 defp hashtag_fetching(params, user, local_only) do
134 [params[:tag], params[:any]]
137 |> Enum.reject(&is_nil/1)
138 |> Enum.map(&String.downcase/1)
143 |> Enum.map(&String.downcase/1)
147 |> Map.get(:none, [])
148 |> Enum.map(&String.downcase/1)
152 |> Map.put(:type, "Create")
153 |> Map.put(:local_only, local_only)
154 |> Map.put(:blocking_user, user)
155 |> Map.put(:muting_user, user)
156 |> Map.put(:user, user)
157 |> Map.put(:tag, tags)
158 |> Map.put(:tag_all, tag_all)
159 |> Map.put(:tag_reject, tag_reject)
160 |> ActivityPub.fetch_public_activities()
163 # GET /api/v1/timelines/tag/:tag
164 def hashtag(%{assigns: %{user: user}} = conn, params) do
165 local_only = params[:local]
167 if is_nil(user) and restrict_unauthenticated?(local_only) do
168 fail_on_bad_auth(conn)
170 activities = hashtag_fetching(params, user, local_only)
173 |> add_link_headers(activities, %{"local" => local_only})
174 |> render("index.json",
175 activities: activities,
182 # GET /api/v1/timelines/list/:list_id
183 def list(%{assigns: %{user: user}} = conn, %{list_id: id} = params) do
184 with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
187 |> Map.new(fn {key, value} -> {to_string(key), value} end)
188 |> Map.put("type", "Create")
189 |> Map.put("blocking_user", user)
190 |> Map.put("user", user)
191 |> Map.put("muting_user", user)
193 # we must filter the following list for the user to avoid leaking statuses the user
194 # does not actually have permission to see (for more info, peruse security issue #270).
196 user_following = User.following(user)
200 |> Enum.filter(fn x -> x in user_following end)
201 |> ActivityPub.fetch_activities_bounded(following, params)
204 render(conn, "index.json",
205 activities: activities,
210 _e -> render_error(conn, :forbidden, "Error.")