redirect_controller.ex 2.52 KB
Newer Older
1
# Pleroma: A lightweight social networking server
feld's avatar
feld committed
2
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3
4
# SPDX-License-Identifier: AGPL-3.0-only

Alexander Strizhakov's avatar
Alexander Strizhakov committed
5
defmodule Pleroma.Web.Fallback.RedirectController do
6
  use Pleroma.Web, :controller
Steven Fuchs's avatar
Steven Fuchs committed
7

8
  require Logger
Steven Fuchs's avatar
Steven Fuchs committed
9

10
11
  alias Pleroma.User
  alias Pleroma.Web.Metadata
Steven Fuchs's avatar
Steven Fuchs committed
12
  alias Pleroma.Web.Preload
13
14
15
16
17
18
19

  def api_not_implemented(conn, _params) do
    conn
    |> put_status(404)
    |> json(%{error: "Not implemented"})
  end

Steven Fuchs's avatar
Steven Fuchs committed
20
  def redirector(conn, _params, code \\ 200) do
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    conn
    |> put_resp_content_type("text/html")
    |> send_file(code, index_file_path())
  end

  def redirector_with_meta(conn, %{"maybe_nickname_or_id" => maybe_nickname_or_id} = params) do
    with %User{} = user <- User.get_cached_by_nickname_or_id(maybe_nickname_or_id) do
      redirector_with_meta(conn, %{user: user})
    else
      nil ->
        redirector(conn, params)
    end
  end

  def redirector_with_meta(conn, params) do
    {:ok, index_content} = File.read(index_file_path())

Steven Fuchs's avatar
Steven Fuchs committed
38
39
    tags = build_tags(conn, params)
    preloads = preload_data(conn, params)
40

Steven Fuchs's avatar
Steven Fuchs committed
41
42
    response =
      index_content
Steven Fuchs's avatar
Steven Fuchs committed
43
      |> String.replace("<!--server-generated-meta-->", tags <> preloads)
44
45
46
47
48
49

    conn
    |> put_resp_content_type("text/html")
    |> send_resp(200, response)
  end

Steven Fuchs's avatar
Steven Fuchs committed
50
51
52
53
54
55
56
57
58
59
  def redirector_with_preload(conn, %{"path" => ["pleroma", "admin"]}) do
    redirect(conn, to: "/pleroma/admin/")
  end

  def redirector_with_preload(conn, params) do
    {:ok, index_content} = File.read(index_file_path())
    preloads = preload_data(conn, params)

    response =
      index_content
Steven Fuchs's avatar
Steven Fuchs committed
60
      |> String.replace("<!--server-generated-meta-->", preloads)
Steven Fuchs's avatar
Steven Fuchs committed
61
62
63
64

    conn
    |> put_resp_content_type("text/html")
    |> send_resp(200, response)
65
66
67
68
69
70
71
72
73
74
75
  end

  def registration_page(conn, params) do
    redirector(conn, params)
  end

  def empty(conn, _params) do
    conn
    |> put_status(204)
    |> text("")
  end
Steven Fuchs's avatar
Steven Fuchs committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

  defp index_file_path do
    Pleroma.Plugs.InstanceStatic.file_path("index.html")
  end

  defp build_tags(conn, params) do
    try do
      Metadata.build_tags(params)
    rescue
      e ->
        Logger.error(
          "Metadata rendering for #{conn.request_path} failed.\n" <>
            Exception.format(:error, e, __STACKTRACE__)
        )

        ""
    end
  end

  defp preload_data(conn, params) do
    try do
      Preload.build_tags(conn, params)
    rescue
      e ->
        Logger.error(
          "Preloading for #{conn.request_path} failed.\n" <>
            Exception.format(:error, e, __STACKTRACE__)
        )

        ""
    end
  end
108
end