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-09-17 21:39:26 +00:00
|
|
|
|
|
|
|
alias MobilizonWeb.Cache
|
2018-01-13 22:33:03 +00:00
|
|
|
|
2019-07-04 15:34:59 +00:00
|
|
|
plug(:put_layout, false)
|
2019-03-05 09:13:19 +00:00
|
|
|
action_fallback(MobilizonWeb.FallbackController)
|
2017-12-08 08:58:14 +00:00
|
|
|
|
2019-05-02 11:04:21 +00:00
|
|
|
def index(conn, _params), do: render(conn, :index)
|
2019-03-04 16:20:18 +00:00
|
|
|
|
|
|
|
def actor(conn, %{"name" => name}) do
|
2019-09-17 21:39:26 +00:00
|
|
|
{status, actor} = Cache.get_local_actor_by_name(name)
|
2019-05-02 11:04:21 +00:00
|
|
|
render_or_error(conn, &ok_status?/2, status, :actor, actor)
|
2019-03-04 16:20:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def event(conn, %{"uuid" => uuid}) do
|
2019-09-17 21:39:26 +00:00
|
|
|
{status, event} = Cache.get_public_event_by_uuid_with_preload(uuid)
|
2019-05-02 11:04:21 +00:00
|
|
|
render_or_error(conn, &ok_status_and_is_visible?/2, status, :event, event)
|
2019-03-04 16:20:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def comment(conn, %{"uuid" => uuid}) do
|
2019-09-17 21:39:26 +00:00
|
|
|
{status, comment} = Cache.get_comment_by_uuid_with_preload(uuid)
|
2019-05-02 11:04:21 +00:00
|
|
|
render_or_error(conn, &ok_status_and_is_visible?/2, status, :comment, comment)
|
|
|
|
end
|
2019-03-04 16:20:18 +00:00
|
|
|
|
2019-05-02 11:04:21 +00:00
|
|
|
defp render_or_error(conn, check_fn, status, object_type, object) do
|
|
|
|
if check_fn.(status, object) do
|
|
|
|
render(conn, object_type, object: object)
|
|
|
|
else
|
|
|
|
{:error, :not_found}
|
2019-03-04 16:20:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-02 11:04:21 +00:00
|
|
|
defp is_visible?(%{visibility: v}), do: v in [:public, :unlisted]
|
|
|
|
|
|
|
|
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)
|
2017-12-08 08:58:14 +00:00
|
|
|
end
|