Merge branch 'bugfix/follow-query-overhead' into 'develop'
[akkoma] / lib / pleroma / web / router.ex
1 defmodule Pleroma.Web.Router do
2 use Pleroma.Web, :router
3
4 alias Pleroma.{Repo, User, Web.Router}
5
6 @instance Application.get_env(:pleroma, :instance)
7 @federating Keyword.get(@instance, :federating)
8 @public Keyword.get(@instance, :public)
9 @registrations_open Keyword.get(@instance, :registrations_open)
10
11 def user_fetcher(username) do
12 {:ok, Repo.get_by(User, %{nickname: username})}
13 end
14
15 pipeline :api do
16 plug(:accepts, ["json"])
17 plug(:fetch_session)
18 plug(Pleroma.Plugs.OAuthPlug)
19 plug(Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true})
20 end
21
22 pipeline :authenticated_api do
23 plug(:accepts, ["json"])
24 plug(:fetch_session)
25 plug(Pleroma.Plugs.OAuthPlug)
26 plug(Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1})
27 end
28
29 pipeline :mastodon_html do
30 plug(:accepts, ["html"])
31 plug(:fetch_session)
32 plug(Pleroma.Plugs.OAuthPlug)
33 plug(Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true})
34 end
35
36 pipeline :pleroma_html do
37 plug(:accepts, ["html"])
38 plug(:fetch_session)
39 plug(Pleroma.Plugs.OAuthPlug)
40 plug(Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true})
41 end
42
43 pipeline :well_known do
44 plug(:accepts, ["xml", "xrd+xml", "json", "jrd+json"])
45 end
46
47 pipeline :config do
48 plug(:accepts, ["json", "xml"])
49 end
50
51 pipeline :oauth do
52 plug(:accepts, ["html", "json"])
53 end
54
55 pipeline :pleroma_api do
56 plug(:accepts, ["html", "json"])
57 end
58
59 scope "/api/pleroma", Pleroma.Web.TwitterAPI do
60 pipe_through(:pleroma_api)
61 get("/password_reset/:token", UtilController, :show_password_reset)
62 post("/password_reset", UtilController, :password_reset)
63 get("/emoji", UtilController, :emoji)
64 end
65
66 scope "/", Pleroma.Web.TwitterAPI do
67 pipe_through(:pleroma_html)
68 get("/ostatus_subscribe", UtilController, :remote_follow)
69 post("/ostatus_subscribe", UtilController, :do_remote_follow)
70 post("/main/ostatus", UtilController, :remote_subscribe)
71 end
72
73 scope "/api/pleroma", Pleroma.Web.TwitterAPI do
74 pipe_through(:authenticated_api)
75 post("/follow_import", UtilController, :follow_import)
76 post("/change_password", UtilController, :change_password)
77 post("/delete_account", UtilController, :delete_account)
78 end
79
80 scope "/oauth", Pleroma.Web.OAuth do
81 get("/authorize", OAuthController, :authorize)
82 post("/authorize", OAuthController, :create_authorization)
83 post("/token", OAuthController, :token_exchange)
84 end
85
86 scope "/api/v1", Pleroma.Web.MastodonAPI do
87 pipe_through(:authenticated_api)
88
89 patch("/accounts/update_credentials", MastodonAPIController, :update_credentials)
90 get("/accounts/verify_credentials", MastodonAPIController, :verify_credentials)
91 get("/accounts/relationships", MastodonAPIController, :relationships)
92 get("/accounts/search", MastodonAPIController, :account_search)
93 post("/accounts/:id/follow", MastodonAPIController, :follow)
94 post("/accounts/:id/unfollow", MastodonAPIController, :unfollow)
95 post("/accounts/:id/block", MastodonAPIController, :block)
96 post("/accounts/:id/unblock", MastodonAPIController, :unblock)
97 post("/accounts/:id/mute", MastodonAPIController, :relationship_noop)
98 post("/accounts/:id/unmute", MastodonAPIController, :relationship_noop)
99
100 post("/follows", MastodonAPIController, :follow)
101
102 get("/blocks", MastodonAPIController, :blocks)
103
104 get("/domain_blocks", MastodonAPIController, :empty_array)
105 get("/follow_requests", MastodonAPIController, :empty_array)
106 get("/mutes", MastodonAPIController, :empty_array)
107
108 get("/timelines/home", MastodonAPIController, :home_timeline)
109
110 get("/favourites", MastodonAPIController, :favourites)
111
112 post("/statuses", MastodonAPIController, :post_status)
113 delete("/statuses/:id", MastodonAPIController, :delete_status)
114
115 post("/statuses/:id/reblog", MastodonAPIController, :reblog_status)
116 post("/statuses/:id/unreblog", MastodonAPIController, :unreblog_status)
117 post("/statuses/:id/favourite", MastodonAPIController, :fav_status)
118 post("/statuses/:id/unfavourite", MastodonAPIController, :unfav_status)
119
120 post("/notifications/clear", MastodonAPIController, :clear_notifications)
121 post("/notifications/dismiss", MastodonAPIController, :dismiss_notification)
122 get("/notifications", MastodonAPIController, :notifications)
123 get("/notifications/:id", MastodonAPIController, :get_notification)
124
125 post("/media", MastodonAPIController, :upload)
126
127 get("/lists", MastodonAPIController, :get_lists)
128 get("/lists/:id", MastodonAPIController, :get_list)
129 delete("/lists/:id", MastodonAPIController, :delete_list)
130 post("/lists", MastodonAPIController, :create_list)
131 put("/lists/:id", MastodonAPIController, :rename_list)
132 get("/lists/:id/accounts", MastodonAPIController, :list_accounts)
133 post("/lists/:id/accounts", MastodonAPIController, :add_to_list)
134 delete("/lists/:id/accounts", MastodonAPIController, :remove_from_list)
135 end
136
137 scope "/api/web", Pleroma.Web.MastodonAPI do
138 pipe_through(:authenticated_api)
139
140 put("/settings", MastodonAPIController, :put_settings)
141 end
142
143 scope "/api/v1", Pleroma.Web.MastodonAPI do
144 pipe_through(:api)
145 get("/instance", MastodonAPIController, :masto_instance)
146 get("/instance/peers", MastodonAPIController, :peers)
147 post("/apps", MastodonAPIController, :create_app)
148 get("/custom_emojis", MastodonAPIController, :custom_emojis)
149
150 get("/timelines/public", MastodonAPIController, :public_timeline)
151 get("/timelines/tag/:tag", MastodonAPIController, :hashtag_timeline)
152 get("/timelines/list/:list_id", MastodonAPIController, :list_timeline)
153
154 get("/statuses/:id", MastodonAPIController, :get_status)
155 get("/statuses/:id/context", MastodonAPIController, :get_context)
156 get("/statuses/:id/card", MastodonAPIController, :empty_object)
157 get("/statuses/:id/favourited_by", MastodonAPIController, :favourited_by)
158 get("/statuses/:id/reblogged_by", MastodonAPIController, :reblogged_by)
159
160 get("/accounts/:id/statuses", MastodonAPIController, :user_statuses)
161 get("/accounts/:id/followers", MastodonAPIController, :followers)
162 get("/accounts/:id/following", MastodonAPIController, :following)
163 get("/accounts/:id", MastodonAPIController, :user)
164
165 get("/search", MastodonAPIController, :search)
166 end
167
168 scope "/api", Pleroma.Web do
169 pipe_through(:config)
170
171 get("/help/test", TwitterAPI.UtilController, :help_test)
172 post("/help/test", TwitterAPI.UtilController, :help_test)
173 get("/statusnet/config", TwitterAPI.UtilController, :config)
174 get("/statusnet/version", TwitterAPI.UtilController, :version)
175 end
176
177 scope "/api", Pleroma.Web do
178 pipe_through(:api)
179
180 get("/statuses/user_timeline", TwitterAPI.Controller, :user_timeline)
181 get("/qvitter/statuses/user_timeline", TwitterAPI.Controller, :user_timeline)
182 get("/users/show", TwitterAPI.Controller, :show_user)
183
184 get("/statuses/followers", TwitterAPI.Controller, :followers)
185 get("/statuses/friends", TwitterAPI.Controller, :friends)
186 get("/statuses/show/:id", TwitterAPI.Controller, :fetch_status)
187 get("/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation)
188
189 if @registrations_open do
190 post("/account/register", TwitterAPI.Controller, :register)
191 end
192
193 get("/search", TwitterAPI.Controller, :search)
194 get("/statusnet/tags/timeline/:tag", TwitterAPI.Controller, :public_and_external_timeline)
195 end
196
197 scope "/api", Pleroma.Web do
198 if @public do
199 pipe_through(:api)
200 else
201 pipe_through(:authenticated_api)
202 end
203
204 get("/statuses/public_timeline", TwitterAPI.Controller, :public_timeline)
205
206 get(
207 "/statuses/public_and_external_timeline",
208 TwitterAPI.Controller,
209 :public_and_external_timeline
210 )
211
212 get("/statuses/networkpublic_timeline", TwitterAPI.Controller, :public_and_external_timeline)
213 end
214
215 scope "/api", Pleroma.Web do
216 pipe_through(:authenticated_api)
217
218 get("/account/verify_credentials", TwitterAPI.Controller, :verify_credentials)
219 post("/account/verify_credentials", TwitterAPI.Controller, :verify_credentials)
220
221 post("/account/update_profile", TwitterAPI.Controller, :update_profile)
222 post("/account/update_profile_banner", TwitterAPI.Controller, :update_banner)
223 post("/qvitter/update_background_image", TwitterAPI.Controller, :update_background)
224
225 post(
226 "/account/most_recent_notification",
227 TwitterAPI.Controller,
228 :update_most_recent_notification
229 )
230
231 get("/statuses/home_timeline", TwitterAPI.Controller, :friends_timeline)
232 get("/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline)
233 get("/statuses/mentions", TwitterAPI.Controller, :mentions_timeline)
234 get("/statuses/mentions_timeline", TwitterAPI.Controller, :mentions_timeline)
235 get("/qvitter/statuses/notifications", TwitterAPI.Controller, :notifications)
236
237 post("/statuses/update", TwitterAPI.Controller, :status_update)
238 post("/statuses/retweet/:id", TwitterAPI.Controller, :retweet)
239 post("/statuses/destroy/:id", TwitterAPI.Controller, :delete_post)
240
241 post("/friendships/create", TwitterAPI.Controller, :follow)
242 post("/friendships/destroy", TwitterAPI.Controller, :unfollow)
243 post("/blocks/create", TwitterAPI.Controller, :block)
244 post("/blocks/destroy", TwitterAPI.Controller, :unblock)
245
246 post("/statusnet/media/upload", TwitterAPI.Controller, :upload)
247 post("/media/upload", TwitterAPI.Controller, :upload_json)
248
249 post("/favorites/create/:id", TwitterAPI.Controller, :favorite)
250 post("/favorites/create", TwitterAPI.Controller, :favorite)
251 post("/favorites/destroy/:id", TwitterAPI.Controller, :unfavorite)
252
253 post("/qvitter/update_avatar", TwitterAPI.Controller, :update_avatar)
254
255 get("/friends/ids", TwitterAPI.Controller, :friends_ids)
256 get("/friendships/no_retweets/ids", TwitterAPI.Controller, :empty_array)
257
258 get("/mutes/users/ids", TwitterAPI.Controller, :empty_array)
259
260 get("/externalprofile/show", TwitterAPI.Controller, :external_profile)
261 end
262
263 pipeline :ostatus do
264 plug(:accepts, ["xml", "atom", "html", "activity+json"])
265 end
266
267 scope "/", Pleroma.Web do
268 pipe_through(:ostatus)
269
270 get("/objects/:uuid", OStatus.OStatusController, :object)
271 get("/activities/:uuid", OStatus.OStatusController, :activity)
272 get("/notice/:id", OStatus.OStatusController, :notice)
273 get("/users/:nickname/feed", OStatus.OStatusController, :feed)
274 get("/users/:nickname", OStatus.OStatusController, :feed_redirect)
275
276 if @federating do
277 post("/users/:nickname/salmon", OStatus.OStatusController, :salmon_incoming)
278 post("/push/hub/:nickname", Websub.WebsubController, :websub_subscription_request)
279 get("/push/subscriptions/:id", Websub.WebsubController, :websub_subscription_confirmation)
280 post("/push/subscriptions/:id", Websub.WebsubController, :websub_incoming)
281 end
282 end
283
284 pipeline :activitypub do
285 plug(:accepts, ["activity+json"])
286 plug(Pleroma.Web.Plugs.HTTPSignaturePlug)
287 end
288
289 scope "/", Pleroma.Web.ActivityPub do
290 # XXX: not really ostatus
291 pipe_through(:ostatus)
292
293 get("/users/:nickname/followers", ActivityPubController, :followers)
294 get("/users/:nickname/following", ActivityPubController, :following)
295 get("/users/:nickname/outbox", ActivityPubController, :outbox)
296 end
297
298 if @federating do
299 scope "/", Pleroma.Web.ActivityPub do
300 pipe_through(:activitypub)
301 post("/users/:nickname/inbox", ActivityPubController, :inbox)
302 post("/inbox", ActivityPubController, :inbox)
303 end
304
305 scope "/.well-known", Pleroma.Web do
306 pipe_through(:well_known)
307
308 get("/host-meta", WebFinger.WebFingerController, :host_meta)
309 get("/webfinger", WebFinger.WebFingerController, :webfinger)
310 get("/nodeinfo", Nodeinfo.NodeinfoController, :schemas)
311 end
312
313 scope "/nodeinfo", Pleroma.Web do
314 get("/:version", Nodeinfo.NodeinfoController, :nodeinfo)
315 end
316 end
317
318 scope "/", Pleroma.Web.MastodonAPI do
319 pipe_through(:mastodon_html)
320
321 get("/web/login", MastodonAPIController, :login)
322 post("/web/login", MastodonAPIController, :login_post)
323 get("/web/*path", MastodonAPIController, :index)
324 delete("/auth/sign_out", MastodonAPIController, :logout)
325 end
326
327 pipeline :remote_media do
328 plug(:accepts, ["html"])
329 end
330
331 scope "/proxy/", Pleroma.Web.MediaProxy do
332 pipe_through(:remote_media)
333 get("/:sig/:url", MediaProxyController, :remote)
334 end
335
336 scope "/", Fallback do
337 get("/*path", RedirectController, :redirector)
338 end
339 end
340
341 defmodule Fallback.RedirectController do
342 use Pleroma.Web, :controller
343
344 def redirector(conn, _params) do
345 if Mix.env() != :test do
346 conn
347 |> put_resp_content_type("text/html")
348 |> send_file(200, "priv/static/index.html")
349 end
350 end
351 end