Fixed OAuth restrictions for :api routes. Made auth info dropped for :api routes...
[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, truthy_param?: 1, skip_relationships?: 1]
10
11 alias Pleroma.Pagination
12 alias Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug
13 alias Pleroma.Plugs.OAuthScopesPlug
14 alias Pleroma.Plugs.RateLimiter
15 alias Pleroma.User
16 alias Pleroma.Web.ActivityPub.ActivityPub
17
18 # TODO: Replace with a macro when there is a Phoenix release with the following commit in it:
19 # https://github.com/phoenixframework/phoenix/commit/2e8c63c01fec4dde5467dbbbf9705ff9e780735e
20
21 plug(RateLimiter, [name: :timeline, bucket_name: :direct_timeline] when action == :direct)
22 plug(RateLimiter, [name: :timeline, bucket_name: :public_timeline] when action == :public)
23 plug(RateLimiter, [name: :timeline, bucket_name: :home_timeline] when action == :home)
24 plug(RateLimiter, [name: :timeline, bucket_name: :hashtag_timeline] when action == :hashtag)
25 plug(RateLimiter, [name: :timeline, bucket_name: :list_timeline] when action == :list)
26
27 plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action in [:home, :direct])
28 plug(OAuthScopesPlug, %{scopes: ["read:lists"]} when action == :list)
29
30 plug(
31 OAuthScopesPlug,
32 %{scopes: ["read:statuses"], fallback: :proceed_unauthenticated}
33 when action in [:public, :hashtag]
34 )
35
36 plug(:skip_plug, EnsurePublicOrAuthenticatedPlug when action in [:public, :hashtag])
37
38 plug(:put_view, Pleroma.Web.MastodonAPI.StatusView)
39
40 # GET /api/v1/timelines/home
41 def home(%{assigns: %{user: user}} = conn, params) do
42 params =
43 params
44 |> Map.put("type", ["Create", "Announce"])
45 |> Map.put("blocking_user", user)
46 |> Map.put("muting_user", user)
47 |> Map.put("user", user)
48
49 recipients = [user.ap_id | User.following(user)]
50
51 activities =
52 recipients
53 |> ActivityPub.fetch_activities(params)
54 |> Enum.reverse()
55
56 conn
57 |> add_link_headers(activities)
58 |> render("index.json",
59 activities: activities,
60 for: user,
61 as: :activity,
62 skip_relationships: skip_relationships?(params)
63 )
64 end
65
66 # GET /api/v1/timelines/direct
67 def direct(%{assigns: %{user: user}} = conn, params) do
68 params =
69 params
70 |> Map.put("type", "Create")
71 |> Map.put("blocking_user", user)
72 |> Map.put("user", user)
73 |> Map.put(:visibility, "direct")
74
75 activities =
76 [user.ap_id]
77 |> ActivityPub.fetch_activities_query(params)
78 |> Pagination.fetch_paginated(params)
79
80 conn
81 |> add_link_headers(activities)
82 |> render("index.json",
83 activities: activities,
84 for: user,
85 as: :activity,
86 skip_relationships: skip_relationships?(params)
87 )
88 end
89
90 # GET /api/v1/timelines/public
91 def public(%{assigns: %{user: user}} = conn, params) do
92 local_only = truthy_param?(params["local"])
93
94 cfg_key =
95 if local_only do
96 :local
97 else
98 :federated
99 end
100
101 restrict? = Pleroma.Config.get([:restrict_unauthenticated, :timelines, cfg_key])
102
103 if restrict? and is_nil(user) do
104 render_error(conn, :unauthorized, "authorization required for timeline view")
105 else
106 activities =
107 params
108 |> Map.put("type", ["Create", "Announce"])
109 |> Map.put("local_only", local_only)
110 |> Map.put("blocking_user", user)
111 |> Map.put("muting_user", user)
112 |> ActivityPub.fetch_public_activities()
113
114 conn
115 |> add_link_headers(activities, %{"local" => local_only})
116 |> render("index.json",
117 activities: activities,
118 for: user,
119 as: :activity,
120 skip_relationships: skip_relationships?(params)
121 )
122 end
123 end
124
125 defp hashtag_fetching(params, user, local_only) do
126 tags =
127 [params["tag"], params["any"]]
128 |> List.flatten()
129 |> Enum.uniq()
130 |> Enum.filter(& &1)
131 |> Enum.map(&String.downcase(&1))
132
133 tag_all =
134 params
135 |> Map.get("all", [])
136 |> Enum.map(&String.downcase(&1))
137
138 tag_reject =
139 params
140 |> Map.get("none", [])
141 |> Enum.map(&String.downcase(&1))
142
143 _activities =
144 params
145 |> Map.put("type", "Create")
146 |> Map.put("local_only", local_only)
147 |> Map.put("blocking_user", user)
148 |> Map.put("muting_user", user)
149 |> Map.put("user", user)
150 |> Map.put("tag", tags)
151 |> Map.put("tag_all", tag_all)
152 |> Map.put("tag_reject", tag_reject)
153 |> ActivityPub.fetch_public_activities()
154 end
155
156 # GET /api/v1/timelines/tag/:tag
157 def hashtag(%{assigns: %{user: user}} = conn, params) do
158 local_only = truthy_param?(params["local"])
159
160 activities = hashtag_fetching(params, user, local_only)
161
162 conn
163 |> add_link_headers(activities, %{"local" => local_only})
164 |> render("index.json",
165 activities: activities,
166 for: user,
167 as: :activity,
168 skip_relationships: skip_relationships?(params)
169 )
170 end
171
172 # GET /api/v1/timelines/list/:list_id
173 def list(%{assigns: %{user: user}} = conn, %{"list_id" => id} = params) do
174 with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
175 params =
176 params
177 |> Map.put("type", "Create")
178 |> Map.put("blocking_user", user)
179 |> Map.put("user", user)
180 |> Map.put("muting_user", user)
181
182 # we must filter the following list for the user to avoid leaking statuses the user
183 # does not actually have permission to see (for more info, peruse security issue #270).
184
185 user_following = User.following(user)
186
187 activities =
188 following
189 |> Enum.filter(fn x -> x in user_following end)
190 |> ActivityPub.fetch_activities_bounded(following, params)
191 |> Enum.reverse()
192
193 render(conn, "index.json",
194 activities: activities,
195 for: user,
196 as: :activity,
197 skip_relationships: skip_relationships?(params)
198 )
199 else
200 _e -> render_error(conn, :forbidden, "Error.")
201 end
202 end
203 end