akkoma/lib/pleroma/web/preload.ex

37 lines
948 B
Elixir
Raw Permalink Normal View History

2020-05-12 09:08:00 -06:00
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
2020-05-12 09:08:00 -06:00
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Preload do
alias Phoenix.HTML
2022-12-16 05:36:34 -07:00
def build_tags(%{assigns: %{csp_nonce: nonce}}, params) do
2020-05-12 09:08:00 -06:00
preload_data =
Enum.reduce(Pleroma.Config.get([__MODULE__, :providers], []), %{}, fn parser, acc ->
2020-06-03 11:40:48 -06:00
terms =
params
|> parser.generate_terms()
|> Enum.map(fn {k, v} -> {k, Base.encode64(Jason.encode!(v))} end)
|> Enum.into(%{})
Map.merge(acc, terms)
2020-05-12 09:08:00 -06:00
end)
rendered_html =
preload_data
|> Jason.encode!()
2022-12-16 04:50:25 -07:00
|> build_script_tag(nonce)
2020-05-12 09:08:00 -06:00
|> HTML.safe_to_string()
rendered_html
end
2022-12-16 04:50:25 -07:00
def build_script_tag(content, nonce) do
2020-05-12 09:08:00 -06:00
HTML.Tag.content_tag(:script, HTML.raw(content),
id: "initial-results",
2022-12-16 04:50:25 -07:00
type: "application/json",
nonce: nonce
2020-05-12 09:08:00 -06:00
)
end
end