From 2749989a287d2fcd7a72a638424b591c10ab4fcb Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 4 Jul 2019 17:34:59 +0200 Subject: [PATCH] Drop the eex template and directly use the webpack index.html Partly reverts ff7fd460 Signed-off-by: Thomas Citharel --- config/prod.exs | 2 +- .../controllers/page_controller.ex | 1 + .../templates/layout/app.html.eex | 23 ------------------- lib/mobilizon_web/views/layout_view.ex | 1 - lib/mobilizon_web/views/page_view.ex | 21 +++++++++++++++++ lib/service/metadata.ex | 14 +++++++++++ 6 files changed, 37 insertions(+), 25 deletions(-) delete mode 100644 lib/mobilizon_web/templates/layout/app.html.eex diff --git a/config/prod.exs b/config/prod.exs index cdbf50a39..dde2a1fe8 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -8,7 +8,7 @@ config :mobilizon, MobilizonWeb.Endpoint, ], secret_key_base: System.get_env("MOBILIZON_SECRET") || "ThisShouldBeAVeryStrongStringPleaseReplaceMe", - cache_static_manifest: "priv/static/cache_manifest.json" + cache_static_manifest: "priv/static/js/manifest.json" # Configure your database config :mobilizon, Mobilizon.Repo, diff --git a/lib/mobilizon_web/controllers/page_controller.ex b/lib/mobilizon_web/controllers/page_controller.ex index bea116c7d..0c608a6df 100644 --- a/lib/mobilizon_web/controllers/page_controller.ex +++ b/lib/mobilizon_web/controllers/page_controller.ex @@ -6,6 +6,7 @@ defmodule MobilizonWeb.PageController do alias Mobilizon.Actors alias Mobilizon.Events + plug(:put_layout, false) action_fallback(MobilizonWeb.FallbackController) def index(conn, _params), do: render(conn, :index) diff --git a/lib/mobilizon_web/templates/layout/app.html.eex b/lib/mobilizon_web/templates/layout/app.html.eex deleted file mode 100644 index 386e2ab10..000000000 --- a/lib/mobilizon_web/templates/layout/app.html.eex +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - "> - - mobilizon - <%= if assigns[:object], do: Metadata.build_tags(@object) %> - - - - -
- - - - - diff --git a/lib/mobilizon_web/views/layout_view.ex b/lib/mobilizon_web/views/layout_view.ex index 6e91e14cf..961a8471d 100644 --- a/lib/mobilizon_web/views/layout_view.ex +++ b/lib/mobilizon_web/views/layout_view.ex @@ -1,4 +1,3 @@ defmodule MobilizonWeb.LayoutView do use MobilizonWeb, :view - alias Mobilizon.Service.Metadata end diff --git a/lib/mobilizon_web/views/page_view.ex b/lib/mobilizon_web/views/page_view.ex index dd9825b73..6ec5c2f65 100644 --- a/lib/mobilizon_web/views/page_view.ex +++ b/lib/mobilizon_web/views/page_view.ex @@ -5,6 +5,8 @@ defmodule MobilizonWeb.PageView do use MobilizonWeb, :view alias Mobilizon.Actors.Actor alias Mobilizon.Service.ActivityPub.Utils + alias Mobilizon.Service.Metadata + alias Mobilizon.Service.MetadataUtils def render("actor.activity-json", %{conn: %{assigns: %{object: actor}}}) do public_key = Mobilizon.Service.ActivityPub.Utils.pem_to_public_key_pem(actor.keys) @@ -82,4 +84,23 @@ defmodule MobilizonWeb.PageView do } |> Map.merge(Utils.make_json_ld_header()) end + + def render(page, %{object: object} = _assigns) + when page in ["actor.html", "event.html", "comment.html"] do + with {:ok, index_content} <- File.read(index_file_path()) do + tags = object |> Metadata.build_tags() |> MetadataUtils.stringify_tags() + index_content = String.replace(index_content, "", tags) + {:safe, index_content} + end + end + + def render("index.html", _assigns) do + with {:ok, index_content} <- File.read(index_file_path()) do + {:safe, index_content} + end + end + + defp index_file_path() do + Path.join(Application.app_dir(:mobilizon, "priv/static/js"), "index.html") + end end diff --git a/lib/service/metadata.ex b/lib/service/metadata.ex index bd3ba99b1..e139e503b 100644 --- a/lib/service/metadata.ex +++ b/lib/service/metadata.ex @@ -4,3 +4,17 @@ defprotocol Mobilizon.Service.Metadata do """ def build_tags(entity) end + +defmodule Mobilizon.Service.MetadataUtils do + @moduledoc """ + Tools to convert tags to string + """ + alias Phoenix.HTML + + def stringify_tags(tags) do + Enum.reduce(tags, "", &stringify_tag/2) + end + + defp stringify_tag(tag, acc) when is_tuple(tag), do: acc <> HTML.safe_to_string(tag) + defp stringify_tag(tag, acc) when is_binary(tag), do: acc <> tag +end