Simplify PageController
This commit is contained in:
@@ -16,74 +16,6 @@ defmodule MobilizonWeb.ActivityPubController do
|
||||
|
||||
action_fallback(:errors)
|
||||
|
||||
@doc """
|
||||
Renders an Actor ActivityPub's representation
|
||||
"""
|
||||
@spec actor(Plug.Conn.t(), String.t()) :: Plug.Conn.t()
|
||||
def actor(conn, %{"name" => name}) do
|
||||
with {status, %Actor{} = actor} when status in [:ok, :commit] <-
|
||||
Actors.get_cached_local_actor_by_name(name) do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|> json(ActorView.render("actor.json", %{actor: actor}))
|
||||
else
|
||||
{:ignore, _} ->
|
||||
{:error, :not_found}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Renders an Event ActivityPub's representation
|
||||
"""
|
||||
@spec event(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
||||
def event(conn, %{"uuid" => uuid}) do
|
||||
with {status, %Event{} = event} when status in [:ok, :commit] <-
|
||||
Events.get_cached_event_full_by_uuid(uuid),
|
||||
true <- event.visibility in [:public, :unlisted] do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|> json(
|
||||
ObjectView.render(
|
||||
"event.json",
|
||||
%{
|
||||
event:
|
||||
event
|
||||
|> Utils.make_event_data()
|
||||
}
|
||||
)
|
||||
)
|
||||
else
|
||||
{:ignore, _} ->
|
||||
{:error, :not_found}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Renders a Comment ActivityPub's representation
|
||||
"""
|
||||
@spec comment(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
||||
def comment(conn, %{"uuid" => uuid}) do
|
||||
with {status, %Comment{} = comment} when status in [:ok, :commit] <-
|
||||
Events.get_cached_comment_full_by_uuid(uuid),
|
||||
true <- comment.visibility in [:public, :unlisted] do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|> json(
|
||||
ObjectView.render(
|
||||
"comment.json",
|
||||
%{
|
||||
comment:
|
||||
comment
|
||||
|> Utils.make_comment_data()
|
||||
}
|
||||
)
|
||||
)
|
||||
else
|
||||
_ ->
|
||||
{:error, :not_found}
|
||||
end
|
||||
end
|
||||
|
||||
def following(conn, %{"name" => name, "page" => page}) do
|
||||
with {page, ""} = Integer.parse(page),
|
||||
%Actor{} = actor <- Actors.get_local_actor_by_name_with_everything(name) do
|
||||
|
||||
@@ -10,6 +10,6 @@ defmodule MobilizonWeb.FallbackController do
|
||||
conn
|
||||
|> put_status(:not_found)
|
||||
|> put_view(MobilizonWeb.ErrorView)
|
||||
|> render("404.html")
|
||||
|> render(:"404")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,69 +10,35 @@ defmodule MobilizonWeb.PageController do
|
||||
|
||||
action_fallback(MobilizonWeb.FallbackController)
|
||||
|
||||
def index(conn, _params) do
|
||||
render(conn, "app.html")
|
||||
end
|
||||
def index(conn, _params), do: render(conn, :index)
|
||||
|
||||
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)
|
||||
else
|
||||
_ -> {:error, :not_found}
|
||||
end
|
||||
|
||||
# "activity-json" matches "application/activity+json" inside our config
|
||||
"activity-json" ->
|
||||
MobilizonWeb.ActivityPubController.call(conn, :actor)
|
||||
|
||||
_ ->
|
||||
{:error, :not_found}
|
||||
end
|
||||
{status, actor} = Actors.get_cached_local_actor_by_name(name)
|
||||
render_or_error(conn, &ok_status?/2, status, :actor, actor)
|
||||
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)
|
||||
else
|
||||
_ -> {:error, :not_found}
|
||||
end
|
||||
|
||||
"activity-json" ->
|
||||
MobilizonWeb.ActivityPubController.call(conn, :event)
|
||||
|
||||
_ ->
|
||||
{:error, :not_found}
|
||||
end
|
||||
{status, event} = Events.get_cached_event_full_by_uuid(uuid)
|
||||
render_or_error(conn, &ok_status_and_is_visible?/2, status, :event, event)
|
||||
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),
|
||||
true <- comment.visibility in [:public, :unlisted] do
|
||||
render_with_meta(conn, comment)
|
||||
else
|
||||
_ -> {:error, :not_found}
|
||||
end
|
||||
{status, comment} = Events.get_cached_comment_full_by_uuid(uuid)
|
||||
render_or_error(conn, &ok_status_and_is_visible?/2, status, :comment, comment)
|
||||
end
|
||||
|
||||
"activity-json" ->
|
||||
MobilizonWeb.ActivityPubController.call(conn, :comment)
|
||||
|
||||
_ ->
|
||||
{:error, :not_found}
|
||||
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}
|
||||
end
|
||||
end
|
||||
|
||||
# Inject OpenGraph information
|
||||
defp render_with_meta(conn, object) do
|
||||
render(conn, "app.html", object: object)
|
||||
end
|
||||
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)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user