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
|
2019-05-22 14:12:11 +02:00
|
|
|
alias Mobilizon.{Actors, Actors.Actor}
|
|
|
|
alias MobilizonWeb.ActivityPub.ActorView
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Service.ActivityPub
|
|
|
|
alias Mobilizon.Service.Federator
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
action_fallback(:errors)
|
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
plug(:relay_active? when action in [:relay])
|
|
|
|
|
|
|
|
def relay_active?(conn, _) do
|
|
|
|
if Mobilizon.CommonConfig.get([:instance, :allow_relay]) do
|
|
|
|
conn
|
|
|
|
else
|
|
|
|
conn
|
|
|
|
|> put_status(404)
|
|
|
|
|> json("Not found")
|
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
def following(conn, %{"name" => name, "page" => page}) do
|
2019-07-23 18:06:22 +02:00
|
|
|
with {page, ""} <- Integer.parse(page),
|
2018-11-27 17:54:54 +01:00
|
|
|
%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
|
2019-07-23 18:06:22 +02:00
|
|
|
with {page, ""} <- Integer.parse(page),
|
2018-11-27 17:54:54 +01:00
|
|
|
%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
|
2019-07-23 18:06:22 +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
|
2019-07-30 16:40:59 +02:00
|
|
|
Logger.debug("Got something with valid signature inside inbox")
|
2018-05-17 11:32:23 +02:00
|
|
|
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!"
|
|
|
|
)
|
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
Logger.debug(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
|
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
def relay(conn, _params) do
|
|
|
|
with {status, actor} <-
|
|
|
|
Cachex.fetch(
|
|
|
|
:activity_pub,
|
|
|
|
"relay_actor",
|
|
|
|
&Mobilizon.Service.ActivityPub.Relay.get_actor/0
|
|
|
|
),
|
|
|
|
true <- status in [:ok, :commit] do
|
|
|
|
conn
|
|
|
|
|> put_resp_header("content-type", "application/activity+json")
|
|
|
|
|> json(ActorView.render("actor.json", %{actor: actor}))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-09 17:01:45 +02:00
|
|
|
def errors(conn, {:error, :not_found}) do
|
|
|
|
conn
|
|
|
|
|> put_status(404)
|
|
|
|
|> json("Not found")
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
def errors(conn, e) do
|
|
|
|
Logger.debug(inspect(e))
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
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
|