Merge pull request 'Correct og:description tag in static-fe' (#373) from sfr/akkoma...
[akkoma] / lib / pleroma / web / preload.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Preload do
6 alias Phoenix.HTML
7
8 def build_tags(%{assigns: %{csp_nonce: nonce}}, params) do
9 preload_data =
10 Enum.reduce(Pleroma.Config.get([__MODULE__, :providers], []), %{}, fn parser, acc ->
11 terms =
12 params
13 |> parser.generate_terms()
14 |> Enum.map(fn {k, v} -> {k, Base.encode64(Jason.encode!(v))} end)
15 |> Enum.into(%{})
16
17 Map.merge(acc, terms)
18 end)
19
20 rendered_html =
21 preload_data
22 |> Jason.encode!()
23 |> build_script_tag(nonce)
24 |> HTML.safe_to_string()
25
26 rendered_html
27 end
28
29 def build_script_tag(content, nonce) do
30 HTML.Tag.content_tag(:script, HTML.raw(content),
31 id: "initial-results",
32 type: "application/json",
33 nonce: nonce
34 )
35 end
36 end