ActivityPub: Add new 'capabilities' to user.
[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.put(:type, ["Create", "Announce"])
48 |> Map.put(:blocking_user, user)
49 |> Map.put(:muting_user, user)
50 |> Map.put(:reply_filtering_user, user)
51 |> Map.put(:announce_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.put(:type, "Create")
73 |> Map.put(:blocking_user, user)
74 |> Map.put(:user, user)
75 |> Map.put(:visibility, "direct")
76
77 activities =
78 [user.ap_id]
79 |> ActivityPub.fetch_activities_query(params)
80 |> Pagination.fetch_paginated(params)
81
82 conn
83 |> add_link_headers(activities)
84 |> render("index.json",
85 activities: activities,
86 for: user,
87 as: :activity
88 )
89 end
90
91 # GET /api/v1/timelines/public
92 def public(%{assigns: %{user: user}} = conn, params) do
93 local_only = params[:local]
94
95 cfg_key =
96 if local_only do
97 :local
98 else
99 :federated
100 end
101
102 restrict? = Pleroma.Config.get([:restrict_unauthenticated, :timelines, cfg_key])
103
104 if restrict? and is_nil(user) do
105 render_error(conn, :unauthorized, "authorization required for timeline view")
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 |> ActivityPub.fetch_public_activities()
115
116 conn
117 |> add_link_headers(activities, %{"local" => local_only})
118 |> render("index.json",
119 activities: activities,
120 for: user,
121 as: :activity
122 )
123 end
124 end
125
126 defp hashtag_fetching(params, user, local_only) do
127 tags =
128 [params[:tag], params[:any]]
129 |> List.flatten()
130 |> Enum.uniq()
131 |> Enum.reject(&is_nil/1)
132 |> Enum.map(&String.downcase/1)
133
134 tag_all =
135 params
136 |> Map.get(:all, [])
137 |> Enum.map(&String.downcase/1)
138
139 tag_reject =
140 params
141 |> Map.get(:none, [])
142 |> Enum.map(&String.downcase/1)
143
144 _activities =
145 params
146 |> Map.put(:type, "Create")
147 |> Map.put(:local_only, local_only)
148 |> Map.put(:blocking_user, user)
149 |> Map.put(:muting_user, user)
150 |> Map.put(:user, user)
151 |> Map.put(:tag, tags)
152 |> Map.put(:tag_all, tag_all)
153 |> Map.put(:tag_reject, tag_reject)
154 |> ActivityPub.fetch_public_activities()
155 end
156
157 # GET /api/v1/timelines/tag/:tag
158 def hashtag(%{assigns: %{user: user}} = conn, params) do
159 local_only = params[:local]
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 )
169 end
170
171 # GET /api/v1/timelines/list/:list_id
172 def list(%{assigns: %{user: user}} = conn, %{list_id: id} = params) do
173 with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
174 params =
175 params
176 |> Map.new(fn {key, value} -> {to_string(key), value} end)
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 )
198 else
199 _e -> render_error(conn, :forbidden, "Error.")
200 end
201 end
202 end