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
|
|
|
|
|
2020-01-26 21:36:50 +01:00
|
|
|
defmodule Mobilizon.Web.ActivityPubController do
|
|
|
|
use Mobilizon.Web, :controller
|
2019-09-08 00:05:54 +02:00
|
|
|
|
2020-01-23 00:55:07 +01:00
|
|
|
alias Mobilizon.{Actors, Config}
|
2020-08-19 11:28:23 +02:00
|
|
|
alias Mobilizon.Actors.{Actor, Member}
|
2020-01-22 02:14:42 +01:00
|
|
|
|
|
|
|
alias Mobilizon.Federation.ActivityPub
|
2021-04-22 12:17:56 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
2021-04-21 18:57:23 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.{Federator, Utils}
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2020-01-26 21:36:50 +01:00
|
|
|
alias Mobilizon.Web.ActivityPub.ActorView
|
|
|
|
alias Mobilizon.Web.Cache
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Plug.Conn
|
2019-09-08 00:05:54 +02:00
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
action_fallback(:errors)
|
|
|
|
|
2020-01-26 21:36:50 +01:00
|
|
|
plug(Mobilizon.Web.Plugs.Federating when action in [:inbox, :relay])
|
2019-07-30 16:40:59 +02:00
|
|
|
plug(:relay_active? when action in [:relay])
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec relay_active?(Plug.Conn.t(), any()) :: Plug.Conn.t()
|
2019-07-30 16:40:59 +02:00
|
|
|
def relay_active?(conn, _) do
|
2019-09-08 00:05:54 +02:00
|
|
|
if Config.get([:instance, :allow_relay]) do
|
2019-07-30 16:40:59 +02:00
|
|
|
conn
|
|
|
|
else
|
|
|
|
conn
|
|
|
|
|> put_status(404)
|
|
|
|
|> json("Not found")
|
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec following(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
2020-07-09 17:24:28 +02:00
|
|
|
def following(conn, args) do
|
|
|
|
actor_collection(conn, "following", args)
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec followers(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
2020-07-09 17:24:28 +02:00
|
|
|
def followers(conn, args) do
|
|
|
|
actor_collection(conn, "followers", args)
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec members(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
2020-07-09 17:24:28 +02:00
|
|
|
def members(conn, args) do
|
|
|
|
actor_collection(conn, "members", args)
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec resources(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
2020-07-09 17:24:28 +02:00
|
|
|
def resources(conn, args) do
|
|
|
|
actor_collection(conn, "resources", args)
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec posts(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
2020-07-09 17:24:28 +02:00
|
|
|
def posts(conn, args) do
|
|
|
|
actor_collection(conn, "posts", args)
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec todos(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
2020-07-09 17:24:28 +02:00
|
|
|
def todos(conn, args) do
|
|
|
|
actor_collection(conn, "todos", args)
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec events(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
2020-07-09 17:24:28 +02:00
|
|
|
def events(conn, args) do
|
|
|
|
actor_collection(conn, "events", args)
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec discussions(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
2020-07-09 17:24:28 +02:00
|
|
|
def discussions(conn, args) do
|
|
|
|
actor_collection(conn, "discussions", args)
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
2020-08-19 11:28:23 +02:00
|
|
|
@ok_statuses [:ok, :commit]
|
|
|
|
@spec member(Plug.Conn.t(), map) :: {:error, :not_found} | Plug.Conn.t()
|
|
|
|
def member(conn, %{"uuid" => uuid}) do
|
2021-09-24 16:46:42 +02:00
|
|
|
case Cache.get_member_by_uuid_with_preload(uuid) do
|
|
|
|
{status, %Member{parent: %Actor{} = group, actor: %Actor{domain: nil} = _actor} = member}
|
|
|
|
when status in @ok_statuses ->
|
|
|
|
actor = Map.get(conn.assigns, :actor)
|
|
|
|
|
|
|
|
if actor_applicant_group_member?(group, actor) do
|
2022-11-22 11:32:31 +01:00
|
|
|
conn
|
|
|
|
|> put_resp_content_type("application/activity+json")
|
|
|
|
|> json(
|
2021-09-24 16:46:42 +02:00
|
|
|
ActorView.render("member.json", %{
|
|
|
|
member: member,
|
|
|
|
actor_applicant: actor
|
|
|
|
})
|
|
|
|
)
|
|
|
|
else
|
|
|
|
not_found(conn)
|
|
|
|
end
|
|
|
|
|
2020-08-19 11:28:23 +02:00
|
|
|
{status, %Member{actor: %Actor{url: domain}, parent: %Actor{} = group, url: url}}
|
|
|
|
when status in @ok_statuses and not is_nil(domain) ->
|
2021-09-24 16:46:42 +02:00
|
|
|
actor = Map.get(conn.assigns, :actor)
|
|
|
|
|
|
|
|
if actor_applicant_group_member?(group, actor) do
|
2020-08-19 11:28:23 +02:00
|
|
|
redirect(conn, external: url)
|
|
|
|
else
|
2021-09-24 16:46:42 +02:00
|
|
|
not_found(conn)
|
2020-08-19 11:28:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
_ ->
|
2021-09-24 16:46:42 +02:00
|
|
|
not_found(conn)
|
2020-08-19 11:28:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec not_found(Plug.Conn.t()) :: Plug.Conn.t()
|
|
|
|
defp not_found(conn) do
|
|
|
|
conn
|
|
|
|
|> put_status(404)
|
|
|
|
|> json("Not found")
|
|
|
|
end
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
def outbox(conn, args) do
|
|
|
|
actor_collection(conn, "outbox", args)
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
2021-04-21 18:57:23 +02:00
|
|
|
def inbox(%{assigns: %{valid_signature: true}} = conn, %{"name" => preferred_username} = params) do
|
|
|
|
with %Actor{url: recipient_url} = recipient <-
|
|
|
|
Actors.get_local_actor_by_name(preferred_username),
|
2021-04-22 12:17:56 +02:00
|
|
|
{:ok, %Actor{} = actor} <- ActivityPubActor.get_or_fetch_actor_by_url(params["actor"]),
|
2021-04-21 18:57:23 +02:00
|
|
|
true <- Utils.recipient_in_message(recipient, actor, params),
|
|
|
|
params <- Utils.maybe_splice_recipient(recipient_url, params) do
|
|
|
|
Federator.enqueue(:incoming_ap_doc, params)
|
|
|
|
json(conn, "ok")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
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
|
2021-04-21 18:57:23 +02:00
|
|
|
Logger.debug(
|
2018-10-09 17:01:45 +02:00
|
|
|
"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, %{})
|
|
|
|
|
2021-04-21 18:57:23 +02:00
|
|
|
if headers["signature"] && params["actor"] &&
|
|
|
|
String.contains?(headers["signature"], params["actor"]) do
|
|
|
|
Logger.debug(
|
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
|
2019-12-03 11:29:51 +01:00
|
|
|
with {status, %Actor{} = actor} when status in [:commit, :ok] <- Cache.get_relay() do
|
2019-07-30 16:40:59 +02:00
|
|
|
conn
|
2022-11-22 11:32:31 +01:00
|
|
|
|> put_resp_content_type("application/activity+json")
|
2019-07-30 16:40:59 +02:00
|
|
|
|> 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
|
2020-07-09 17:24:28 +02:00
|
|
|
|
|
|
|
@spec actor_collection(Conn.t(), String.t(), map()) :: Conn.t()
|
|
|
|
|
|
|
|
defp actor_collection(conn, collection, %{"name" => name, "page" => page}) do
|
|
|
|
with {page, ""} <- Integer.parse(page),
|
2022-11-21 16:56:20 +01:00
|
|
|
page <- max(page, 1),
|
2020-07-09 17:24:28 +02:00
|
|
|
%Actor{} = actor <- Actors.get_local_actor_by_name_with_preload(name) do
|
|
|
|
conn
|
2022-11-22 11:32:31 +01:00
|
|
|
|> put_resp_content_type("application/activity+json")
|
2020-07-09 17:24:28 +02:00
|
|
|
|> json(
|
|
|
|
ActorView.render("#{collection}.json", %{
|
|
|
|
actor: actor,
|
|
|
|
page: page,
|
|
|
|
actor_applicant: Map.get(conn.assigns, :actor)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp actor_collection(conn, collection, %{"name" => name}) do
|
|
|
|
with %Actor{} = actor <- Actors.get_local_actor_by_name_with_preload(name) do
|
|
|
|
conn
|
2022-11-22 11:32:31 +01:00
|
|
|
|> put_resp_content_type("application/activity+json")
|
2020-07-09 17:24:28 +02:00
|
|
|
|> json(
|
|
|
|
ActorView.render("#{collection}.json", %{
|
|
|
|
actor: actor,
|
|
|
|
actor_applicant: Map.get(conn.assigns, :actor)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2020-08-19 11:28:23 +02:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec actor_applicant_group_member?(Actor.t(), Actor.t() | nil) :: boolean()
|
2020-08-19 11:28:23 +02:00
|
|
|
defp actor_applicant_group_member?(%Actor{}, nil), do: false
|
|
|
|
|
|
|
|
defp actor_applicant_group_member?(%Actor{id: group_id}, %Actor{id: actor_applicant_id}),
|
|
|
|
do:
|
|
|
|
Actors.get_member(actor_applicant_id, group_id, [
|
|
|
|
:member,
|
|
|
|
:moderator,
|
|
|
|
:administrator,
|
|
|
|
:creator
|
|
|
|
]) != {:error, :member_not_found}
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|