Merge branch 'stable' into mergeback/2.2.2
[akkoma] / lib / pleroma / web / mastodon_api / controllers / timeline_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.TimelineController do
6 use Pleroma.Web, :controller
7
8 import Pleroma.Web.ControllerHelper,
9 only: [add_link_headers: 2, add_link_headers: 3]
10
11 alias Pleroma.Config
12 alias Pleroma.Pagination
13 alias Pleroma.User
14 alias Pleroma.Web.ActivityPub.ActivityPub
15 alias Pleroma.Web.Plugs.EnsurePublicOrAuthenticatedPlug
16 alias Pleroma.Web.Plugs.OAuthScopesPlug
17 alias Pleroma.Web.Plugs.RateLimiter
18
19 plug(Pleroma.Web.ApiSpec.CastAndValidate)
20 plug(:skip_plug, EnsurePublicOrAuthenticatedPlug when action in [:public, :hashtag])
21
22 # TODO: Replace with a macro when there is a Phoenix release with the following commit in it:
23 # https://github.com/phoenixframework/phoenix/commit/2e8c63c01fec4dde5467dbbbf9705ff9e780735e
24
25 plug(RateLimiter, [name: :timeline, bucket_name: :direct_timeline] when action == :direct)
26 plug(RateLimiter, [name: :timeline, bucket_name: :public_timeline] when action == :public)
27 plug(RateLimiter, [name: :timeline, bucket_name: :home_timeline] when action == :home)
28 plug(RateLimiter, [name: :timeline, bucket_name: :hashtag_timeline] when action == :hashtag)
29 plug(RateLimiter, [name: :timeline, bucket_name: :list_timeline] when action == :list)
30
31 plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action in [:home, :direct])
32 plug(OAuthScopesPlug, %{scopes: ["read:lists"]} when action == :list)
33
34 plug(
35 OAuthScopesPlug,
36 %{scopes: ["read:statuses"], fallback: :proceed_unauthenticated}
37 when action in [:public, :hashtag]
38 )
39
40 plug(:put_view, Pleroma.Web.MastodonAPI.StatusView)
41
42 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TimelineOperation
43
44 # GET /api/v1/timelines/home
45 def home(%{assigns: %{user: user}} = conn, params) do
46 params =
47 params
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(:announce_filtering_user, user)
53 |> Map.put(:user, user)
54
55 activities =
56 [user.ap_id | User.following(user)]
57 |> ActivityPub.fetch_activities(params)
58 |> Enum.reverse()
59
60 conn
61 |> add_link_headers(activities)
62 |> render("index.json",
63 activities: activities,
64 for: user,
65 as: :activity,
66 with_muted: Map.get(params, :with_muted, false)
67 )
68 end
69
70 # GET /api/v1/timelines/direct
71 def direct(%{assigns: %{user: user}} = conn, params) do
72 params =
73 params
74 |> Map.put(:type, "Create")
75 |> Map.put(:blocking_user, user)
76 |> Map.put(:user, user)
77 |> Map.put(:visibility, "direct")
78
79 activities =
80 [user.ap_id]
81 |> ActivityPub.fetch_activities_query(params)
82 |> Pagination.fetch_paginated(params)
83
84 conn
85 |> add_link_headers(activities)
86 |> render("index.json",
87 activities: activities,
88 for: user,
89 as: :activity
90 )
91 end
92
93 defp restrict_unauthenticated?(true = _local_only) do
94 Config.restrict_unauthenticated_access?(:timelines, :local)
95 end
96
97 defp restrict_unauthenticated?(_) do
98 Config.restrict_unauthenticated_access?(:timelines, :federated)
99 end
100
101 # GET /api/v1/timelines/public
102 def public(%{assigns: %{user: user}} = conn, params) do
103 local_only = params[:local]
104
105 if is_nil(user) and restrict_unauthenticated?(local_only) do
106 fail_on_bad_auth(conn)
107 else
108 activities =
109 params
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()
117
118 conn
119 |> add_link_headers(activities, %{"local" => local_only})
120 |> render("index.json",
121 activities: activities,
122 for: user,
123 as: :activity,
124 with_muted: Map.get(params, :with_muted, false)
125 )
126 end
127 end
128
129 defp fail_on_bad_auth(conn) do
130 render_error(conn, :unauthorized, "authorization required for timeline view")
131 end
132
133 defp hashtag_fetching(params, user, local_only) do
134 tags =
135 [params[:tag], params[:any]]
136 |> List.flatten()
137 |> Enum.uniq()
138 |> Enum.reject(&is_nil/1)
139 |> Enum.map(&String.downcase/1)
140
141 tag_all =
142 params
143 |> Map.get(:all, [])
144 |> Enum.map(&String.downcase/1)
145
146 tag_reject =
147 params
148 |> Map.get(:none, [])
149 |> Enum.map(&String.downcase/1)
150
151 _activities =
152 params
153 |> Map.put(:type, "Create")
154 |> Map.put(:local_only, local_only)
155 |> Map.put(:blocking_user, user)
156 |> Map.put(:muting_user, user)
157 |> Map.put(:user, user)
158 |> Map.put(:tag, tags)
159 |> Map.put(:tag_all, tag_all)
160 |> Map.put(:tag_reject, tag_reject)
161 |> ActivityPub.fetch_public_activities()
162 end
163
164 # GET /api/v1/timelines/tag/:tag
165 def hashtag(%{assigns: %{user: user}} = conn, params) do
166 local_only = params[:local]
167
168 if is_nil(user) and restrict_unauthenticated?(local_only) do
169 fail_on_bad_auth(conn)
170 else
171 activities = hashtag_fetching(params, user, local_only)
172
173 conn
174 |> add_link_headers(activities, %{"local" => local_only})
175 |> render("index.json",
176 activities: activities,
177 for: user,
178 as: :activity,
179 with_muted: Map.get(params, :with_muted, false)
180 )
181 end
182 end
183
184 # GET /api/v1/timelines/list/:list_id
185 def list(%{assigns: %{user: user}} = conn, %{list_id: id} = params) do
186 with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
187 params =
188 params
189 |> Map.put(:type, "Create")
190 |> Map.put(:blocking_user, user)
191 |> Map.put(:user, user)
192 |> Map.put(:muting_user, user)
193
194 # we must filter the following list for the user to avoid leaking statuses the user
195 # does not actually have permission to see (for more info, peruse security issue #270).
196
197 user_following = User.following(user)
198
199 activities =
200 following
201 |> Enum.filter(fn x -> x in user_following end)
202 |> ActivityPub.fetch_activities_bounded(following, params)
203 |> Enum.reverse()
204
205 render(conn, "index.json",
206 activities: activities,
207 for: user,
208 as: :activity,
209 with_muted: Map.get(params, :with_muted, false)
210 )
211 else
212 _e -> render_error(conn, :forbidden, "Error.")
213 end
214 end
215 end