1734df4b56a9c6db7562134c628a5f61f4dfb7ce
[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.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 plug(Pleroma.Web.ApiSpec.CastAndValidate)
19 plug(:skip_plug, EnsurePublicOrAuthenticatedPlug 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 plug(:put_view, Pleroma.Web.MastodonAPI.StatusView)
40
41 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TimelineOperation
42
43 # GET /api/v1/timelines/home
44 def home(%{assigns: %{user: user}} = conn, params) do
45 params =
46 params
47 |> Map.new(fn {key, value} -> {to_string(key), value} end)
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("user", user)
53
54 recipients = [user.ap_id | User.following(user)]
55
56 activities =
57 recipients
58 |> ActivityPub.fetch_activities(params)
59 |> Enum.reverse()
60
61 conn
62 |> add_link_headers(activities)
63 |> render("index.json",
64 activities: activities,
65 for: user,
66 as: :activity
67 )
68 end
69
70 # GET /api/v1/timelines/direct
71 def direct(%{assigns: %{user: user}} = conn, params) do
72 params =
73 params
74 |> Map.new(fn {key, value} -> {to_string(key), value} end)
75 |> Map.put("type", "Create")
76 |> Map.put("blocking_user", user)
77 |> Map.put("user", user)
78 |> Map.put(:visibility, "direct")
79
80 activities =
81 [user.ap_id]
82 |> ActivityPub.fetch_activities_query(params)
83 |> Pagination.fetch_paginated(params)
84
85 conn
86 |> add_link_headers(activities)
87 |> render("index.json",
88 activities: activities,
89 for: user,
90 as: :activity
91 )
92 end
93
94 # GET /api/v1/timelines/public
95 def public(%{assigns: %{user: user}} = conn, params) do
96 params = Map.new(params, fn {key, value} -> {to_string(key), value} end)
97
98 local_only = params["local"]
99
100 cfg_key =
101 if local_only do
102 :local
103 else
104 :federated
105 end
106
107 restrict? = Pleroma.Config.get([:restrict_unauthenticated, :timelines, cfg_key])
108
109 if restrict? and is_nil(user) do
110 render_error(conn, :unauthorized, "authorization required for timeline view")
111 else
112 # TODO: return back after benchmarks
113 params =
114 params
115 |> Map.put("type", ["Create", "Announce"])
116 |> Map.put("local_only", local_only)
117 |> Map.put("blocking_user", user)
118 |> Map.put("muting_user", user)
119 |> Map.put("reply_filtering_user", user)
120
121 params =
122 if params["method"] do
123 Map.put(params, :method, String.to_existing_atom(params["method"]))
124 else
125 params
126 end
127
128 activities = ActivityPub.fetch_public_activities(params)
129
130 conn
131 |> add_link_headers(activities, %{"local" => local_only})
132 |> render("index.json",
133 activities: activities,
134 for: user,
135 as: :activity
136 )
137 end
138 end
139
140 defp hashtag_fetching(params, user, local_only) do
141 tags =
142 [params["tag"], params["any"]]
143 |> List.flatten()
144 |> Enum.uniq()
145 |> Enum.filter(& &1)
146 |> Enum.map(&String.downcase(&1))
147
148 tag_all =
149 params
150 |> Map.get("all", [])
151 |> Enum.map(&String.downcase(&1))
152
153 tag_reject =
154 params
155 |> Map.get("none", [])
156 |> Enum.map(&String.downcase(&1))
157
158 _activities =
159 params
160 |> Map.put("type", "Create")
161 |> Map.put("local_only", local_only)
162 |> Map.put("blocking_user", user)
163 |> Map.put("muting_user", user)
164 |> Map.put("user", user)
165 |> Map.put("tag", tags)
166 |> Map.put("tag_all", tag_all)
167 |> Map.put("tag_reject", tag_reject)
168 |> ActivityPub.fetch_public_activities()
169 end
170
171 # GET /api/v1/timelines/tag/:tag
172 def hashtag(%{assigns: %{user: user}} = conn, params) do
173 params = Map.new(params, fn {key, value} -> {to_string(key), value} end)
174 local_only = params["local"]
175 activities = hashtag_fetching(params, user, local_only)
176
177 conn
178 |> add_link_headers(activities, %{"local" => local_only})
179 |> render("index.json",
180 activities: activities,
181 for: user,
182 as: :activity
183 )
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.new(fn {key, value} -> {to_string(key), value} end)
192 |> Map.put("type", "Create")
193 |> Map.put("blocking_user", user)
194 |> Map.put("user", user)
195 |> Map.put("muting_user", user)
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 )
213 else
214 _e -> render_error(conn, :forbidden, "Error.")
215 end
216 end
217 end