Merge branch 'develop' into 'remove-twitter-api'
[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, 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 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 skip_relationships: skip_relationships?(params)
68 )
69 end
70
71 # GET /api/v1/timelines/direct
72 def direct(%{assigns: %{user: user}} = conn, params) do
73 params =
74 params
75 |> Map.new(fn {key, value} -> {to_string(key), value} end)
76 |> Map.put("type", "Create")
77 |> Map.put("blocking_user", user)
78 |> Map.put("user", user)
79 |> Map.put(:visibility, "direct")
80
81 activities =
82 [user.ap_id]
83 |> ActivityPub.fetch_activities_query(params)
84 |> Pagination.fetch_paginated(params)
85
86 conn
87 |> add_link_headers(activities)
88 |> render("index.json",
89 activities: activities,
90 for: user,
91 as: :activity,
92 skip_relationships: skip_relationships?(params)
93 )
94 end
95
96 # GET /api/v1/timelines/public
97 def public(%{assigns: %{user: user}} = conn, params) do
98 params = Map.new(params, fn {key, value} -> {to_string(key), value} end)
99
100 local_only = params["local"]
101
102 cfg_key =
103 if local_only do
104 :local
105 else
106 :federated
107 end
108
109 restrict? = Pleroma.Config.get([:restrict_unauthenticated, :timelines, cfg_key])
110
111 if restrict? and is_nil(user) do
112 render_error(conn, :unauthorized, "authorization required for timeline view")
113 else
114 activities =
115 params
116 |> Map.put("type", ["Create", "Announce"])
117 |> Map.put("local_only", local_only)
118 |> Map.put("blocking_user", user)
119 |> Map.put("muting_user", user)
120 |> Map.put("reply_filtering_user", user)
121 |> ActivityPub.fetch_public_activities()
122
123 conn
124 |> add_link_headers(activities, %{"local" => local_only})
125 |> render("index.json",
126 activities: activities,
127 for: user,
128 as: :activity,
129 skip_relationships: skip_relationships?(params)
130 )
131 end
132 end
133
134 defp hashtag_fetching(params, user, local_only) do
135 tags =
136 [params["tag"], params["any"]]
137 |> List.flatten()
138 |> Enum.uniq()
139 |> Enum.filter(& &1)
140 |> Enum.map(&String.downcase(&1))
141
142 tag_all =
143 params
144 |> Map.get("all", [])
145 |> Enum.map(&String.downcase(&1))
146
147 tag_reject =
148 params
149 |> Map.get("none", [])
150 |> Enum.map(&String.downcase(&1))
151
152 _activities =
153 params
154 |> Map.put("type", "Create")
155 |> Map.put("local_only", local_only)
156 |> Map.put("blocking_user", user)
157 |> Map.put("muting_user", user)
158 |> Map.put("user", user)
159 |> Map.put("tag", tags)
160 |> Map.put("tag_all", tag_all)
161 |> Map.put("tag_reject", tag_reject)
162 |> ActivityPub.fetch_public_activities()
163 end
164
165 # GET /api/v1/timelines/tag/:tag
166 def hashtag(%{assigns: %{user: user}} = conn, params) do
167 params = Map.new(params, fn {key, value} -> {to_string(key), value} end)
168 local_only = params["local"]
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 skip_relationships: skip_relationships?(params)
178 )
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.new(fn {key, value} -> {to_string(key), value} end)
187 |> Map.put("type", "Create")
188 |> Map.put("blocking_user", user)
189 |> Map.put("user", user)
190 |> Map.put("muting_user", user)
191
192 # we must filter the following list for the user to avoid leaking statuses the user
193 # does not actually have permission to see (for more info, peruse security issue #270).
194
195 user_following = User.following(user)
196
197 activities =
198 following
199 |> Enum.filter(fn x -> x in user_following end)
200 |> ActivityPub.fetch_activities_bounded(following, params)
201 |> Enum.reverse()
202
203 render(conn, "index.json",
204 activities: activities,
205 for: user,
206 as: :activity,
207 skip_relationships: skip_relationships?(params)
208 )
209 else
210 _e -> render_error(conn, :forbidden, "Error.")
211 end
212 end
213 end