[#468] Refactored OAuth scopes' defaults & missing selection handling.
[akkoma] / lib / pleroma / web / router.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Router do
6 use Pleroma.Web, :router
7
8 pipeline :api do
9 plug(:accepts, ["json"])
10 plug(:fetch_session)
11 plug(Pleroma.Plugs.OAuthPlug)
12 plug(Pleroma.Plugs.BasicAuthDecoderPlug)
13 plug(Pleroma.Plugs.UserFetcherPlug)
14 plug(Pleroma.Plugs.SessionAuthenticationPlug)
15 plug(Pleroma.Plugs.LegacyAuthenticationPlug)
16 plug(Pleroma.Plugs.AuthenticationPlug)
17 plug(Pleroma.Plugs.UserEnabledPlug)
18 plug(Pleroma.Plugs.SetUserSessionIdPlug)
19 plug(Pleroma.Plugs.EnsureUserKeyPlug)
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.BasicAuthDecoderPlug)
27 plug(Pleroma.Plugs.UserFetcherPlug)
28 plug(Pleroma.Plugs.SessionAuthenticationPlug)
29 plug(Pleroma.Plugs.LegacyAuthenticationPlug)
30 plug(Pleroma.Plugs.AuthenticationPlug)
31 plug(Pleroma.Plugs.UserEnabledPlug)
32 plug(Pleroma.Plugs.SetUserSessionIdPlug)
33 plug(Pleroma.Plugs.EnsureAuthenticatedPlug)
34 end
35
36 pipeline :admin_api do
37 plug(:accepts, ["json"])
38 plug(:fetch_session)
39 plug(Pleroma.Plugs.OAuthPlug)
40 plug(Pleroma.Plugs.BasicAuthDecoderPlug)
41 plug(Pleroma.Plugs.UserFetcherPlug)
42 plug(Pleroma.Plugs.SessionAuthenticationPlug)
43 plug(Pleroma.Plugs.LegacyAuthenticationPlug)
44 plug(Pleroma.Plugs.AuthenticationPlug)
45 plug(Pleroma.Plugs.AdminSecretAuthenticationPlug)
46 plug(Pleroma.Plugs.UserEnabledPlug)
47 plug(Pleroma.Plugs.SetUserSessionIdPlug)
48 plug(Pleroma.Plugs.EnsureAuthenticatedPlug)
49 plug(Pleroma.Plugs.UserIsAdminPlug)
50 end
51
52 pipeline :mastodon_html do
53 plug(:accepts, ["html"])
54 plug(:fetch_session)
55 plug(Pleroma.Plugs.OAuthPlug)
56 plug(Pleroma.Plugs.BasicAuthDecoderPlug)
57 plug(Pleroma.Plugs.UserFetcherPlug)
58 plug(Pleroma.Plugs.SessionAuthenticationPlug)
59 plug(Pleroma.Plugs.LegacyAuthenticationPlug)
60 plug(Pleroma.Plugs.AuthenticationPlug)
61 plug(Pleroma.Plugs.UserEnabledPlug)
62 plug(Pleroma.Plugs.SetUserSessionIdPlug)
63 plug(Pleroma.Plugs.EnsureUserKeyPlug)
64 end
65
66 pipeline :pleroma_html do
67 plug(:accepts, ["html"])
68 plug(:fetch_session)
69 plug(Pleroma.Plugs.OAuthPlug)
70 plug(Pleroma.Plugs.BasicAuthDecoderPlug)
71 plug(Pleroma.Plugs.UserFetcherPlug)
72 plug(Pleroma.Plugs.SessionAuthenticationPlug)
73 plug(Pleroma.Plugs.AuthenticationPlug)
74 plug(Pleroma.Plugs.EnsureUserKeyPlug)
75 end
76
77 pipeline :oauth_read_or_unauthenticated do
78 plug(Pleroma.Plugs.OAuthScopesPlug, %{
79 scopes: ["read"],
80 fallback: :proceed_unauthenticated
81 })
82 end
83
84 pipeline :oauth_read do
85 plug(Pleroma.Plugs.OAuthScopesPlug, %{scopes: ["read"]})
86 end
87
88 pipeline :oauth_write do
89 plug(Pleroma.Plugs.OAuthScopesPlug, %{scopes: ["write"]})
90 end
91
92 pipeline :oauth_follow do
93 plug(Pleroma.Plugs.OAuthScopesPlug, %{scopes: ["follow"]})
94 end
95
96 pipeline :well_known do
97 plug(:accepts, ["json", "jrd+json", "xml", "xrd+xml"])
98 end
99
100 pipeline :config do
101 plug(:accepts, ["json", "xml"])
102 end
103
104 pipeline :oauth do
105 plug(:accepts, ["html", "json"])
106 end
107
108 pipeline :pleroma_api do
109 plug(:accepts, ["html", "json"])
110 end
111
112 pipeline :mailbox_preview do
113 plug(:accepts, ["html"])
114
115 plug(:put_secure_browser_headers, %{
116 "content-security-policy" =>
117 "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline' 'unsafe-eval'"
118 })
119 end
120
121 scope "/api/pleroma", Pleroma.Web.TwitterAPI do
122 pipe_through(:pleroma_api)
123
124 get("/password_reset/:token", UtilController, :show_password_reset)
125 post("/password_reset", UtilController, :password_reset)
126 get("/emoji", UtilController, :emoji)
127 get("/captcha", UtilController, :captcha)
128 end
129
130 scope "/api/pleroma", Pleroma.Web do
131 pipe_through(:pleroma_api)
132 post("/uploader_callback/:upload_path", UploaderController, :callback)
133 end
134
135 scope "/api/pleroma/admin", Pleroma.Web.AdminAPI do
136 pipe_through([:admin_api, :oauth_write])
137
138 delete("/user", AdminAPIController, :user_delete)
139 post("/user", AdminAPIController, :user_create)
140 put("/users/tag", AdminAPIController, :tag_users)
141 delete("/users/tag", AdminAPIController, :untag_users)
142
143 get("/permission_group/:nickname", AdminAPIController, :right_get)
144 get("/permission_group/:nickname/:permission_group", AdminAPIController, :right_get)
145 post("/permission_group/:nickname/:permission_group", AdminAPIController, :right_add)
146 delete("/permission_group/:nickname/:permission_group", AdminAPIController, :right_delete)
147
148 post("/relay", AdminAPIController, :relay_follow)
149 delete("/relay", AdminAPIController, :relay_unfollow)
150
151 get("/invite_token", AdminAPIController, :get_invite_token)
152 post("/email_invite", AdminAPIController, :email_invite)
153
154 get("/password_reset", AdminAPIController, :get_password_reset)
155 end
156
157 scope "/", Pleroma.Web.TwitterAPI do
158 pipe_through(:pleroma_html)
159
160 post("/main/ostatus", UtilController, :remote_subscribe)
161 get("/ostatus_subscribe", UtilController, :remote_follow)
162
163 scope [] do
164 pipe_through(:oauth_follow)
165 post("/ostatus_subscribe", UtilController, :do_remote_follow)
166 end
167 end
168
169 scope "/api/pleroma", Pleroma.Web.TwitterAPI do
170 pipe_through(:authenticated_api)
171
172 scope [] do
173 pipe_through(:oauth_write)
174
175 post("/change_password", UtilController, :change_password)
176 post("/delete_account", UtilController, :delete_account)
177 end
178
179 scope [] do
180 pipe_through(:oauth_follow)
181
182 post("/blocks_import", UtilController, :blocks_import)
183 post("/follow_import", UtilController, :follow_import)
184 end
185 end
186
187 scope "/oauth", Pleroma.Web.OAuth do
188 get("/authorize", OAuthController, :authorize)
189 post("/authorize", OAuthController, :create_authorization)
190 post("/token", OAuthController, :token_exchange)
191 post("/revoke", OAuthController, :token_revoke)
192 end
193
194 scope "/api/v1", Pleroma.Web.MastodonAPI do
195 pipe_through(:authenticated_api)
196
197 scope [] do
198 pipe_through(:oauth_read)
199
200 get("/accounts/verify_credentials", MastodonAPIController, :verify_credentials)
201
202 get("/accounts/relationships", MastodonAPIController, :relationships)
203 get("/accounts/search", MastodonAPIController, :account_search)
204
205 get("/accounts/:id/lists", MastodonAPIController, :account_lists)
206
207 get("/follow_requests", MastodonAPIController, :follow_requests)
208 get("/blocks", MastodonAPIController, :blocks)
209 get("/mutes", MastodonAPIController, :empty_array)
210
211 get("/timelines/home", MastodonAPIController, :home_timeline)
212 get("/timelines/direct", MastodonAPIController, :dm_timeline)
213
214 get("/favourites", MastodonAPIController, :favourites)
215 get("/bookmarks", MastodonAPIController, :bookmarks)
216
217 post("/notifications/clear", MastodonAPIController, :clear_notifications)
218 post("/notifications/dismiss", MastodonAPIController, :dismiss_notification)
219 get("/notifications", MastodonAPIController, :notifications)
220 get("/notifications/:id", MastodonAPIController, :get_notification)
221
222 get("/lists", MastodonAPIController, :get_lists)
223 get("/lists/:id", MastodonAPIController, :get_list)
224 get("/lists/:id/accounts", MastodonAPIController, :list_accounts)
225
226 get("/domain_blocks", MastodonAPIController, :domain_blocks)
227
228 get("/filters", MastodonAPIController, :get_filters)
229
230 get("/suggestions", MastodonAPIController, :suggestions)
231
232 get("/endorsements", MastodonAPIController, :empty_array)
233 end
234
235 scope [] do
236 pipe_through(:oauth_write)
237
238 patch("/accounts/update_credentials", MastodonAPIController, :update_credentials)
239
240 post("/statuses", MastodonAPIController, :post_status)
241 delete("/statuses/:id", MastodonAPIController, :delete_status)
242
243 post("/statuses/:id/reblog", MastodonAPIController, :reblog_status)
244 post("/statuses/:id/unreblog", MastodonAPIController, :unreblog_status)
245 post("/statuses/:id/favourite", MastodonAPIController, :fav_status)
246 post("/statuses/:id/unfavourite", MastodonAPIController, :unfav_status)
247 post("/statuses/:id/pin", MastodonAPIController, :pin_status)
248 post("/statuses/:id/unpin", MastodonAPIController, :unpin_status)
249 post("/statuses/:id/bookmark", MastodonAPIController, :bookmark_status)
250 post("/statuses/:id/unbookmark", MastodonAPIController, :unbookmark_status)
251
252 post("/media", MastodonAPIController, :upload)
253 put("/media/:id", MastodonAPIController, :update_media)
254
255 delete("/lists/:id", MastodonAPIController, :delete_list)
256 post("/lists", MastodonAPIController, :create_list)
257 put("/lists/:id", MastodonAPIController, :rename_list)
258
259 post("/lists/:id/accounts", MastodonAPIController, :add_to_list)
260 delete("/lists/:id/accounts", MastodonAPIController, :remove_from_list)
261
262 post("/filters", MastodonAPIController, :create_filter)
263 get("/filters/:id", MastodonAPIController, :get_filter)
264 put("/filters/:id", MastodonAPIController, :update_filter)
265 delete("/filters/:id", MastodonAPIController, :delete_filter)
266 end
267
268 scope [] do
269 pipe_through(:oauth_follow)
270
271 post("/follows", MastodonAPIController, :follow)
272 post("/accounts/:id/follow", MastodonAPIController, :follow)
273
274 post("/accounts/:id/unfollow", MastodonAPIController, :unfollow)
275 post("/accounts/:id/block", MastodonAPIController, :block)
276 post("/accounts/:id/unblock", MastodonAPIController, :unblock)
277 post("/accounts/:id/mute", MastodonAPIController, :relationship_noop)
278 post("/accounts/:id/unmute", MastodonAPIController, :relationship_noop)
279
280 post("/follow_requests/:id/authorize", MastodonAPIController, :authorize_follow_request)
281 post("/follow_requests/:id/reject", MastodonAPIController, :reject_follow_request)
282
283 post("/domain_blocks", MastodonAPIController, :block_domain)
284 delete("/domain_blocks", MastodonAPIController, :unblock_domain)
285
286 post("/push/subscription", MastodonAPIController, :create_push_subscription)
287 get("/push/subscription", MastodonAPIController, :get_push_subscription)
288 put("/push/subscription", MastodonAPIController, :update_push_subscription)
289 delete("/push/subscription", MastodonAPIController, :delete_push_subscription)
290 end
291 end
292
293 scope "/api/web", Pleroma.Web.MastodonAPI do
294 pipe_through([:authenticated_api, :oauth_write])
295
296 put("/settings", MastodonAPIController, :put_settings)
297 end
298
299 scope "/api/v1", Pleroma.Web.MastodonAPI do
300 pipe_through(:api)
301
302 get("/instance", MastodonAPIController, :masto_instance)
303 get("/instance/peers", MastodonAPIController, :peers)
304 post("/apps", MastodonAPIController, :create_app)
305 get("/custom_emojis", MastodonAPIController, :custom_emojis)
306
307 get("/statuses/:id/card", MastodonAPIController, :status_card)
308
309 get("/statuses/:id/favourited_by", MastodonAPIController, :favourited_by)
310 get("/statuses/:id/reblogged_by", MastodonAPIController, :reblogged_by)
311
312 get("/trends", MastodonAPIController, :empty_array)
313
314 scope [] do
315 pipe_through(:oauth_read_or_unauthenticated)
316
317 get("/timelines/public", MastodonAPIController, :public_timeline)
318 get("/timelines/tag/:tag", MastodonAPIController, :hashtag_timeline)
319 get("/timelines/list/:list_id", MastodonAPIController, :list_timeline)
320
321 get("/statuses/:id", MastodonAPIController, :get_status)
322 get("/statuses/:id/context", MastodonAPIController, :get_context)
323
324 get("/accounts/:id/statuses", MastodonAPIController, :user_statuses)
325 get("/accounts/:id/followers", MastodonAPIController, :followers)
326 get("/accounts/:id/following", MastodonAPIController, :following)
327 get("/accounts/:id", MastodonAPIController, :user)
328
329 get("/search", MastodonAPIController, :search)
330 end
331 end
332
333 scope "/api/v2", Pleroma.Web.MastodonAPI do
334 pipe_through([:api, :oauth_read_or_unauthenticated])
335 get("/search", MastodonAPIController, :search2)
336 end
337
338 scope "/api", Pleroma.Web do
339 pipe_through(:config)
340
341 get("/help/test", TwitterAPI.UtilController, :help_test)
342 post("/help/test", TwitterAPI.UtilController, :help_test)
343 get("/statusnet/config", TwitterAPI.UtilController, :config)
344 get("/statusnet/version", TwitterAPI.UtilController, :version)
345 get("/pleroma/frontend_configurations", TwitterAPI.UtilController, :frontend_configurations)
346 end
347
348 scope "/api", Pleroma.Web do
349 pipe_through(:api)
350
351 post("/account/register", TwitterAPI.Controller, :register)
352 post("/account/password_reset", TwitterAPI.Controller, :password_reset)
353
354 post("/account/resend_confirmation_email", TwitterAPI.Controller, :resend_confirmation_email)
355
356 get(
357 "/account/confirm_email/:user_id/:token",
358 TwitterAPI.Controller,
359 :confirm_email,
360 as: :confirm_email
361 )
362
363 scope [] do
364 pipe_through(:oauth_read_or_unauthenticated)
365
366 get("/statuses/user_timeline", TwitterAPI.Controller, :user_timeline)
367 get("/qvitter/statuses/user_timeline", TwitterAPI.Controller, :user_timeline)
368 get("/users/show", TwitterAPI.Controller, :show_user)
369
370 get("/statuses/followers", TwitterAPI.Controller, :followers)
371 get("/statuses/friends", TwitterAPI.Controller, :friends)
372 get("/statuses/blocks", TwitterAPI.Controller, :blocks)
373 get("/statuses/show/:id", TwitterAPI.Controller, :fetch_status)
374 get("/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation)
375
376 get("/search", TwitterAPI.Controller, :search)
377 get("/statusnet/tags/timeline/:tag", TwitterAPI.Controller, :public_and_external_timeline)
378 end
379 end
380
381 scope "/api", Pleroma.Web do
382 pipe_through([:api, :oauth_read_or_unauthenticated])
383
384 get("/statuses/public_timeline", TwitterAPI.Controller, :public_timeline)
385
386 get(
387 "/statuses/public_and_external_timeline",
388 TwitterAPI.Controller,
389 :public_and_external_timeline
390 )
391
392 get("/statuses/networkpublic_timeline", TwitterAPI.Controller, :public_and_external_timeline)
393 end
394
395 scope "/api", Pleroma.Web, as: :twitter_api_search do
396 pipe_through([:api, :oauth_read_or_unauthenticated])
397 get("/pleroma/search_user", TwitterAPI.Controller, :search_user)
398 end
399
400 scope "/api", Pleroma.Web, as: :authenticated_twitter_api do
401 pipe_through(:authenticated_api)
402
403 scope [] do
404 pipe_through(:oauth_read)
405
406 get("/account/verify_credentials", TwitterAPI.Controller, :verify_credentials)
407 post("/account/verify_credentials", TwitterAPI.Controller, :verify_credentials)
408
409 get("/statuses/home_timeline", TwitterAPI.Controller, :friends_timeline)
410 get("/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline)
411 get("/statuses/mentions", TwitterAPI.Controller, :mentions_timeline)
412 get("/statuses/mentions_timeline", TwitterAPI.Controller, :mentions_timeline)
413 get("/statuses/dm_timeline", TwitterAPI.Controller, :dm_timeline)
414 get("/qvitter/statuses/notifications", TwitterAPI.Controller, :notifications)
415
416 get("/pleroma/friend_requests", TwitterAPI.Controller, :friend_requests)
417
418 get("/friends/ids", TwitterAPI.Controller, :friends_ids)
419 get("/friendships/no_retweets/ids", TwitterAPI.Controller, :empty_array)
420
421 get("/mutes/users/ids", TwitterAPI.Controller, :empty_array)
422 get("/qvitter/mutes", TwitterAPI.Controller, :raw_empty_array)
423
424 get("/externalprofile/show", TwitterAPI.Controller, :external_profile)
425
426 post("/qvitter/statuses/notifications/read", TwitterAPI.Controller, :notifications_read)
427 end
428
429 scope [] do
430 pipe_through(:oauth_write)
431
432 post("/account/update_profile", TwitterAPI.Controller, :update_profile)
433 post("/account/update_profile_banner", TwitterAPI.Controller, :update_banner)
434 post("/qvitter/update_background_image", TwitterAPI.Controller, :update_background)
435
436 post("/statuses/update", TwitterAPI.Controller, :status_update)
437 post("/statuses/retweet/:id", TwitterAPI.Controller, :retweet)
438 post("/statuses/unretweet/:id", TwitterAPI.Controller, :unretweet)
439 post("/statuses/destroy/:id", TwitterAPI.Controller, :delete_post)
440
441 post("/statuses/pin/:id", TwitterAPI.Controller, :pin)
442 post("/statuses/unpin/:id", TwitterAPI.Controller, :unpin)
443
444 post("/statusnet/media/upload", TwitterAPI.Controller, :upload)
445 post("/media/upload", TwitterAPI.Controller, :upload_json)
446 post("/media/metadata/create", TwitterAPI.Controller, :update_media)
447
448 post("/favorites/create/:id", TwitterAPI.Controller, :favorite)
449 post("/favorites/create", TwitterAPI.Controller, :favorite)
450 post("/favorites/destroy/:id", TwitterAPI.Controller, :unfavorite)
451
452 post("/qvitter/update_avatar", TwitterAPI.Controller, :update_avatar)
453 end
454
455 scope [] do
456 pipe_through(:oauth_follow)
457
458 post("/pleroma/friendships/approve", TwitterAPI.Controller, :approve_friend_request)
459 post("/pleroma/friendships/deny", TwitterAPI.Controller, :deny_friend_request)
460
461 post("/friendships/create", TwitterAPI.Controller, :follow)
462 post("/friendships/destroy", TwitterAPI.Controller, :unfollow)
463
464 post("/blocks/create", TwitterAPI.Controller, :block)
465 post("/blocks/destroy", TwitterAPI.Controller, :unblock)
466 end
467 end
468
469 pipeline :ap_relay do
470 plug(:accepts, ["activity+json"])
471 end
472
473 pipeline :ostatus do
474 plug(:accepts, ["html", "xml", "atom", "activity+json"])
475 end
476
477 pipeline :oembed do
478 plug(:accepts, ["json", "xml"])
479 end
480
481 scope "/", Pleroma.Web do
482 pipe_through(:ostatus)
483
484 get("/objects/:uuid", OStatus.OStatusController, :object)
485 get("/activities/:uuid", OStatus.OStatusController, :activity)
486 get("/notice/:id", OStatus.OStatusController, :notice)
487 get("/users/:nickname/feed", OStatus.OStatusController, :feed)
488 get("/users/:nickname", OStatus.OStatusController, :feed_redirect)
489
490 post("/users/:nickname/salmon", OStatus.OStatusController, :salmon_incoming)
491 post("/push/hub/:nickname", Websub.WebsubController, :websub_subscription_request)
492 get("/push/subscriptions/:id", Websub.WebsubController, :websub_subscription_confirmation)
493 post("/push/subscriptions/:id", Websub.WebsubController, :websub_incoming)
494 end
495
496 scope "/", Pleroma.Web do
497 pipe_through(:oembed)
498
499 get("/oembed", OEmbed.OEmbedController, :url)
500 end
501
502 pipeline :activitypub do
503 plug(:accepts, ["activity+json"])
504 plug(Pleroma.Web.Plugs.HTTPSignaturePlug)
505 end
506
507 scope "/", Pleroma.Web.ActivityPub do
508 # XXX: not really ostatus
509 pipe_through(:ostatus)
510
511 get("/users/:nickname/followers", ActivityPubController, :followers)
512 get("/users/:nickname/following", ActivityPubController, :following)
513 get("/users/:nickname/outbox", ActivityPubController, :outbox)
514 get("/objects/:uuid/likes", ActivityPubController, :object_likes)
515 end
516
517 pipeline :activitypub_client do
518 plug(:accepts, ["activity+json"])
519 plug(:fetch_session)
520 plug(Pleroma.Plugs.OAuthPlug)
521 plug(Pleroma.Plugs.BasicAuthDecoderPlug)
522 plug(Pleroma.Plugs.UserFetcherPlug)
523 plug(Pleroma.Plugs.SessionAuthenticationPlug)
524 plug(Pleroma.Plugs.LegacyAuthenticationPlug)
525 plug(Pleroma.Plugs.AuthenticationPlug)
526 plug(Pleroma.Plugs.UserEnabledPlug)
527 plug(Pleroma.Plugs.SetUserSessionIdPlug)
528 plug(Pleroma.Plugs.EnsureUserKeyPlug)
529 end
530
531 scope "/", Pleroma.Web.ActivityPub do
532 pipe_through([:activitypub_client])
533
534 scope [] do
535 pipe_through(:oauth_read)
536 get("/api/ap/whoami", ActivityPubController, :whoami)
537 get("/users/:nickname/inbox", ActivityPubController, :read_inbox)
538 end
539
540 scope [] do
541 pipe_through(:oauth_write)
542 post("/users/:nickname/outbox", ActivityPubController, :update_outbox)
543 end
544 end
545
546 scope "/relay", Pleroma.Web.ActivityPub do
547 pipe_through(:ap_relay)
548 get("/", ActivityPubController, :relay)
549 end
550
551 scope "/", Pleroma.Web.ActivityPub do
552 pipe_through(:activitypub)
553
554 post("/users/:nickname/inbox", ActivityPubController, :inbox)
555 post("/inbox", ActivityPubController, :inbox)
556 end
557
558 scope "/.well-known", Pleroma.Web do
559 pipe_through(:well_known)
560
561 get("/host-meta", WebFinger.WebFingerController, :host_meta)
562 get("/webfinger", WebFinger.WebFingerController, :webfinger)
563 get("/nodeinfo", Nodeinfo.NodeinfoController, :schemas)
564 end
565
566 scope "/nodeinfo", Pleroma.Web do
567 get("/:version", Nodeinfo.NodeinfoController, :nodeinfo)
568 end
569
570 scope "/", Pleroma.Web.MastodonAPI do
571 pipe_through(:mastodon_html)
572
573 get("/web/login", MastodonAPIController, :login)
574 delete("/auth/sign_out", MastodonAPIController, :logout)
575
576 scope [] do
577 pipe_through(:oauth_read)
578 get("/web/*path", MastodonAPIController, :index)
579 end
580 end
581
582 pipeline :remote_media do
583 end
584
585 scope "/proxy/", Pleroma.Web.MediaProxy do
586 pipe_through(:remote_media)
587
588 get("/:sig/:url", MediaProxyController, :remote)
589 get("/:sig/:url/:filename", MediaProxyController, :remote)
590 end
591
592 if Mix.env() == :dev do
593 scope "/dev" do
594 pipe_through([:mailbox_preview])
595
596 forward("/mailbox", Plug.Swoosh.MailboxPreview, base_path: "/dev/mailbox")
597 end
598 end
599
600 scope "/", Fallback do
601 get("/registration/:token", RedirectController, :registration_page)
602 get("/:maybe_nickname_or_id", RedirectController, :redirector_with_meta)
603 get("/*path", RedirectController, :redirector)
604
605 options("/*path", RedirectController, :empty)
606 end
607 end
608
609 defmodule Fallback.RedirectController do
610 use Pleroma.Web, :controller
611 alias Pleroma.Web.Metadata
612 alias Pleroma.User
613
614 def redirector(conn, _params, code \\ 200) do
615 conn
616 |> put_resp_content_type("text/html")
617 |> send_file(code, index_file_path())
618 end
619
620 def redirector_with_meta(conn, %{"maybe_nickname_or_id" => maybe_nickname_or_id} = params) do
621 with %User{} = user <- User.get_cached_by_nickname_or_id(maybe_nickname_or_id) do
622 redirector_with_meta(conn, %{user: user})
623 else
624 nil ->
625 redirector(conn, params)
626 end
627 end
628
629 def redirector_with_meta(conn, params) do
630 {:ok, index_content} = File.read(index_file_path())
631 tags = Metadata.build_tags(params)
632 response = String.replace(index_content, "<!--server-generated-meta-->", tags)
633
634 conn
635 |> put_resp_content_type("text/html")
636 |> send_resp(200, response)
637 end
638
639 def index_file_path do
640 Pleroma.Plugs.InstanceStatic.file_path("index.html")
641 end
642
643 def registration_page(conn, params) do
644 redirector(conn, params)
645 end
646
647 def empty(conn, _params) do
648 conn
649 |> put_status(204)
650 |> text("")
651 end
652 end