Merge branch 'restrict-domain' into 'develop'
[akkoma] / lib / pleroma / web / mastodon_api / controllers / timeline_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 )
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 )
124 end
125 end
126
127 defp fail_on_bad_auth(conn) do
128 render_error(conn, :unauthorized, "authorization required for timeline view")
129 end
130
131 defp hashtag_fetching(params, user, local_only) do
132 tags =
133 [params[:tag], params[:any]]
134 |> List.flatten()
135 |> Enum.uniq()
136 |> Enum.reject(&is_nil/1)
137 |> Enum.map(&String.downcase/1)
138
139 tag_all =
140 params
141 |> Map.get(:all, [])
142 |> Enum.map(&String.downcase/1)
143
144 tag_reject =
145 params
146 |> Map.get(:none, [])
147 |> Enum.map(&String.downcase/1)
148
149 _activities =
150 params
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()
160 end
161
162 # GET /api/v1/timelines/tag/:tag
163 def hashtag(%{assigns: %{user: user}} = conn, params) do
164 local_only = params[:local]
165
166 if is_nil(user) and restrict_unauthenticated?(local_only) do
167 fail_on_bad_auth(conn)
168 else
169 activities = hashtag_fetching(params, user, local_only)
170
171 conn
172 |> add_link_headers(activities, %{"local" => local_only})
173 |> render("index.json",
174 activities: activities,
175 for: user,
176 as: :activity
177 )
178 end
179 end
180
181 # GET /api/v1/timelines/list/:list_id
182 def list(%{assigns: %{user: user}} = conn, %{list_id: id} = params) do
183 with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
184 params =
185 params
186 |> Map.put(:type, "Create")
187 |> Map.put(:blocking_user, user)
188 |> Map.put(:user, user)
189 |> Map.put(:muting_user, user)
190
191 # we must filter the following list for the user to avoid leaking statuses the user
192 # does not actually have permission to see (for more info, peruse security issue #270).
193
194 user_following = User.following(user)
195
196 activities =
197 following
198 |> Enum.filter(fn x -> x in user_following end)
199 |> ActivityPub.fetch_activities_bounded(following, params)
200 |> Enum.reverse()
201
202 render(conn, "index.json",
203 activities: activities,
204 for: user,
205 as: :activity
206 )
207 else
208 _e -> render_error(conn, :forbidden, "Error.")
209 end
210 end
211 end