Merge branch 'features/apc2s-pagination' 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.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 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 )
66 end
67
68 # GET /api/v1/timelines/direct
69 def direct(%{assigns: %{user: user}} = conn, params) do
70 params =
71 params
72 |> Map.new(fn {key, value} -> {to_string(key), value} end)
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 # GET /api/v1/timelines/public
93 def public(%{assigns: %{user: user}} = conn, params) do
94 params = Map.new(params, fn {key, value} -> {to_string(key), value} end)
95
96 local_only = params["local"]
97
98 cfg_key =
99 if local_only do
100 :local
101 else
102 :federated
103 end
104
105 restrict? = Pleroma.Config.get([:restrict_unauthenticated, :timelines, cfg_key])
106
107 if restrict? and is_nil(user) do
108 render_error(conn, :unauthorized, "authorization required for timeline view")
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 |> ActivityPub.fetch_public_activities()
118
119 conn
120 |> add_link_headers(activities, %{"local" => local_only})
121 |> render("index.json",
122 activities: activities,
123 for: user,
124 as: :activity
125 )
126 end
127 end
128
129 defp hashtag_fetching(params, user, local_only) do
130 tags =
131 [params["tag"], params["any"]]
132 |> List.flatten()
133 |> Enum.uniq()
134 |> Enum.filter(& &1)
135 |> Enum.map(&String.downcase(&1))
136
137 tag_all =
138 params
139 |> Map.get("all", [])
140 |> Enum.map(&String.downcase(&1))
141
142 tag_reject =
143 params
144 |> Map.get("none", [])
145 |> Enum.map(&String.downcase(&1))
146
147 _activities =
148 params
149 |> Map.put("type", "Create")
150 |> Map.put("local_only", local_only)
151 |> Map.put("blocking_user", user)
152 |> Map.put("muting_user", user)
153 |> Map.put("user", user)
154 |> Map.put("tag", tags)
155 |> Map.put("tag_all", tag_all)
156 |> Map.put("tag_reject", tag_reject)
157 |> ActivityPub.fetch_public_activities()
158 end
159
160 # GET /api/v1/timelines/tag/:tag
161 def hashtag(%{assigns: %{user: user}} = conn, params) do
162 params = Map.new(params, fn {key, value} -> {to_string(key), value} end)
163 local_only = params["local"]
164 activities = hashtag_fetching(params, user, local_only)
165
166 conn
167 |> add_link_headers(activities, %{"local" => local_only})
168 |> render("index.json",
169 activities: activities,
170 for: user,
171 as: :activity
172 )
173 end
174
175 # GET /api/v1/timelines/list/:list_id
176 def list(%{assigns: %{user: user}} = conn, %{list_id: id} = params) do
177 with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
178 params =
179 params
180 |> Map.new(fn {key, value} -> {to_string(key), value} end)
181 |> Map.put("type", "Create")
182 |> Map.put("blocking_user", user)
183 |> Map.put("user", user)
184 |> Map.put("muting_user", user)
185
186 # we must filter the following list for the user to avoid leaking statuses the user
187 # does not actually have permission to see (for more info, peruse security issue #270).
188
189 user_following = User.following(user)
190
191 activities =
192 following
193 |> Enum.filter(fn x -> x in user_following end)
194 |> ActivityPub.fetch_activities_bounded(following, params)
195 |> Enum.reverse()
196
197 render(conn, "index.json",
198 activities: activities,
199 for: user,
200 as: :activity
201 )
202 else
203 _e -> render_error(conn, :forbidden, "Error.")
204 end
205 end
206 end