2018-10-11 15:37:39 +00:00
|
|
|
defmodule MobilizonWeb.PageController do
|
2018-01-14 16:56:50 +00:00
|
|
|
@moduledoc """
|
|
|
|
Controller to load our webapp
|
|
|
|
"""
|
2018-10-11 15:37:39 +00:00
|
|
|
use MobilizonWeb, :controller
|
2019-03-04 16:20:18 +00:00
|
|
|
alias Mobilizon.Service.Metadata
|
|
|
|
alias Mobilizon.Actors
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Events
|
|
|
|
alias Mobilizon.Events.{Event, Comment}
|
2018-01-13 22:33:03 +00:00
|
|
|
|
2018-07-27 08:45:35 +00:00
|
|
|
plug(:put_layout, false)
|
2017-12-08 08:58:14 +00:00
|
|
|
|
|
|
|
def index(conn, _params) do
|
2018-10-10 12:57:35 +00:00
|
|
|
conn
|
2018-10-11 15:47:02 +00:00
|
|
|
|> put_resp_content_type("text/html")
|
|
|
|
|> send_file(200, "priv/static/index.html")
|
2017-12-08 08:58:14 +00:00
|
|
|
end
|
2019-03-04 16:20:18 +00:00
|
|
|
|
|
|
|
def actor(conn, %{"name" => name}) do
|
|
|
|
case get_format(conn) do
|
|
|
|
"html" ->
|
|
|
|
with {status, %Actor{} = actor} when status in [:ok, :commit] <-
|
|
|
|
Actors.get_cached_local_actor_by_name(name) do
|
|
|
|
render_with_meta(conn, actor)
|
|
|
|
end
|
|
|
|
|
|
|
|
"activity+json" ->
|
|
|
|
MobilizonWeb.ActivityPubController.call(conn, :actor)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
{:error, :not_found}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def event(conn, %{"uuid" => uuid}) do
|
|
|
|
case get_format(conn) do
|
|
|
|
"html" ->
|
|
|
|
with {status, %Event{} = event} when status in [:ok, :commit] <-
|
|
|
|
Events.get_cached_event_full_by_uuid(uuid),
|
|
|
|
true <- event.visibility in [:public, :unlisted] do
|
|
|
|
render_with_meta(conn, event)
|
|
|
|
end
|
|
|
|
|
|
|
|
"activity+json" ->
|
|
|
|
MobilizonWeb.ActivityPubController.call(conn, :event)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
{:error, :not_found}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def comment(conn, %{"uuid" => uuid}) do
|
|
|
|
case get_format(conn) do
|
|
|
|
"html" ->
|
|
|
|
with {status, %Comment{} = comment} when status in [:ok, :commit] <-
|
|
|
|
Events.get_cached_comment_full_by_uuid(uuid) do
|
|
|
|
# Comments are always public for now
|
|
|
|
# TODO : Make comments maybe restricted
|
|
|
|
# true <- comment.public do
|
|
|
|
render_with_meta(conn, comment)
|
|
|
|
end
|
|
|
|
|
|
|
|
"activity+json" ->
|
|
|
|
MobilizonWeb.ActivityPubController.call(conn, :comment)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
{:error, :not_found}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Inject OpenGraph information
|
|
|
|
defp render_with_meta(conn, object) do
|
|
|
|
{:ok, index_content} = File.read(index_file_path())
|
|
|
|
tags = Metadata.build_tags(object)
|
|
|
|
response = String.replace(index_content, "<!--server-generated-meta-->", tags)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_resp_content_type("text/html")
|
|
|
|
|> send_resp(200, response)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp index_file_path() do
|
|
|
|
Path.join(Application.app_dir(:mobilizon, "priv/static/"), "index.html")
|
|
|
|
end
|
2017-12-08 08:58:14 +00:00
|
|
|
end
|