mobilizon/lib/mobilizon_web/controllers/page_controller.ex

53 lines
1.5 KiB
Elixir
Raw Normal View History

defmodule MobilizonWeb.PageController do
@moduledoc """
Controller to load our webapp
"""
use MobilizonWeb, :controller
2019-09-17 23:39:26 +02:00
alias MobilizonWeb.Cache
plug(:put_layout, false)
action_fallback(MobilizonWeb.FallbackController)
2019-05-02 13:04:21 +02:00
def index(conn, _params), do: render(conn, :index)
def actor(conn, %{"name" => name}) do
2019-09-17 23:39:26 +02:00
{status, actor} = Cache.get_local_actor_by_name(name)
2019-05-02 13:04:21 +02:00
render_or_error(conn, &ok_status?/2, status, :actor, actor)
end
def event(conn, %{"uuid" => uuid}) do
2019-09-17 23:39:26 +02:00
{status, event} = Cache.get_public_event_by_uuid_with_preload(uuid)
2019-05-02 13:04:21 +02:00
render_or_error(conn, &ok_status_and_is_visible?/2, status, :event, event)
end
def comment(conn, %{"uuid" => uuid}) do
2019-09-17 23:39:26 +02:00
{status, comment} = Cache.get_comment_by_uuid_with_preload(uuid)
2019-05-02 13:04:21 +02:00
render_or_error(conn, &ok_status_and_is_visible?/2, status, :comment, comment)
end
2019-05-02 13:04:21 +02:00
defp render_or_error(conn, check_fn, status, object_type, object) do
if check_fn.(status, object) do
case object do
%Mobilizon.Tombstone{} ->
conn
|> put_status(:gone)
|> render(object_type, object: object)
_ ->
render(conn, object_type, object: object)
end
2019-05-02 13:04:21 +02:00
else
{:error, :not_found}
end
end
2019-05-02 13:04:21 +02:00
defp is_visible?(%{visibility: v}), do: v in [:public, :unlisted]
defp is_visible?(%Mobilizon.Tombstone{}), do: true
2019-05-02 13:04:21 +02:00
defp ok_status?(status), do: status in [:ok, :commit]
defp ok_status?(status, _), do: ok_status?(status)
defp ok_status_and_is_visible?(status, o), do: ok_status?(status) and is_visible?(o)
end