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.new(fn {key, value} -> {to_string(key), value} end)
48 |> Map.put("type", ["Create", "Announce"])
49 |> Map.put("blocking_user", user)
50 |> Map.put("muting_user", user)
51 |> Map.put("reply_filtering_user", user)
52 |> Map.put("user", user)
54 recipients = [user.ap_id | User.following(user)]
58 |> ActivityPub.fetch_activities(params)
62 |> add_link_headers(activities)
63 |> render("index.json",
64 activities: activities,
70 # GET /api/v1/timelines/direct
71 def direct(%{assigns: %{user: user}} = conn, params) do
74 |> Map.new(fn {key, value} -> {to_string(key), value} end)
75 |> Map.put("type", "Create")
76 |> Map.put("blocking_user", user)
77 |> Map.put("user", user)
78 |> Map.put(:visibility, "direct")
82 |> ActivityPub.fetch_activities_query(params)
83 |> Pagination.fetch_paginated(params)
86 |> add_link_headers(activities)
87 |> render("index.json",
88 activities: activities,
94 # GET /api/v1/timelines/public
95 def public(%{assigns: %{user: user}} = conn, params) do
96 params = Map.new(params, fn {key, value} -> {to_string(key), value} end)
98 local_only = params["local"]
107 restrict? = Pleroma.Config.get([:restrict_unauthenticated, :timelines, cfg_key])
109 if restrict? and is_nil(user) do
110 render_error(conn, :unauthorized, "authorization required for timeline view")
114 |> Map.put("type", ["Create", "Announce"])
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 |> ActivityPub.fetch_public_activities()
122 |> add_link_headers(activities, %{"local" => local_only})
123 |> render("index.json",
124 activities: activities,
131 defp hashtag_fetching(params, user, local_only) do
133 [params["tag"], params["any"]]
137 |> Enum.map(&String.downcase(&1))
141 |> Map.get("all", [])
142 |> Enum.map(&String.downcase(&1))
146 |> Map.get("none", [])
147 |> Enum.map(&String.downcase(&1))
151 |> Map.put("type", "Create")
152 |> Map.put("local_only", local_only)
153 |> Map.put("blocking_user", user)
154 |> Map.put("muting_user", user)
155 |> Map.put("user", user)
156 |> Map.put("tag", tags)
157 |> Map.put("tag_all", tag_all)
158 |> Map.put("tag_reject", tag_reject)
159 |> ActivityPub.fetch_public_activities()
162 # GET /api/v1/timelines/tag/:tag
163 def hashtag(%{assigns: %{user: user}} = conn, params) do
164 params = Map.new(params, fn {key, value} -> {to_string(key), value} end)
165 local_only = params["local"]
166 activities = hashtag_fetching(params, user, local_only)
169 |> add_link_headers(activities, %{"local" => local_only})
170 |> render("index.json",
171 activities: activities,
177 # GET /api/v1/timelines/list/:list_id
178 def list(%{assigns: %{user: user}} = conn, %{list_id: id} = params) do
179 with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
182 |> Map.new(fn {key, value} -> {to_string(key), value} end)
183 |> Map.put("type", "Create")
184 |> Map.put("blocking_user", user)
185 |> Map.put("user", user)
186 |> Map.put("muting_user", user)
188 # we must filter the following list for the user to avoid leaking statuses the user
189 # does not actually have permission to see (for more info, peruse security issue #270).
191 user_following = User.following(user)
195 |> Enum.filter(fn x -> x in user_following end)
196 |> ActivityPub.fetch_activities_bounded(following, params)
199 render(conn, "index.json",
200 activities: activities,
205 _e -> render_error(conn, :forbidden, "Error.")