preload.ex 890 Bytes
Newer Older
Steven Fuchs's avatar
Steven Fuchs committed
1
# Pleroma: A lightweight social networking server
Haelwenn's avatar
Haelwenn committed
2
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
Steven Fuchs's avatar
Steven Fuchs committed
3
4
5
6
7
8
9
10
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.Preload do
  alias Phoenix.HTML

  def build_tags(_conn, params) do
    preload_data =
      Enum.reduce(Pleroma.Config.get([__MODULE__, :providers], []), %{}, fn parser, acc ->
Steven Fuchs's avatar
Steven Fuchs committed
11
12
13
14
15
16
17
        terms =
          params
          |> parser.generate_terms()
          |> Enum.map(fn {k, v} -> {k, Base.encode64(Jason.encode!(v))} end)
          |> Enum.into(%{})

        Map.merge(acc, terms)
Steven Fuchs's avatar
Steven Fuchs committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
      end)

    rendered_html =
      preload_data
      |> Jason.encode!()
      |> build_script_tag()
      |> HTML.safe_to_string()

    rendered_html
  end

  def build_script_tag(content) do
    HTML.Tag.content_tag(:script, HTML.raw(content),
      id: "initial-results",
      type: "application/json"
    )
  end
end