Simplify PageController

This commit is contained in:
Vincent 2019-05-02 13:04:21 +02:00 committed by Thomas Citharel
parent fb0e9c42f8
commit 7f31121880
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
8 changed files with 109 additions and 164 deletions

View file

@ -64,7 +64,7 @@ config :geolix,
config :arc, config :arc,
storage: Arc.Storage.Local storage: Arc.Storage.Local
config :phoenix, :format_encoders, json: Jason config :phoenix, :format_encoders, json: Jason, "activity-json": Jason
config :mobilizon, Mobilizon.Service.Geospatial.Nominatim, config :mobilizon, Mobilizon.Service.Geospatial.Nominatim,
endpoint: endpoint:

View file

@ -16,74 +16,6 @@ defmodule MobilizonWeb.ActivityPubController do
action_fallback(:errors) 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 def following(conn, %{"name" => name, "page" => page}) do
with {page, ""} = Integer.parse(page), with {page, ""} = Integer.parse(page),
%Actor{} = actor <- Actors.get_local_actor_by_name_with_everything(name) do %Actor{} = actor <- Actors.get_local_actor_by_name_with_everything(name) do

View file

@ -10,6 +10,6 @@ defmodule MobilizonWeb.FallbackController do
conn conn
|> put_status(:not_found) |> put_status(:not_found)
|> put_view(MobilizonWeb.ErrorView) |> put_view(MobilizonWeb.ErrorView)
|> render("404.html") |> render(:"404")
end end
end end

View file

@ -10,69 +10,35 @@ defmodule MobilizonWeb.PageController do
action_fallback(MobilizonWeb.FallbackController) action_fallback(MobilizonWeb.FallbackController)
def index(conn, _params) do def index(conn, _params), do: render(conn, :index)
render(conn, "app.html")
end
def actor(conn, %{"name" => name}) do def actor(conn, %{"name" => name}) do
case get_format(conn) do {status, actor} = Actors.get_cached_local_actor_by_name(name)
"html" -> render_or_error(conn, &ok_status?/2, status, :actor, actor)
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
end end
def event(conn, %{"uuid" => uuid}) do def event(conn, %{"uuid" => uuid}) do
case get_format(conn) do {status, event} = Events.get_cached_event_full_by_uuid(uuid)
"html" -> render_or_error(conn, &ok_status_and_is_visible?/2, status, :event, event)
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
end end
def comment(conn, %{"uuid" => uuid}) do def comment(conn, %{"uuid" => uuid}) do
case get_format(conn) do {status, comment} = Events.get_cached_comment_full_by_uuid(uuid)
"html" -> render_or_error(conn, &ok_status_and_is_visible?/2, status, :comment, comment)
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 end
"activity-json" -> defp render_or_error(conn, check_fn, status, object_type, object) do
MobilizonWeb.ActivityPubController.call(conn, :comment) if check_fn.(status, object) do
render(conn, object_type, object: object)
_ -> else
{:error, :not_found} {:error, :not_found}
end end
end end
# Inject OpenGraph information defp is_visible?(%{visibility: v}), do: v in [:public, :unlisted]
defp render_with_meta(conn, object) do
render(conn, "app.html", object: object) defp ok_status?(status), do: status in [:ok, :commit]
end 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 end

View file

