From ad1730a3a3c0631cbf9dfbd147ae3419c089a9c6 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 4 Jul 2019 17:34:59 +0200 Subject: [PATCH 1/2] 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 From 443dbb7f8104511184ebebf49d58bb652eea8963 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 4 Jul 2019 17:41:06 +0200 Subject: [PATCH 2/2] Upload pictures fixes Signed-off-by: Thomas Citharel --- lib/mobilizon_web/api/events.ex | 3 ++- lib/mobilizon_web/resolvers/event.ex | 7 ++++++- lib/service/activity_pub/utils.ex | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/mobilizon_web/api/events.ex b/lib/mobilizon_web/api/events.ex index 58307d49f..7adb7ac01 100644 --- a/lib/mobilizon_web/api/events.ex +++ b/lib/mobilizon_web/api/events.ex @@ -22,7 +22,8 @@ defmodule MobilizonWeb.API.Events do category: category } = args ) do - with %Actor{url: url} = actor <- Actors.get_local_actor_with_everything(organizer_actor_id), + with %Actor{url: url} = actor <- + Actors.get_local_actor_with_everything(organizer_actor_id), title <- String.trim(title), mentions <- Formatter.parse_mentions(description), visibility <- Map.get(args, :visibility, "public"), diff --git a/lib/mobilizon_web/resolvers/event.ex b/lib/mobilizon_web/resolvers/event.ex index 3f0c3f29d..c53a47a86 100644 --- a/lib/mobilizon_web/resolvers/event.ex +++ b/lib/mobilizon_web/resolvers/event.ex @@ -217,8 +217,13 @@ defmodule MobilizonWeb.Resolvers.Event do # If we have an attached picture, just transmit it. It will be handled by # Mobilizon.Service.ActivityPub.Utils.make_picture_data/1 + # However, we need to pass it's actor ID @spec save_attached_picture(map()) :: {:ok, map()} - defp save_attached_picture(%{picture: %{picture: %Plug.Upload{} = _picture}} = args), do: args + defp save_attached_picture( + %{picture: %{picture: %{file: %Plug.Upload{} = _picture} = all_pic}} = args + ) do + {:ok, Map.put(args, :picture, Map.put(all_pic, :actor_id, args.organizer_actor_id))} + end # Otherwise if we use a previously uploaded picture we need to fetch it from database @spec save_attached_picture(map()) :: {:ok, map()} diff --git a/lib/service/activity_pub/utils.ex b/lib/service/activity_pub/utils.ex index fdd469eee..7c02e82a9 100644 --- a/lib/service/activity_pub/utils.ex +++ b/lib/service/activity_pub/utils.ex @@ -208,7 +208,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do @doc """ Save picture data from raw data and return AS Link data. """ - def make_picture_data(%{picture: picture}) do + def make_picture_data(picture) when is_map(picture) do with {:ok, %{"url" => [%{"href" => url, "mediaType" => content_type}], "size" => size}} <- MobilizonWeb.Upload.store(picture.file), {:ok, %Picture{file: _file} = pic} <-