redirect_controller.ex 2.77 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)
Sean King's avatar
Sean King committed
40
    title = "<title>#{Pleroma.Config.get([:instance, :name])}</title>"
41

Steven Fuchs's avatar
Steven Fuchs committed
42
43
    response =
      index_content
Sean King's avatar
Sean King committed
44
      |> String.replace(~r/<title>.+?<\/title>/, title)
Steven Fuchs's avatar
Steven Fuchs committed
45
      |> String.replace("<!--server-generated-meta-->", tags <> preloads)
46
47
48
49
50
51

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

Steven Fuchs's avatar
Steven Fuchs committed
52
53
54
55
56
57
58
  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)
Sean King's avatar
Sean King committed
59
    title = "<title>#{Pleroma.Config.get([:instance, :name])}</title>"
Steven Fuchs's avatar
Steven Fuchs committed
60
61
62

    response =
      index_content
Sean King's avatar
Sean King committed
63
      |> String.replace(~r/<title>.+?<\/title>/, title)
Steven Fuchs's avatar
Steven Fuchs committed
64
      |> String.replace("<!--server-generated-meta-->", preloads)
Steven Fuchs's avatar
Steven Fuchs committed
65
66
67
68

    conn
    |> put_resp_content_type("text/html")
    |> send_resp(200, response)
69
70
71
72
73
74
75
76
77
78
79
  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
80
81

  defp index_file_path do
82
    Pleroma.Web.Plugs.InstanceStatic.file_path("index.html")
Steven Fuchs's avatar
Steven Fuchs committed
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
108
109
110
111
  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
112
end