4b49b74ca4b4393cf9680644989fadfd072071d8
[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.OAuthScopesPlug
16 alias Pleroma.Web.Plugs.RateLimiter
17
18 plug(Pleroma.Web.ApiSpec.CastAndValidate)
19 plug(:skip_public_check when action in [:public, :hashtag])
20
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
23
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)
29
30 plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action in [:home, :direct])
31 plug(OAuthScopesPlug, %{scopes: ["read:lists"]} when action == :list)
32
33 plug(
34 OAuthScopesPlug,
35 %{scopes: ["read:statuses"], fallback: :proceed_unauthenticated}
36 when action in [:public, :hashtag]
37 )
38
39 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TimelineOperation
40
41 # GET /api/v1/timelines/home
42 def home(%{assigns: %{user: user}} = conn, params) do
43 params =
44 params
45 |> Map.put(:type, ["Create", "Announce"])
46 |> Map.put(:blocking_user, user)
47 |> Map.put(:muting_user, user)
48 |> Map.put(:reply_filtering_user, user)
49 |> Map.put(:announce_filtering_user, user)
50 |> Map.put(:user, user)
51 |> Map.put(:local_only, params[:local])
52 |> Map.delete(:local)
53
54 activities =
55 [user.ap_id | User.following(user)]
56 |> ActivityPub.fetch_activities(params)
57 |> Enum.reverse()
58
59 conn
60 |> add_link_headers(activities)
61 |> render("index.json",
62 activities: activities,
63 for: user,
64 as: :activity,
65 with_muted: Map.get(params, :with_muted, false)
66 )
67 end
68
69 # GET /api/v1/timelines/direct
70 def direct(%{assigns: %{user: user}} = conn, params) do
71 params =
72 params
73 |> Map.put(:type, "Create")
74 |> Map.put(:blocking_user, user)
75 |> Map.put(:user, user)
76 |> Map.put(:visibility, "direct")
77
78 activities =
79 [user.ap_id]
80 |> ActivityPub.fetch_activities_query(params)
81 |> Pagination.fetch_paginated(params)
82
83 conn
84 |> add_link_headers(activities)
85 |> render("index.json",
86 activities: activities,
87 for: user,
88 as: :activity
89 )
90 end
91
92 defp restrict_unauthenticated?(true = _local_only) do
93 Config.restrict_unauthenticated_access?(:timelines, :local)
94 end
95
96 defp restrict_unauthenticated?(_) do
97 Config.restrict_unauthenticated_access?(:timelines, :federated)
98 end
99
100 # GET /api/v1/timelines/public
101 def public(%{assigns: %{user: user}} = conn, params) do
102 local_only = params[:local]
103
104 if is_nil(user) and restrict_unauthenticated?(local_only) do
105 fail_on_bad_auth(conn)
106 else
107 activities =
108 params
109 |> Map.put(:type, ["Create"])
110 |> Map.put(:local_only, local_only)
111 |> Map.put(:blocking_user, user)
112 |> Map.put(:muting_user, user)
113 |> Map.put(:reply_filtering_user, user)
114 |> Map.put(:instance, params[:instance])
115 |> ActivityPub.fetch_public_activities()
116
117 conn
118 |> add_link_headers(activities, %{"local" => local_only})
119 |> render("index.json",
120 activities: activities,
121 for: user,
122 as: :activity,
123 with_muted: Map.get(params, :with_muted, false)
124 )
125 end
126 end
127
128 defp fail_on_bad_auth(conn) do
129 render_error(conn, :unauthorized, "authorization required for timeline view")
130 end
131
132 defp hashtag_fetching(params, user, local_only) do
133 # Note: not sanitizing tag options at this stage (may be mix-cased, have duplicates etc.)
134 tags_any =
135 [params[:tag], params[:any]]
136 |> List.flatten()
137 |> Enum.filter(& &1)
138
139 tag_all = Map.get(params, :all, [])
140 tag_reject = Map.get(params, :none, [])
141
142 params
143 |> Map.put(:type, "Create")
144 |> Map.put(:local_only, local_only)
145 |> Map.put(:blocking_user, user)
146 |> Map.put(:muting_user, user)
147 |> Map.put(:user, user)
148 |> Map.put(:tag, tags_any)
149 |> Map.put(:tag_all, tag_all)
150 |> Map.put(:tag_reject, tag_reject)
151 |> ActivityPub.fetch_public_activities()
152 end
153
154 # GET /api/v1/timelines/tag/:tag
155 def hashtag(%{assigns: %{user: user}} = conn, params) do
156 local_only = params[:local]
157
158 if is_nil(user) and restrict_unauthenticated?(local_only) do
159 fail_on_bad_auth(conn)
160 else
161 activities = hashtag_fetching(params, user, local_only)
162
163 conn
164 |> add_link_headers(activities, %{"local" => local_only})
165 |> render("index.json",
166 activities: activities,
167 for: user,
168 as: :activity,
169 with_muted: Map.get(params, :with_muted, false)
170 )
171 end
172 end
173
174 # GET /api/v1/timelines/list/:list_id
175 def list(%{assigns: %{user: user}} = conn, %{list_id: id} = params) do
176 with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
177 params =
178 params
179 |> Map.put(:type, "Create")
180 |> Map.put(:blocking_user, user)
181 |> Map.put(:user, user)
182 |> Map.put(:muting_user, user)
183 |> Map.put(:local_only, params[:local])
184
185 # we must filter the following list for the user to avoid leaking statuses the user
186 # does not actually have permission to see (for more info, peruse security issue #270).
187
188 user_following = User.following(user)
189
190 activities =
191 following
192 |> Enum.filter(fn x -> x in user_following end)
193 |> ActivityPub.fetch_activities_bounded(following, params)
194 |> Enum.reverse()
195
196 render(conn, "index.json",
197 activities: activities,
198 for: user,
199 as: :activity,
200 with_muted: Map.get(params, :with_muted, false)
201 )
202 else
203 _e -> render_error(conn, :forbidden, "Error.")
204 end
205 end
206 end