2018-10-11 17:37:39 +02:00
|
|
|
defmodule MobilizonWeb.PageView do
|
|
|
|
@moduledoc """
|
|
|
|
View for our webapp
|
|
|
|
"""
|
|
|
|
use MobilizonWeb, :view
|
2019-05-02 13:04:21 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-12-03 11:29:51 +01:00
|
|
|
alias Mobilizon.Tombstone
|
|
|
|
alias Mobilizon.Service.ActivityPub.{Convertible, Utils}
|
2019-07-04 17:34:59 +02:00
|
|
|
alias Mobilizon.Service.Metadata
|
|
|
|
alias Mobilizon.Service.MetadataUtils
|
2019-10-10 16:47:38 +02:00
|
|
|
alias Mobilizon.Service.Metadata.Instance
|
2019-12-03 11:29:51 +01:00
|
|
|
alias Mobilizon.Events.{Comment, Event}
|
2019-05-02 13:04:21 +02:00
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
def render("actor.activity-json", %{conn: %{assigns: %{object: %Actor{} = actor}}}) do
|
|
|
|
actor
|
|
|
|
|> Convertible.model_to_as()
|
2019-05-02 13:04:21 +02:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
def render("event.activity-json", %{conn: %{assigns: %{object: %Event{} = event}}}) do
|
2019-07-30 16:40:59 +02:00
|
|
|
event
|
2019-12-03 11:29:51 +01:00
|
|
|
|> Convertible.model_to_as()
|
2019-05-02 13:04:21 +02:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
2019-12-03 11:29:51 +01: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 13:04:21 +02:00
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
def render("comment.activity-json", %{conn: %{assigns: %{object: %Comment{} = comment}}}) do
|
|
|
|
comment
|
|
|
|
|> Convertible.model_to_as()
|
2019-05-02 13:04:21 +02:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
2019-07-04 17:34:59 +02:00
|
|
|
|
|
|
|
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()
|
2019-12-03 11:29:51 +01:00
|
|
|
|
|
|
|
index_content = replace_meta(index_content, tags)
|
|
|
|
|
2019-07-04 17:34:59 +02:00
|
|
|
{:safe, index_content}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("index.html", _assigns) do
|
|
|
|
with {:ok, index_content} <- File.read(index_file_path()) do
|
2019-10-10 16:47:38 +02:00
|
|
|
tags = Instance.build_tags() |> MetadataUtils.stringify_tags()
|
2019-12-03 11:29:51 +01:00
|
|
|
|
|
|
|
index_content = replace_meta(index_content, tags)
|
|
|
|
|
2019-07-04 17:34:59 +02:00
|
|
|
{:safe, index_content}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-22 16:26:23 +02:00
|
|
|
defp index_file_path do
|
2019-07-05 16:59:25 +02:00
|
|
|
Path.join(Application.app_dir(:mobilizon, "priv/static"), "index.html")
|
2019-07-04 17:34:59 +02:00
|
|
|
end
|
2019-12-03 11:29:51 +01: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
|
2018-10-11 17:37:39 +02:00
|
|
|
end
|