2020-06-18 15:23:05 +02:00
|
|
|
defmodule Mobilizon.Web.Views.Utils do
|
|
|
|
@moduledoc """
|
|
|
|
Utils for views
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Service.Metadata.Utils, as: MetadataUtils
|
2021-03-09 19:39:54 +01:00
|
|
|
import Mobilizon.Web.Gettext, only: [dgettext: 2]
|
|
|
|
import Plug.Conn, only: [put_status: 2, halt: 1]
|
|
|
|
|
2021-01-22 19:11:13 +01:00
|
|
|
# sobelow_skip ["Traversal.FileModule"]
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec inject_tags(Enum.t(), String.t()) :: {:ok, {:safe, String.t()}} | {:error, atom()}
|
2020-06-18 15:23:05 +02:00
|
|
|
def inject_tags(tags, locale \\ "en") do
|
2021-03-16 11:08:04 +01:00
|
|
|
with path <- Path.join(Application.app_dir(:mobilizon, "priv/static"), "index.html"),
|
|
|
|
{:exists, true} <- {:exists, File.exists?(path)},
|
|
|
|
{:ok, index_content} <- File.read(path),
|
2021-03-09 19:39:54 +01:00
|
|
|
safe <- do_replacements(index_content, MetadataUtils.stringify_tags(tags), locale) do
|
|
|
|
{:ok, {:safe, safe}}
|
|
|
|
else
|
|
|
|
{:exists, false} -> {:error, :index_not_found}
|
2021-09-10 11:27:59 +02:00
|
|
|
{:error, error} when is_atom(error) -> {:error, error}
|
2020-06-18 15:23:05 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-09 19:39:54 +01:00
|
|
|
@spec return_error(Plug.Conn.t(), atom()) :: Plug.Conn.t()
|
|
|
|
def return_error(conn, error) do
|
|
|
|
conn
|
|
|
|
|> put_status(500)
|
|
|
|
|> Phoenix.Controller.put_view(Mobilizon.Web.ErrorView)
|
|
|
|
|> Phoenix.Controller.render("500.html", %{details: details(error)})
|
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp details(:index_not_found) do
|
|
|
|
[dgettext("errors", "Index file not found. You need to recompile the front-end.")]
|
|
|
|
end
|
|
|
|
|
|
|
|
defp details(_) do
|
|
|
|
[]
|
2020-06-18 15:23:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec replace_meta(String.t(), String.t()) :: String.t()
|
|
|
|
defp replace_meta(index_content, tags) do
|
2020-10-12 12:16:36 +02:00
|
|
|
index_content
|
2021-06-06 20:25:16 +02:00
|
|
|
|> String.replace("<meta name=\"server-injected-data\"/>", tags)
|
2020-10-12 12:16:36 +02:00
|
|
|
|> String.replace("<meta name=\"server-injected-data\" />", tags)
|
2020-06-18 15:23:05 +02:00
|
|
|
end
|
|
|
|
|
2021-03-09 19:39:54 +01:00
|
|
|
@spec do_replacements(String.t(), String.t(), String.t()) :: String.t()
|
2020-06-18 15:23:05 +02:00
|
|
|
defp do_replacements(index_content, tags, locale) do
|
|
|
|
index_content
|
|
|
|
|> replace_meta(tags)
|
2021-11-07 21:02:06 +01:00
|
|
|
|> String.replace(
|
2021-11-07 21:02:36 +01:00
|
|
|
~s(<html lang="en" dir="auto">),
|
|
|
|
~s(<html lang="#{locale}" dir="#{get_language_direction(locale)}">)
|
2021-11-07 21:02:06 +01:00
|
|
|
)
|
2020-06-18 15:23:05 +02:00
|
|
|
end
|
|
|
|
|
2021-07-21 15:46:04 +02:00
|
|
|
@spec get_locale(Plug.Conn.t()) :: String.t()
|
|
|
|
def get_locale(%Plug.Conn{assigns: assigns}) do
|
2021-07-27 19:47:54 +02:00
|
|
|
Map.get(assigns, :locale)
|
2021-07-21 15:46:04 +02:00
|
|
|
end
|
2021-11-07 21:02:06 +01:00
|
|
|
|
|
|
|
@ltr_languages ["ar", "ae", "he", "fa", "ku", "ur"]
|
|
|
|
|
|
|
|
@spec get_language_direction(String.t()) :: String.t()
|
|
|
|
defp get_language_direction(locale) when locale in @ltr_languages, do: "rtl"
|
|
|
|
defp get_language_direction(_locale), do: "ltr"
|
2020-06-18 15:23:05 +02:00
|
|
|
end
|