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