2018-12-27 10:24:04 +00: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
|
|
|
|
|
2020-01-26 20:36:50 +00:00
|
|
|
defmodule Mobilizon.Web.ActivityPubController do
|
|
|
|
use Mobilizon.Web, :controller
|
2019-09-07 22:05:54 +00:00
|
|
|
|
2020-01-22 23:55:07 +00:00
|
|
|
alias Mobilizon.{Actors, Config}
|
|
|
|
alias Mobilizon.Actors.Actor
|
2020-01-22 01:14:42 +00:00
|
|
|
|
|
|
|
alias Mobilizon.Federation.ActivityPub
|
|
|
|
alias Mobilizon.Federation.ActivityPub.Federator
|
2018-05-17 09:32:23 +00:00
|
|
|
|
2020-01-26 20:36:50 +00:00
|
|
|
alias Mobilizon.Web.ActivityPub.ActorView
|
|
|
|
alias Mobilizon.Web.Cache
|
2019-09-07 22:05:54 +00:00
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
action_fallback(:errors)
|
|
|
|
|
2020-01-26 20:36:50 +00:00
|
|
|
plug(Mobilizon.Web.Plugs.Federating when action in [:inbox, :relay])
|
2019-07-30 14:40:59 +00:00
|
|
|
plug(:relay_active? when action in [:relay])
|
|
|
|
|
|
|
|
def relay_active?(conn, _) do
|
2019-09-07 22:05:54 +00:00
|
|
|
if Config.get([:instance, :allow_relay]) do
|
2019-07-30 14:40:59 +00:00
|
|
|
conn
|
|
|
|
else
|
|
|
|
conn
|
|
|
|
|> put_status(404)
|
|
|
|
|> json("Not found")
|
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-18 07:56:21 +00:00
|
|
|
def following(conn, %{"name" => name, "page" => page}) do
|
2019-07-23 16:06:22 +00:00
|
|
|
with {page, ""} <- Integer.parse(page),
|
2019-09-08 22:52:49 +00:00
|
|
|
%Actor{} = actor <- Actors.get_local_actor_by_name_with_preload(name) do
|
2018-05-18 07:56:21 +00: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
|
2019-09-08 22:52:49 +00:00
|
|
|
with %Actor{} = actor <- Actors.get_local_actor_by_name_with_preload(name) do
|
2018-05-18 07:56:21 +00: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 16:06:22 +00:00
|
|
|
with {page, ""} <- Integer.parse(page),
|
2019-09-08 22:52:49 +00:00
|
|
|
%Actor{} = actor <- Actors.get_local_actor_by_name_with_preload(name) do
|
2018-05-18 07:56:21 +00: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
|
2019-09-08 22:52:49 +00:00
|
|
|
with %Actor{} = actor <- Actors.get_local_actor_by_name_with_preload(name) do
|
2018-05-18 07:56:21 +00:00
|
|
|
conn
|
|
|
|
|> put_resp_header("content-type", "application/activity+json")
|
|
|
|
|> json(ActorView.render("followers.json", %{actor: actor}))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
def members(conn, %{"name" => name, "page" => page}) do
|
|
|
|
with {page, ""} <- Integer.parse(page),
|
|
|
|
%Actor{} = group <- Actors.get_local_actor_by_name_with_preload(name) do
|
|
|
|
conn
|
|
|
|
|> put_resp_header("content-type", "application/activity+json")
|
|
|
|
|> json(
|
|
|
|
ActorView.render("members.json", %{
|
|
|
|
group: group,
|
|
|
|
page: page,
|
|
|
|
actor_applicant: Map.get(conn.assigns, :actor)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def members(conn, %{"name" => name}) do
|
|
|
|
with %Actor{} = group <- Actors.get_local_actor_by_name_with_preload(name) do
|
|
|
|
conn
|
|
|
|
|> put_resp_header("content-type", "application/activity+json")
|
|
|
|
|> json(
|
|
|
|
ActorView.render("members.json", %{
|
|
|
|
group: group,
|
|
|
|
actor_applicant: Map.get(conn.assigns, :actor)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def resources(conn, %{"name" => name}) do
|
|
|
|
with %Actor{} = group <- Actors.get_local_actor_by_name_with_preload(name) do
|
|
|
|
conn
|
|
|
|
|> put_resp_header("content-type", "application/activity+json")
|
|
|
|
|> json(
|
|
|
|
ActorView.render("resources.json", %{
|
|
|
|
group: group,
|
|
|
|
actor_applicant: Map.get(conn.assigns, :actor)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-18 07:56:21 +00:00
|
|
|
def outbox(conn, %{"name" => name, "page" => page}) do
|
2019-07-23 16:06:22 +00:00
|
|
|
with {page, ""} <- Integer.parse(page),
|
2018-05-18 07:56:21 +00:00
|
|
|
%Actor{} = actor <- Actors.get_local_actor_by_name(name) do
|
2018-05-17 09:32:23 +00:00
|
|
|
conn
|
|
|
|
|> put_resp_header("content-type", "application/activity+json")
|
2018-05-18 07:56:21 +00:00
|
|
|
|> json(ActorView.render("outbox.json", %{actor: actor, page: page}))
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-25 17:05:05 +00: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 09:32:23 +00: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 14:40:59 +00:00
|
|
|
Logger.debug("Got something with valid signature inside inbox")
|
2018-05-17 09:32:23 +00:00
|
|
|
Federator.enqueue(:incoming_ap_doc, params)
|
|
|
|
json(conn, "ok")
|
|
|
|
end
|
|
|
|
|
2018-10-09 15:01:45 +00: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 09:32:23 +00:00
|
|
|
def inbox(conn, params) do
|
|
|
|
headers = Enum.into(conn.req_headers, %{})
|
|
|
|
|
2018-10-09 15:01:45 +00:00
|
|
|
if String.contains?(headers["signature"], params["actor"]) do
|
2018-11-12 17:17:53 +00:00
|
|
|
Logger.error(
|
2018-10-09 15:01:45 +00:00
|
|
|
"Signature validation error for: #{params["actor"]}, make sure you are forwarding the HTTP Host header!"
|
|
|
|
)
|
|
|
|
|
2019-07-30 14:40:59 +00:00
|
|
|
Logger.debug(inspect(conn.req_headers))
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
|
2018-10-09 15:01:45 +00:00
|
|
|
json(conn, "error")
|
|
|
|
end
|
|
|
|
|
2019-07-30 14:40:59 +00:00
|
|
|
def relay(conn, _params) do
|
2019-12-03 10:29:51 +00:00
|
|
|
with {status, %Actor{} = actor} when status in [:commit, :ok] <- Cache.get_relay() do
|
2019-07-30 14:40:59 +00:00
|
|
|
conn
|
|
|
|
|> put_resp_header("content-type", "application/activity+json")
|
|
|
|
|> json(ActorView.render("actor.json", %{actor: actor}))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-09 15:01:45 +00:00
|
|
|
def errors(conn, {:error, :not_found}) do
|
|
|
|
conn
|
|
|
|
|> put_status(404)
|
|
|
|
|> json("Not found")
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
|
2019-07-30 14:40:59 +00:00
|
|
|
def errors(conn, e) do
|
|
|
|
Logger.debug(inspect(e))
|
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
conn
|
|
|
|
|> put_status(500)
|
2018-11-27 16:54:54 +00:00
|
|
|
|> json("Unknown Error")
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
end
|