Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.9KB

  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. defmodule Fallback.RedirectController do
  5. use Pleroma.Web, :controller
  6. require Logger
  7. alias Pleroma.User
  8. alias Pleroma.Web.Metadata
  9. def api_not_implemented(conn, _params) do
  10. conn
  11. |> put_status(404)
  12. |> json(%{error: "Not implemented"})
  13. end
  14. def redirector(conn, _params, code \\ 200)
  15. # redirect to admin section
  16. # /pleroma/admin -> /pleroma/admin/
  17. #
  18. def redirector(conn, %{"path" => ["pleroma", "admin"]} = _, _code) do
  19. redirect(conn, to: "/pleroma/admin/")
  20. end
  21. def redirector(conn, _params, code) do
  22. conn
  23. |> put_resp_content_type("text/html")
  24. |> send_file(code, index_file_path())
  25. end
  26. def redirector_with_meta(conn, %{"maybe_nickname_or_id" => maybe_nickname_or_id} = params) do
  27. with %User{} = user <- User.get_cached_by_nickname_or_id(maybe_nickname_or_id) do
  28. redirector_with_meta(conn, %{user: user})
  29. else
  30. nil ->
  31. redirector(conn, params)
  32. end
  33. end
  34. def redirector_with_meta(conn, params) do
  35. {:ok, index_content} = File.read(index_file_path())
  36. tags =
  37. try do
  38. Metadata.build_tags(params)
  39. rescue
  40. e ->
  41. Logger.error(
  42. "Metadata rendering for #{conn.request_path} failed.\n" <>
  43. Exception.format(:error, e, __STACKTRACE__)
  44. )
  45. ""
  46. end
  47. response = String.replace(index_content, "<!--server-generated-meta-->", tags)
  48. conn
  49. |> put_resp_content_type("text/html")
  50. |> send_resp(200, response)
  51. end
  52. def index_file_path do
  53. Pleroma.Plugs.InstanceStatic.file_path("index.html")
  54. end
  55. def registration_page(conn, params) do
  56. redirector(conn, params)
  57. end
  58. def empty(conn, _params) do
  59. conn
  60. |> put_status(204)
  61. |> text("")
  62. end
  63. end