2020-01-26 20:36:50 +00:00
|
|
|
defmodule Mobilizon.Web.PageView do
|
2018-10-11 15:37:39 +00:00
|
|
|
@moduledoc """
|
|
|
|
View for our webapp
|
|
|
|
"""
|
2020-01-22 01:14:42 +00:00
|
|
|
|
2020-01-26 20:36:50 +00:00
|
|
|
use Mobilizon.Web, :view
|
2020-01-22 01:14:42 +00:00
|
|
|
|
2019-05-02 11:04:21 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2020-02-18 07:57:00 +00:00
|
|
|
alias Mobilizon.Conversations.Comment
|
|
|
|
alias Mobilizon.Events.Event
|
|
|
|
alias Mobilizon.Resources.Resource
|
2019-12-03 10:29:51 +00:00
|
|
|
alias Mobilizon.Tombstone
|
2020-01-22 01:14:42 +00:00
|
|
|
|
2019-07-04 15:34:59 +00:00
|
|
|
alias Mobilizon.Service.Metadata
|
2019-10-10 14:47:38 +00:00
|
|
|
alias Mobilizon.Service.Metadata.Instance
|
2020-01-22 01:14:42 +00:00
|
|
|
alias Mobilizon.Service.Metadata.Utils, as: MetadataUtils
|
|
|
|
|
|
|
|
alias Mobilizon.Federation.ActivityPub.Utils
|
2020-01-22 21:40:40 +00:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Convertible
|
2019-05-02 11:04:21 +00:00
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
def render("actor.activity-json", %{conn: %{assigns: %{object: %Actor{} = actor}}}) do
|
|
|
|
actor
|
|
|
|
|> Convertible.model_to_as()
|
2019-05-02 11:04:21 +00:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
def render("event.activity-json", %{conn: %{assigns: %{object: %Event{} = event}}}) do
|
2019-07-30 14:40:59 +00:00
|
|
|
event
|
2019-12-03 10:29:51 +00:00
|
|
|
|> Convertible.model_to_as()
|
2019-05-02 11:04:21 +00:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
def render("event.activity-json", %{conn: %{assigns: %{object: %Tombstone{} = event}}}) do
|
|
|
|
event
|
|
|
|
|> Convertible.model_to_as()
|
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
2019-05-02 11:04:21 +00:00
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
def render("comment.activity-json", %{conn: %{assigns: %{object: %Comment{} = comment}}}) do
|
|
|
|
comment
|
|
|
|
|> Convertible.model_to_as()
|
2019-05-02 11:04:21 +00:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
2019-07-04 15:34:59 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
def render("resource.activity-json", %{conn: %{assigns: %{object: %Resource{} = resource}}}) do
|
|
|
|
resource
|
|
|
|
|> Convertible.model_to_as()
|
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
2019-12-03 10:29:51 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
def render(page, %{object: object, conn: conn} = _assigns)
|
|
|
|
when page in ["actor.html", "event.html", "comment.html"] do
|
|
|
|
tags = object |> Metadata.build_tags()
|
|
|
|
inject_tags(conn, tags)
|
|
|
|
end
|
2019-12-03 10:29:51 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
def render("index.html", %{conn: conn}) do
|
|
|
|
tags = Instance.build_tags()
|
|
|
|
inject_tags(conn, tags)
|
2019-07-04 15:34:59 +00:00
|
|
|
end
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
@spec inject_tags(Conn.t(), List.t()) :: {:safe, String.t()}
|
2020-06-09 14:08:03 +00:00
|
|
|
def inject_tags(conn, tags) do
|
2019-07-04 15:34:59 +00:00
|
|
|
with {:ok, index_content} <- File.read(index_file_path()) do
|
2020-02-18 07:57:00 +00:00
|
|
|
locale = get_locale(conn)
|
2019-12-03 10:29:51 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
do_replacements(index_content, MetadataUtils.stringify_tags(tags), locale)
|
2019-07-04 15:34:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
@spec index_file_path :: String.t()
|
2019-09-22 14:26:23 +00:00
|
|
|
defp index_file_path do
|
2019-07-05 14:59:25 +00:00
|
|
|
Path.join(Application.app_dir(:mobilizon, "priv/static"), "index.html")
|
2019-07-04 15:34:59 +00:00
|
|
|
end
|
2019-12-03 10:29:51 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
@spec replace_meta(String.t(), String.t()) :: String.t()
|
2019-12-03 10:29:51 +00:00
|
|
|
# TODO: Find why it's different in dev/prod and during tests
|
|
|
|
defp replace_meta(index_content, tags) do
|
|
|
|
index_content
|
|
|
|
|> String.replace("<meta name=\"server-injected-data\" />", tags)
|
|
|
|
|> String.replace("<meta name=server-injected-data>", tags)
|
|
|
|
end
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
@spec do_replacements(String.t(), String.t(), String.t()) :: {:safe, String.t()}
|
|
|
|
defp do_replacements(index_content, tags, locale) do
|
|
|
|
index_content
|
|
|
|
|> replace_meta(tags)
|
|
|
|
|> String.replace("<html lang=\"en\">", "<html lang=\"#{locale}\">")
|
|
|
|
|> String.replace("<html lang=en>", "<html lang=\"#{locale}\">")
|
|
|
|
|> (&{:safe, &1}).()
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec get_locale(Conn.t()) :: String.t()
|
|
|
|
defp get_locale(%{private: %{cldr_locale: nil}}), do: "en"
|
|
|
|
defp get_locale(%{private: %{cldr_locale: %{requested_locale_name: locale}}}), do: locale
|
|
|
|
defp get_locale(_), do: "en"
|
2018-10-11 15:37:39 +00:00
|
|
|
end
|