2018-12-27 11:24:04 +01:00
|
|
|
# Portions of this file are derived from Pleroma:
|
|
|
|
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/activity_pub/activity_pub_controller.ex
|
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
defmodule MobilizonWeb.ActivityPubController do
|
|
|
|
use MobilizonWeb, :controller
|
2018-11-12 09:05:31 +01:00
|
|
|
alias Mobilizon.{Actors, Actors.Actor, Events}
|
|
|
|
alias Mobilizon.Events.{Event, Comment}
|
2018-10-11 17:37:39 +02:00
|
|
|
alias MobilizonWeb.ActivityPub.{ObjectView, ActorView}
|
|
|
|
alias Mobilizon.Service.ActivityPub
|
2018-11-12 18:17:53 +01:00
|
|
|
alias Mobilizon.Service.ActivityPub.Utils
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Service.Federator
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
action_fallback(:errors)
|
|
|
|
|
2019-03-01 18:30:46 +01:00
|
|
|
@doc """
|
2019-03-04 17:20:18 +01:00
|
|
|
Renders an Actor ActivityPub's representation
|
2019-03-01 18:30:46 +01:00
|
|
|
"""
|
2019-03-04 17:20:18 +01:00
|
|
|
@spec actor(Plug.Conn.t(), String.t()) :: Plug.Conn.t()
|
2018-05-18 09:56:21 +02:00
|
|
|
def actor(conn, %{"name" => name}) do
|
2019-03-04 17:20:18 +01:00
|
|
|
with {status, %Actor{} = actor} when status in [:ok, :commit] <-
|
|
|
|
Actors.get_cached_local_actor_by_name(name) do
|
2019-03-01 18:30:46 +01:00
|
|
|
conn
|
2019-03-04 17:20:18 +01:00
|
|
|
|> put_resp_header("content-type", "application/activity+json")
|
|
|
|
|> json(ActorView.render("actor.json", %{actor: actor}))
|
|
|
|
else
|
2019-03-01 18:30:46 +01:00
|
|
|
{:ignore, _} ->
|
|
|
|
{:error, :not_found}
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-01 18:30:46 +01:00
|
|
|
@doc """
|
|
|
|
Renders an Event ActivityPub's representation
|
|
|
|
"""
|
|
|
|
@spec event(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
|
|
|
def event(conn, %{"uuid" => uuid}) do
|
2019-03-05 10:13:19 +01:00
|
|
|
with {status, %Event{} = event} when status in [:ok, :commit] <-
|
2019-03-04 17:20:18 +01:00
|
|
|
Events.get_cached_event_full_by_uuid(uuid),
|
2019-01-14 16:21:13 +01:00
|
|
|
true <- event.visibility in [:public, :unlisted] do
|
2019-03-04 17:20:18 +01:00
|
|
|
conn
|
|
|
|
|> put_resp_header("content-type", "application/activity+json")
|
2019-04-26 09:56:25 +02:00
|
|
|
|> json(
|
|
|
|
ObjectView.render(
|
|
|
|
"event.json",
|
|
|
|
%{
|
|
|
|
event:
|
|
|
|
event
|
|
|
|
|> Utils.make_event_data()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2018-08-24 11:34:00 +02:00
|
|
|
else
|
2019-03-04 17:20:18 +01:00
|
|
|
{:ignore, _} ->
|
|
|
|
{:error, :not_found}
|
2018-11-12 09:05:31 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-01 18:30:46 +01:00
|
|
|
@doc """
|
|
|
|
Renders a Comment ActivityPub's representation
|
|
|
|
"""
|
|
|
|
@spec comment(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
2018-11-12 09:05:31 +01:00
|
|
|
def comment(conn, %{"uuid" => uuid}) do
|
2019-03-04 17:20:18 +01:00
|
|
|
with {status, %Comment{} = comment} when status in [:ok, :commit] <-
|
2019-04-26 09:56:25 +02:00
|
|
|
Events.get_cached_comment_full_by_uuid(uuid),
|
|
|
|
true <- comment.visibility in [:public, :unlisted] do
|
2019-03-04 17:20:18 +01:00
|
|
|
conn
|
|
|
|
|> put_resp_header("content-type", "application/activity+json")
|
2019-04-26 09:56:25 +02:00
|
|
|
|> json(
|
|
|
|
ObjectView.render(
|
|
|
|
"comment.json",
|
|
|
|
%{
|
|
|
|
comment:
|
|
|
|
comment
|
|
|
|
|> Utils.make_comment_data()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2018-11-12 09:05:31 +01:00
|
|
|
else
|
2019-04-26 09:56:25 +02:00
|
|
|
_ ->
|
2019-03-04 17:20:18 +01: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
|
2018-11-27 17:54:54 +01:00
|
|
|
with {page, ""} = Integer.parse(page),
|
|
|
|
%Actor{} = actor <- Actors.get_local_actor_by_name_with_everything(name) do
|
2018-05-18 09:56:21 +02:00
|
|
|
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
|
2018-11-27 17:54:54 +01:00
|
|
|
with %Actor{} = actor <- Actors.get_local_actor_by_name_with_everything(name) do
|
2018-05-18 09:56:21 +02:00
|
|
|
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
|
2018-11-27 17:54:54 +01:00
|
|
|
with {page, ""} = Integer.parse(page),
|
|
|
|
%Actor{} = actor <- Actors.get_local_actor_by_name_with_everything(name) do
|
2018-05-18 09:56:21 +02:00
|
|
|
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
|
2018-11-27 17:54:54 +01:00
|
|
|
with %Actor{} = actor <- Actors.get_local_actor_by_name_with_everything(name) do
|
2018-05-18 09:56:21 +02:00
|
|
|
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
|
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
def outbox(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("outbox.json", %{actor: actor}))
|
|
|
|
end
|
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
|
2018-11-12 18:17:53 +01:00
|
|
|
Logger.error(
|
2018-10-09 17:01:45 +02:00
|
|
|
"Signature validation error for: #{params["actor"]}, make sure you are forwarding the HTTP Host header!"
|
|
|
|
)
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
Logger.error(inspect(conn.req_headers))
|
2018-05-17 11:32:23 +02:00
|
|
|
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)
|
2018-11-27 17:54:54 +01:00
|
|
|
|> json("Unknown Error")
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
end
|