Merge branch 'fix-prod-build-2' into 'master'

Fixes production build through templating refactoring

See merge request framasoft/mobilizon!152
This commit is contained in:
Thomas Citharel 2019-07-04 18:01:15 +02:00
commit 3f9a4c38d6
9 changed files with 46 additions and 28 deletions

View File

@ -8,7 +8,7 @@ config :mobilizon, MobilizonWeb.Endpoint,
], ],
secret_key_base: secret_key_base:
System.get_env("MOBILIZON_SECRET") || "ThisShouldBeAVeryStrongStringPleaseReplaceMe", 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 # Configure your database
config :mobilizon, Mobilizon.Repo, config :mobilizon, Mobilizon.Repo,

View File

@ -22,7 +22,8 @@ defmodule MobilizonWeb.API.Events do
category: category category: category
} = args } = args
) do ) 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), title <- String.trim(title),
mentions <- Formatter.parse_mentions(description), mentions <- Formatter.parse_mentions(description),
visibility <- Map.get(args, :visibility, "public"), visibility <- Map.get(args, :visibility, "public"),

View File

@ -6,6 +6,7 @@ defmodule MobilizonWeb.PageController do
alias Mobilizon.Actors alias Mobilizon.Actors
alias Mobilizon.Events alias Mobilizon.Events
plug(:put_layout, false)
action_fallback(MobilizonWeb.FallbackController) action_fallback(MobilizonWeb.FallbackController)
def index(conn, _params), do: render(conn, :index) def index(conn, _params), do: render(conn, :index)

View File

@ -217,8 +217,13 @@ defmodule MobilizonWeb.Resolvers.Event do
# If we have an attached picture, just transmit it. It will be handled by # If we have an attached picture, just transmit it. It will be handled by
# Mobilizon.Service.ActivityPub.Utils.make_picture_data/1 # Mobilizon.Service.ActivityPub.Utils.make_picture_data/1
# However, we need to pass it's actor ID
@spec save_attached_picture(map()) :: {:ok, map()} @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 # Otherwise if we use a previously uploaded picture we need to fetch it from database
@spec save_attached_picture(map()) :: {:ok, map()} @spec save_attached_picture(map()) :: {:ok, map()}

View File

@ -1,23 +0,0 @@
<!DOCTYPE html>
<html class="has-navbar-fixed-top">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= static_path(@conn, "/js/favicon.ico") %>">
<link rel="stylesheet" href="//cdn.materialdesignicons.com/3.5.95/css/materialdesignicons.min.css">
<title>mobilizon</title>
<%= if assigns[:object], do: Metadata.build_tags(@object) %>
</head>
<body>
<noscript>
<strong>We're sorry but mobilizon doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
<script src="<%= static_path(@conn, "/js/app.js") %>"></script>
</body>
</html>

View File

@ -1,4 +1,3 @@
defmodule MobilizonWeb.LayoutView do defmodule MobilizonWeb.LayoutView do
use MobilizonWeb, :view use MobilizonWeb, :view
alias Mobilizon.Service.Metadata
end end

View File

@ -5,6 +5,8 @@ defmodule MobilizonWeb.PageView do
use MobilizonWeb, :view use MobilizonWeb, :view
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Service.ActivityPub.Utils alias Mobilizon.Service.ActivityPub.Utils
alias Mobilizon.Service.Metadata
alias Mobilizon.Service.MetadataUtils
def render("actor.activity-json", %{conn: %{assigns: %{object: actor}}}) do def render("actor.activity-json", %{conn: %{assigns: %{object: actor}}}) do
public_key = Mobilizon.Service.ActivityPub.Utils.pem_to_public_key_pem(actor.keys) 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()) |> Map.merge(Utils.make_json_ld_header())
end 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, "<!--server-generated-meta-->", 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 end

View File

@ -208,7 +208,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
@doc """ @doc """
Save picture data from raw data and return AS Link data. 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}} <- with {:ok, %{"url" => [%{"href" => url, "mediaType" => content_type}], "size" => size}} <-
MobilizonWeb.Upload.store(picture.file), MobilizonWeb.Upload.store(picture.file),
{:ok, %Picture{file: _file} = pic} <- {:ok, %Picture{file: _file} = pic} <-

View File

@ -4,3 +4,17 @@ defprotocol Mobilizon.Service.Metadata do
""" """
def build_tags(entity) def build_tags(entity)
end 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