Merge branch 'fix/configdb-error' into 'develop'
[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 tags =
137 [params[:tag], params[:any]]
138 |> List.flatten()
139 |> Enum.uniq()
140 |> Enum.reject(&is_nil/1)
141 |> Enum.map(&String.downcase/1)
142
143 tag_all =
144 params
145 |> Map.get(:all, [])
146 |> Enum.map(&String.downcase/1)
147
148 tag_reject =
149 params
150 |> Map.get(:none, [])
151 |> Enum.map(&String.downcase/1)
152
153 _activities =
154 params
155 |> Map.put(:type, "Create")
156 |> Map.put(:local_only, local_only)
157 |> Map.put(:blocking_user, user)
158 |> Map.put(:muting_user, user)
159 |> Map.put(:user, user)
160 |> Map.put(:tag, tags)
161 |> Map.put(:tag_all, tag_all)
162 |> Map.put(:tag_reject, tag_reject)
163 |> ActivityPub.fetch_public_activities()
164 end
165
166 # GET /api/v1/timelines/tag/:tag
167 def hashtag(%{assigns: %{user: user}} = conn, params) do
168 local_only = params[:local]
169
170 if is_nil(user) and restrict_unauthenticated?(local_only) do
171 fail_on_bad_auth(conn)
172 else
173 activities = hashtag_fetching(params, user, local_only)
174
175 conn
176 |> add_link_headers(activities, %{"local" => local_only})
177 |> render("index.json",
178 activities: activities,
179 for: user,
180 as: :activity,
181 with_muted: Map.get(params, :with_muted, false)
182 )
183 end
184 end
185
186 # GET /api/v1/timelines/list/:list_id
187 def list(%{assigns: %{user: user}} = conn, %{list_id: id} = params) do
188 with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
189 params =
190 params
191 |> Map.put(:type, "Create")
192 |> Map.put(:blocking_user, user)
193 |> Map.put(:user, user)
194 |> Map.put(:muting_user, user)
195 |> Map.put(:local_only, params[:local])
196
197 # we must filter the following list for the user to avoid leaking statuses the user
198 # does not actually have permission to see (for more info, peruse security issue #270).
199
200 user_following = User.following(user)
201
202 activities =
203 following
204 |> Enum.filter(fn x -> x in user_following end)
205 |> ActivityPub.fetch_activities_bounded(following, params)
206 |> Enum.reverse()
207
208 render(conn, "index.json",
209 activities: activities,
210 for: user,
211 as: :activity,
212 with_muted: Map.get(params, :with_muted, false)
213 )
214 else
215 _e -> render_error(conn, :forbidden, "Error.")
216 end
217 end
218 end