2020-01-26 21:36:50 +01:00
|
|
|
defmodule Mobilizon.Web.PageController do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
|
|
|
Controller to load our webapp
|
|
|
|
"""
|
2020-01-26 21:36:50 +01:00
|
|
|
use Mobilizon.Web, :controller
|
2019-09-17 23:39:26 +02:00
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
alias Mobilizon.Events.{Comment, Event}
|
|
|
|
alias Mobilizon.Federation.ActivityPub
|
|
|
|
alias Mobilizon.Tombstone
|
2020-01-26 21:36:50 +01:00
|
|
|
alias Mobilizon.Web.Cache
|
2018-01-13 23:33:03 +01:00
|
|
|
|
2019-07-04 17:34:59 +02:00
|
|
|
plug(:put_layout, false)
|
2020-01-26 21:36:50 +01:00
|
|
|
action_fallback(Mobilizon.Web.FallbackController)
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2019-05-02 13:04:21 +02:00
|
|
|
def index(conn, _params), do: render(conn, :index)
|
2019-03-04 17:20:18 +01:00
|
|
|
|
|
|
|
def actor(conn, %{"name" => name}) do
|
2019-09-17 23:39:26 +02:00
|
|
|
{status, actor} = Cache.get_local_actor_by_name(name)
|
2019-12-20 13:04:34 +01:00
|
|
|
render_or_error(conn, &ok_status?/3, status, :actor, actor)
|
2019-03-04 17:20:18 +01:00
|
|
|
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-12-20 13:04:34 +01:00
|
|
|
render_or_error(conn, &checks?/3, status, :event, event)
|
2019-03-04 17:20:18 +01:00
|
|
|
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-12-20 13:04:34 +01:00
|
|
|
render_or_error(conn, &checks?/3, status, :comment, comment)
|
|
|
|
end
|
|
|
|
|
|
|
|
def interact(conn, %{"uri" => uri}) do
|
|
|
|
case ActivityPub.fetch_object_from_url(uri) do
|
|
|
|
{:ok, %Event{uuid: uuid}} -> redirect(conn, to: "/events/#{uuid}")
|
|
|
|
{:ok, %Comment{uuid: uuid}} -> redirect(conn, to: "/comments/#{uuid}")
|
|
|
|
_ -> {:error, :not_found}
|
|
|
|
end
|
2019-05-02 13:04:21 +02:00
|
|
|
end
|
2019-03-04 17:20:18 +01:00
|
|
|
|
2019-05-02 13:04:21 +02:00
|
|
|
defp render_or_error(conn, check_fn, status, object_type, object) do
|
2019-12-20 13:04:34 +01:00
|
|
|
case check_fn.(conn, status, object) do
|
|
|
|
true ->
|
|
|
|
case object do
|
|
|
|
%Tombstone{} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:gone)
|
|
|
|
|> render(object_type, object: object)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
render(conn, object_type, object: object)
|
|
|
|
end
|
|
|
|
|
|
|
|
:remote ->
|
|
|
|
redirect(conn, external: object.url)
|
|
|
|
|
|
|
|
false ->
|
|
|
|
{:error, :not_found}
|
2019-03-04 17:20:18 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-02 13:04:21 +02:00
|
|
|
defp is_visible?(%{visibility: v}), do: v in [:public, :unlisted]
|
2019-12-20 13:04:34 +01:00
|
|
|
defp is_visible?(%Tombstone{}), do: true
|
2019-05-02 13:04:21 +02:00
|
|
|
|
|
|
|
defp ok_status?(status), do: status in [:ok, :commit]
|
2019-12-20 13:04:34 +01:00
|
|
|
defp ok_status?(_conn, status, _), do: ok_status?(status)
|
|
|
|
|
|
|
|
defp ok_status_and_is_visible?(_conn, status, o),
|
|
|
|
do: ok_status?(status) and is_visible?(o)
|
|
|
|
|
|
|
|
defp checks?(conn, status, o) do
|
|
|
|
if ok_status_and_is_visible?(conn, status, o) do
|
|
|
|
if is_local?(o) == :remote && get_format(conn) == "activity-json", do: :remote, else: true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2019-05-02 13:04:21 +02:00
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
defp is_local?(%Event{local: local}), do: if(local, do: true, else: :remote)
|
|
|
|
defp is_local?(%Comment{local: local}), do: if(local, do: true, else: :remote)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|