2018-10-11 17:37:39 +02:00
|
|
|
defmodule MobilizonWeb.ActivityPubController do
|
|
|
|
use MobilizonWeb, :controller
|
|
|
|
alias Mobilizon.{Actors, Actors.Actor, Events, Events.Event}
|
|
|
|
alias MobilizonWeb.ActivityPub.{ObjectView, ActorView}
|
|
|
|
alias Mobilizon.Service.ActivityPub
|
|
|
|
alias Mobilizon.Service.Federator
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
action_fallback(:errors)
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
def actor(conn, %{"name" => name}) do
|
|
|
|
with %Actor{} = actor <- Actors.get_local_actor_by_name(name) do
|
2018-10-10 14:57:35 +02:00
|
|
|
case get_req_header(conn, "accept") do
|
2018-10-11 17:47:02 +02:00
|
|
|
["application/activity+json"] ->
|
2018-10-10 14:57:35 +02:00
|
|
|
conn
|
|
|
|
|> put_resp_header("content-type", "application/activity+json")
|
|
|
|
|> json(ActorView.render("actor.json", %{actor: actor}))
|
2018-10-11 17:47:02 +02:00
|
|
|
|
2018-10-10 14:57:35 +02:00
|
|
|
_ ->
|
|
|
|
conn
|
|
|
|
|> put_resp_content_type("text/html")
|
|
|
|
|> send_file(200, "priv/static/index.html")
|
|
|
|
end
|
2018-10-09 17:01:45 +02:00
|
|
|
else
|
|
|
|
nil -> {:error, :not_found}
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-14 17:25:55 +02:00
|
|
|
def event(conn, %{"uuid" => uuid}) do
|
2018-08-24 11:34:00 +02:00
|
|
|
with %Event{} = event <- Events.get_event_full_by_uuid(uuid),
|
|
|
|
true <- event.public do
|
2018-05-17 11:32:23 +02:00
|
|
|
conn
|
|
|
|
|> put_resp_header("content-type", "application/activity+json")
|
|
|
|
|> json(ObjectView.render("event.json", %{event: event}))
|
2018-08-24 11:34:00 +02:00
|
|
|
else
|
|
|
|
false ->
|
2018-10-09 17:01:45 +02:00
|
|
|
{:error, :not_found}
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
def following(conn, %{"name" => name, "page" => page}) do
|
|
|
|
with %Actor{} = actor <- Actors.get_local_actor_by_name(name) do
|
|
|
|
{page, _} = Integer.parse(page)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_resp_header("content-type", "application/activity+json")
|
|
|
|
|> json(ActorView.render("following.json", %{actor: actor, page: page}))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def following(conn, %{"name" => name}) do
|
|
|
|
with %Actor{} = actor <- Actors.get_local_actor_by_name(name) do
|
|
|
|
conn
|
|
|
|
|> put_resp_header("content-type", "application/activity+json")
|
|
|
|
|> json(ActorView.render("following.json", %{actor: actor}))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def followers(conn, %{"name" => name, "page" => page}) do
|
|
|
|
with %Actor{} = actor <- Actors.get_local_actor_by_name(name) do
|
|
|
|
{page, _} = Integer.parse(page)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_resp_header("content-type", "application/activity+json")
|
|
|
|
|> json(ActorView.render("followers.json", %{actor: actor, page: page}))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def followers(conn, %{"name" => name}) do
|
|
|
|
with %Actor{} = actor <- Actors.get_local_actor_by_name(name) do
|
|
|
|
conn
|
|
|
|
|> put_resp_header("content-type", "application/activity+json")
|
|
|
|
|> json(ActorView.render("followers.json", %{actor: actor}))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def outbox(conn, %{"name" => name, "page" => page}) do
|
2018-05-17 11:32:23 +02:00
|
|
|
with {page, ""} = Integer.parse(page),
|
2018-05-18 09:56:21 +02:00
|
|
|
%Actor{} = actor <- Actors.get_local_actor_by_name(name) do
|
2018-05-17 11:32:23 +02:00
|
|
|
conn
|
|
|
|
|> put_resp_header("content-type", "application/activity+json")
|
2018-05-18 09:56:21 +02:00
|
|
|
|> json(ActorView.render("outbox.json", %{actor: actor, page: page}))
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
def outbox(conn, %{"name" => username}) do
|
|
|
|
outbox(conn, %{"name" => username, "page" => "0"})
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: Ensure that this inbox is a recipient of the message
|
|
|
|
def inbox(%{assigns: %{valid_signature: true}} = conn, params) do
|
|
|
|
Federator.enqueue(:incoming_ap_doc, params)
|
|
|
|
json(conn, "ok")
|
|
|
|
end
|
|
|
|
|
2018-10-09 17:01:45 +02:00
|
|
|
# only accept relayed Creates
|
|
|
|
def inbox(conn, %{"type" => "Create"} = params) do
|
|
|
|
Logger.info(
|
|
|
|
"Signature missing or not from author, relayed Create message, fetching object from source"
|
|
|
|
)
|
|
|
|
|
|
|
|
ActivityPub.fetch_object_from_url(params["object"]["id"])
|
|
|
|
|
|
|
|
json(conn, "ok")
|
|
|
|
end
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
def inbox(conn, params) do
|
|
|
|
headers = Enum.into(conn.req_headers, %{})
|
|
|
|
|
2018-10-09 17:01:45 +02:00
|
|
|
if String.contains?(headers["signature"], params["actor"]) do
|
|
|
|
Logger.info(
|
|
|
|
"Signature validation error for: #{params["actor"]}, make sure you are forwarding the HTTP Host header!"
|
|
|
|
)
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
Logger.info(inspect(conn.req_headers))
|
|
|
|
end
|
|
|
|
|
2018-10-09 17:01:45 +02:00
|
|
|
json(conn, "error")
|
|
|
|
end
|
|
|
|
|
|
|
|
def errors(conn, {:error, :not_found}) do
|
|
|
|
conn
|
|
|
|
|> put_status(404)
|
|
|
|
|> json("Not found")
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def errors(conn, _e) do
|
|
|
|
conn
|
|
|
|
|> put_status(500)
|
|
|
|
|> json("error")
|
|
|
|
end
|
|
|
|
end
|