@ -3,45 +3,6 @@ defmodule MobilizonWeb.ActivityPub.ObjectView do
alias Mobilizon.Service.ActivityPub.Utils alias Mobilizon.Service.ActivityPub.Utils
alias Mobilizon.Activity alias Mobilizon.Activity
def render("event.json", %{event: event}) do
{:ok, html, []} = Earmark.as_html(event["summary"])
event = %{
"type" => "Event",
"attributedTo" => event["actor"],
"id" => event["id"],
"name" => event["title"],
"category" => event["category"],
"content" => html,
"source" => %{
"content" => event["summary"],
"mediaType" => "text/markdown"
},
"mediaType" => "text/html",
"published" => event["publish_at"],
"updated" => event["updated_at"]
}
Map.merge(event, Utils.make_json_ld_header())
end
def render("comment.json", %{comment: comment}) do
comment = %{
"actor" => comment["actor"],
"uuid" => comment["uuid"],
# The activity should have attributedTo, not the comment itself
# "attributedTo" => comment.attributed_to,
"type" => "Note",
"id" => comment["id"],
"content" => comment["content"],
"mediaType" => "text/html"
# "published" => Timex.format!(comment.inserted_at, "{ISO:Extended}"),
# "updated" => Timex.format!(comment.updated_at, "{ISO:Extended}")
}
Map.merge(comment, Utils.make_json_ld_header())
end
def render("activity.json", %{activity: %Activity{local: local, data: data} = activity}) do def render("activity.json", %{activity: %Activity{local: local, data: data} = activity}) do
%{ %{
"id" => data["id"], "id" => data["id"],

View file

@ -8,6 +8,10 @@ defmodule MobilizonWeb.ErrorView do
"Page not found" "Page not found"
end end
def render("404.json", _assigns) do
%{msg: "Resource not found"}
end
def render("invalid_request.json", _assigns) do def render("invalid_request.json", _assigns) do
%{errors: "Invalid request"} %{errors: "Invalid request"}
end end

View file

@ -3,4 +3,83 @@ defmodule MobilizonWeb.PageView do
View for our webapp View for our webapp
""" """
use MobilizonWeb, :view use MobilizonWeb, :view
alias Mobilizon.Actors.Actor
alias Mobilizon.Service.ActivityPub.Utils
def render("actor.activity-json", %{conn: %{assigns: %{object: actor}}}) do
public_key = Mobilizon.Service.ActivityPub.Utils.pem_to_public_key_pem(actor.keys)
%{
"id" => Actor.build_url(actor.preferred_username, :page),
"type" => "Person",
"following" => Actor.build_url(actor.preferred_username, :following),
"followers" => Actor.build_url(actor.preferred_username, :followers),
"inbox" => Actor.build_url(actor.preferred_username, :inbox),
"outbox" => Actor.build_url(actor.preferred_username, :outbox),
"preferredUsername" => actor.preferred_username,
"name" => actor.name,
"summary" => actor.summary,
"url" => actor.url,
"manuallyApprovesFollowers" => actor.manually_approves_followers,
"publicKey" => %{
"id" => "#{actor.url}#main-key",
"owner" => actor.url,
"publicKeyPem" => public_key
},
# TODO : Make have actors have an uuid
# "uuid" => actor.uuid
"endpoints" => %{
"sharedInbox" => actor.shared_inbox_url
}
# "icon" => %{
# "type" => "Image",
# "url" => User.avatar_url(actor)
# },
# "image" => %{
# "type" => "Image",
# "url" => User.banner_url(actor)
# }
}
|> Map.merge(Utils.make_json_ld_header())
end
def render("event.activity-json", %{conn: %{assigns: %{object: event}}}) do
event = Utils.make_event_data(event)
{:ok, html, []} = Earmark.as_html(event["summary"])
%{
"type" => "Event",
"attributedTo" => event["actor"],
"id" => event["id"],
"name" => event["title"],
"category" => event["category"],
"content" => html,
"source" => %{
"content" => event["summary"],
"mediaType" => "text/markdown"
},
"mediaType" => "text/html",
"published" => event["publish_at"],
"updated" => event["updated_at"]
}
|> Map.merge(Utils.make_json_ld_header())
end
def render("comment.activity-json", %{conn: %{assigns: %{object: comment}}}) do
comment = Utils.make_comment_data(comment)
%{
"actor" => comment["actor"],
"uuid" => comment["uuid"],
# The activity should have attributedTo, not the comment itself
# "attributedTo" => comment.attributed_to,
"type" => "Note",
"id" => comment["id"],
"content" => comment["content"],
"mediaType" => "text/html"
# "published" => Timex.format!(comment.inserted_at, "{ISO:Extended}"),
# "updated" => Timex.format!(comment.updated_at, "{ISO:Extended}")
}
|> Map.merge(Utils.make_json_ld_header())
end
end end

View file

@ -7,6 +7,7 @@ defmodule MobilizonWeb.ActivityPubControllerTest do
use MobilizonWeb.ConnCase use MobilizonWeb.ConnCase
import Mobilizon.Factory import Mobilizon.Factory
alias MobilizonWeb.ActivityPub.{ActorView, ObjectView} alias MobilizonWeb.ActivityPub.{ActorView, ObjectView}
alias MobilizonWeb.PageView
alias Mobilizon.Actors alias Mobilizon.Actors
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Service.ActivityPub alias Mobilizon.Service.ActivityPub
@ -43,7 +44,7 @@ defmodule MobilizonWeb.ActivityPubControllerTest do
|> get(Routes.page_url(Endpoint, :event, event.uuid)) |> get(Routes.page_url(Endpoint, :event, event.uuid))
assert json_response(conn, 200) == assert json_response(conn, 200) ==
ObjectView.render("event.json", %{event: event |> Utils.make_event_data()}) PageView.render("event.activity-json", %{conn: %{assigns: %{object: event}}})
end end
test "it returns 404 for non-public events", %{conn: conn} do test "it returns 404 for non-public events", %{conn: conn} do
@ -51,6 +52,7 @@ defmodule MobilizonWeb.ActivityPubControllerTest do
conn = conn =
conn conn
|> put_req_header("accept", "application/activity+json")
|> get(Routes.page_url(Endpoint, :event, event.uuid)) |> get(Routes.page_url(Endpoint, :event, event.uuid))
assert json_response(conn, 404) assert json_response(conn, 404)
@ -63,10 +65,11 @@ defmodule MobilizonWeb.ActivityPubControllerTest do
conn = conn =
conn conn
|> put_req_header("accept", "application/activity+json")
|> get(Routes.page_url(Endpoint, :comment, comment.uuid)) |> get(Routes.page_url(Endpoint, :comment, comment.uuid))
assert json_response(conn, 200) == assert json_response(conn, 200) ==
ObjectView.render("comment.json", %{comment: comment |> Utils.make_comment_data()}) PageView.render("comment.activity-json", %{conn: %{assigns: %{object: comment}}})
end end
test "it returns 404 for non-public comments", %{conn: conn} do test "it returns 404 for non-public comments", %{conn: conn} do