2018-10-11 15:37:39 +00:00
|
|
|
defmodule MobilizonWeb.PageView do
|
|
|
|
@moduledoc """
|
|
|
|
View for our webapp
|
|
|
|
"""
|
|
|
|
use MobilizonWeb, :view
|
2019-05-02 11:04:21 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-09-22 16:29:13 +00:00
|
|
|
alias Mobilizon.Service.ActivityPub.{Converter, Utils}
|
2019-07-04 15:34:59 +00:00
|
|
|
alias Mobilizon.Service.Metadata
|
|
|
|
alias Mobilizon.Service.MetadataUtils
|
2019-10-10 14:47:38 +00:00
|
|
|
alias Mobilizon.Service.Metadata.Instance
|
2019-05-02 11:04:21 +00:00
|
|
|
|
|
|
|
def render("actor.activity-json", %{conn: %{assigns: %{object: actor}}}) do
|
2019-09-22 16:29:13 +00:00
|
|
|
public_key = Utils.pem_to_public_key_pem(actor.keys)
|
2019-05-02 11:04:21 +00:00
|
|
|
|
|
|
|
%{
|
|
|
|
"id" => Actor.build_url(actor.preferred_username, :page),
|
|
|
|
"type" => "Person",
|
|
|
|
"following" => Actor.build_url(actor.preferred_username, :following),
|
|
|
|
"followers" => Actor.build_url(actor.preferred_username, :followers),
|
|
|
|
"inbox" => Actor.build_url(actor.preferred_username, :inbox),
|
|
|
|
"outbox" => Actor.build_url(actor.preferred_username, :outbox),
|
|
|
|
"preferredUsername" => actor.preferred_username,
|
|
|
|
"name" => actor.name,
|
|
|
|
"summary" => actor.summary,
|
|
|
|
"url" => actor.url,
|
|
|
|
"manuallyApprovesFollowers" => actor.manually_approves_followers,
|
|
|
|
"publicKey" => %{
|
|
|
|
"id" => "#{actor.url}#main-key",
|
|
|
|
"owner" => actor.url,
|
|
|
|
"publicKeyPem" => public_key
|
|
|
|
},
|
|
|
|
# TODO : Make have actors have an uuid
|
|
|
|
# "uuid" => actor.uuid
|
|
|
|
"endpoints" => %{
|
|
|
|
"sharedInbox" => actor.shared_inbox_url
|
|
|
|
}
|
|
|
|
# "icon" => %{
|
|
|
|
# "type" => "Image",
|
|
|
|
# "url" => User.avatar_url(actor)
|
|
|
|
# },
|
|
|
|
# "image" => %{
|
|
|
|
# "type" => "Image",
|
|
|
|
# "url" => User.banner_url(actor)
|
|
|
|
# }
|
|
|
|
}
|
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("event.activity-json", %{conn: %{assigns: %{object: event}}}) do
|
2019-07-30 14:40:59 +00:00
|
|
|
event
|
2019-09-22 16:29:13 +00:00
|
|
|
|> Converter.Event.model_to_as()
|
2019-05-02 11:04:21 +00:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("comment.activity-json", %{conn: %{assigns: %{object: comment}}}) do
|
2019-09-22 16:29:13 +00:00
|
|
|
comment = Converter.Comment.model_to_as(comment)
|
2019-05-02 11:04:21 +00:00
|
|
|
|
|
|
|
%{
|
|
|
|
"actor" => comment["actor"],
|
|
|
|
"uuid" => comment["uuid"],
|
|
|
|
# The activity should have attributedTo, not the comment itself
|
|
|
|
# "attributedTo" => comment.attributed_to,
|
|
|
|
"type" => "Note",
|
|
|
|
"id" => comment["id"],
|
|
|
|
"content" => comment["content"],
|
|
|
|
"mediaType" => "text/html"
|
|
|
|
# "published" => Timex.format!(comment.inserted_at, "{ISO:Extended}"),
|
|
|
|
# "updated" => Timex.format!(comment.updated_at, "{ISO:Extended}")
|
|
|
|
}
|
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
2019-07-04 15:34:59 +00: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()
|
|
|
|
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
|
2019-10-10 14:47:38 +00:00
|
|
|
tags = Instance.build_tags() |> MetadataUtils.stringify_tags()
|
|
|
|
index_content = String.replace(index_content, "<!--server-generated-meta-->", tags)
|
2019-07-04 15:34:59 +00:00
|
|
|
{:safe, index_content}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
2018-10-11 15:37:39 +00:00
|
|
|
end
|