Merge remote-tracking branch 'remotes/origin/develop' into feature/object-hashtags...
[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 |> Map.put(:local_only, params[:local])
55 |> Map.delete(:local)
56
57 activities =
58 [user.ap_id | User.following(user)]
59 |> ActivityPub.fetch_activities(params)
60 |> Enum.reverse()
61
62 conn
63 |> add_link_headers(activities)
64 |> render("index.json",
65 activities: activities,
66 for: user,
67 as: :activity,
68 with_muted: Map.get(params, :with_muted, false)
69 )
70 end
71
72 # GET /api/v1/timelines/direct
73 def direct(%{assigns: %{user: user}} = conn, params) do
74 params =
75 params
76 |> Map.put(:type, "Create")
77 |> Map.put(:blocking_user, user)
78 |> Map.put(:user, user)
79 |> Map.put(:visibility, "direct")
80
81 activities =
82 [user.ap_id]
83 |> ActivityPub.fetch_activities_query(params)
84 |> Pagination.fetch_paginated(params)
85
86 conn
87 |> add_link_headers(activities)
88 |> render("index.json",
89 activities: activities,
90 for: user,
91 as: :activity
92 )
93 end
94
95 defp restrict_unauthenticated?(true = _local_only) do
96 Config.restrict_unauthenticated_access?(:timelines, :local)
97 end
98
99 defp restrict_unauthenticated?(_) do
100 Config.restrict_unauthenticated_access?(:timelines, :federated)
101 end
102
103 # GET /api/v1/timelines/public
104 def public(%{assigns: %{user: user}} = conn, params) do
105 local_only = params[:local]
106
107 if is_nil(user) and restrict_unauthenticated?(local_only) do
108 fail_on_bad_auth(conn)
109 else
110 activities =
111 params
112 |> Map.put(:type, ["Create"])
113 |> Map.put(:local_only, local_only)
114 |> Map.put(:blocking_user, user)
115 |> Map.put(:muting_user, user)
116 |> Map.put(:reply_filtering_user, user)
117 |> Map.put(:instance, params[:instance])
118 |> ActivityPub.fetch_public_activities()
119
120 conn
121 |> add_link_headers(activities, %{"local" => local_only})
122 |> render("index.json",
123 activities: activities,
124 for: user,
125 as: :activity,
126 with_muted: Map.get(params, :with_muted, false)
127 )
128 end
129 end
130
131 defp fail_on_bad_auth(conn) do
132 render_error(conn, :unauthorized, "authorization required for timeline view")
133 end
134
135 defp hashtag_fetching(params, user, local_only) do
136 # Note: not sanitizing tag options at this stage (may be mix-cased, have duplicates etc.)
137 tags_any =
138 [params[:tag], params[:any]]
139 |> List.flatten()
140 |> Enum.filter(& &1)
141
142 tag_all = Map.get(params, :all, [])
143 tag_reject = Map.get(params, :none, [])
144
145 params
146 |> Map.put(:type, "Create")
147 |> Map.put(:local_only, local_only)
148 |> Map.put(:blocking_user, user)
149 |> Map.put(:muting_user, user)
150 |> Map.put(:user, user)
151 |> Map.put(:tag, tags_any)
152 |> Map.put(:tag_all, tag_all)
153 |> Map.put(:tag_reject, tag_reject)
154 |> ActivityPub.fetch_public_activities()
155 end
156
157 # GET /api/v1/timelines/tag/:tag
158 def hashtag(%{assigns: %{user: user}} = conn, params) do
159 local_only = params[:local]
160
161 if is_nil(user) and restrict_unauthenticated?(local_only) do
162 fail_on_bad_auth(conn)
163 else
164 activities = hashtag_fetching(params, user, local_only)
165
166 conn
167 |> add_link_headers(activities, %{"local" => local_only})
168 |> render("index.json",
169 activities: activities,
170 for: user,
171 as: :activity,
172 with_muted: Map.get(params, :with_muted, false)
173 )
174 end
175 end
176
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
180 params =
181 params
182 |> Map.put(:type, "Create")
183 |> Map.put(:blocking_user, user)
184 |> Map.put(:user, user)
185 |> Map.put(:muting_user, user)
186 |> Map.put(:local_only, params[:local])
187
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).
190
191 user_following = User.following(user)
192
193 activities =
194 following
195 |> Enum.filter(fn x -> x in user_following end)
196 |> ActivityPub.fetch_activities_bounded(following, params)
197 |> Enum.reverse()
198
199 render(conn, "index.json",
200 activities: activities,
201 for: user,
202 as: :activity,
203 with_muted: Map.get(params, :with_muted, false)
204 )
205 else
206 _e -> render_error(conn, :forbidden, "Error.")
207 end
208 end
209 end