2019-07-30 16:40:59 +02:00
|
|
|
# Portions of this file are derived from Pleroma:
|
|
|
|
# Copyright © 2017-2019 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/relay.ex
|
|
|
|
|
2020-01-22 02:14:42 +01:00
|
|
|
defmodule Mobilizon.Federation.ActivityPub.Relay do
|
2019-07-30 16:40:59 +02:00
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
Handles following and unfollowing relays and instances.
|
2019-07-30 16:40:59 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Actors
|
2019-12-03 11:29:51 +01:00
|
|
|
alias Mobilizon.Actors.{Actor, Follower}
|
2020-01-22 02:14:42 +01:00
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.{Actions, Activity, Transmogrifier}
|
2021-04-22 12:17:56 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
2020-01-22 22:40:40 +01:00
|
|
|
alias Mobilizon.Federation.WebFinger
|
2020-01-26 21:11:16 +01:00
|
|
|
alias Mobilizon.GraphQL.API.Follows
|
2022-04-20 18:35:18 +02:00
|
|
|
alias Mobilizon.Service.Workers.Background
|
|
|
|
import Mobilizon.Federation.ActivityPub.Utils, only: [create_full_domain_string: 1]
|
2019-09-13 01:35:03 +02:00
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
require Logger
|
|
|
|
|
2020-01-28 20:15:59 +01:00
|
|
|
def init do
|
2019-12-03 11:29:51 +01:00
|
|
|
# Wait for everything to settle.
|
|
|
|
Process.sleep(1000 * 5)
|
|
|
|
get_actor()
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec get_actor() :: Actor.t() | no_return
|
2019-07-30 16:40:59 +02:00
|
|
|
def get_actor do
|
2021-09-24 16:46:42 +02:00
|
|
|
case Actors.get_or_create_internal_actor("relay") do
|
|
|
|
{:ok, %Actor{} = actor} ->
|
|
|
|
actor
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = _err} ->
|
|
|
|
raise("Relay actor not found")
|
2019-07-30 16:40:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec follow(String.t()) ::
|
|
|
|
{:ok, Activity.t(), Follower.t()} | {:error, atom()} | {:error, String.t()}
|
2019-12-03 11:29:51 +01:00
|
|
|
def follow(address) do
|
2021-09-24 16:46:42 +02:00
|
|
|
%Actor{} = local_actor = get_actor()
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
with {:ok, target_instance} <- fetch_actor(address),
|
2021-04-22 12:17:56 +02:00
|
|
|
{:ok, %Actor{} = target_actor} <-
|
|
|
|
ActivityPubActor.get_or_fetch_actor_by_url(target_instance),
|
2019-12-03 11:29:51 +01:00
|
|
|
{:ok, activity, follow} <- Follows.follow(local_actor, target_actor) do
|
2019-07-30 16:40:59 +02:00
|
|
|
Logger.info("Relay: followed instance #{target_instance}; id=#{activity.data["id"]}")
|
2019-12-03 11:29:51 +01:00
|
|
|
{:ok, activity, follow}
|
2019-07-30 16:40:59 +02:00
|
|
|
else
|
2021-09-24 16:46:42 +02:00
|
|
|
{:error, :person_no_follow} ->
|
|
|
|
Logger.warn("Only group and instances can be followed")
|
|
|
|
{:error, :person_no_follow}
|
2020-09-02 08:59:59 +02:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
{:error, e} ->
|
2019-07-30 16:40:59 +02:00
|
|
|
Logger.warn("Error while following remote instance: #{inspect(e)}")
|
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec unfollow(String.t()) ::
|
|
|
|
{:ok, Activity.t(), Follower.t()} | {:error, atom()} | {:error, String.t()}
|
2019-12-03 11:29:51 +01:00
|
|
|
def unfollow(address) do
|
2021-09-24 16:46:42 +02:00
|
|
|
%Actor{} = local_actor = get_actor()
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
with {:ok, target_instance} <- fetch_actor(address),
|
2021-04-22 12:17:56 +02:00
|
|
|
{:ok, %Actor{} = target_actor} <-
|
|
|
|
ActivityPubActor.get_or_fetch_actor_by_url(target_instance),
|
2019-12-03 11:29:51 +01:00
|
|
|
{:ok, activity, follow} <- Follows.unfollow(local_actor, target_actor) do
|
2019-07-30 16:40:59 +02:00
|
|
|
Logger.info("Relay: unfollowed instance #{target_instance}: id=#{activity.data["id"]}")
|
2019-12-03 11:29:51 +01:00
|
|
|
{:ok, activity, follow}
|
2019-07-30 16:40:59 +02:00
|
|
|
else
|
2021-09-24 16:46:42 +02:00
|
|
|
{:error, e} ->
|
2019-07-30 16:40:59 +02:00
|
|
|
Logger.warn("Error while unfollowing remote instance: #{inspect(e)}")
|
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec accept(String.t()) ::
|
|
|
|
{:ok, Activity.t(), Follower.t()} | {:error, atom()} | {:error, String.t()}
|
2019-12-03 11:29:51 +01:00
|
|
|
def accept(address) do
|
|
|
|
Logger.debug("We're trying to accept a relay subscription")
|
2021-09-24 16:46:42 +02:00
|
|
|
%Actor{} = local_actor = get_actor()
|
2019-12-03 11:29:51 +01:00
|
|
|
|
|
|
|
with {:ok, target_instance} <- fetch_actor(address),
|
2021-04-22 12:17:56 +02:00
|
|
|
{:ok, %Actor{} = target_actor} <-
|
|
|
|
ActivityPubActor.get_or_fetch_actor_by_url(target_instance),
|
2019-12-03 11:29:51 +01:00
|
|
|
{:ok, activity, follow} <- Follows.accept(target_actor, local_actor) do
|
|
|
|
{:ok, activity, follow}
|
2021-09-24 16:46:42 +02:00
|
|
|
else
|
|
|
|
{:error, e} ->
|
|
|
|
Logger.warn("Error while accepting remote instance follow: #{inspect(e)}")
|
|
|
|
{:error, e}
|
2019-07-30 16:40:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec reject(String.t()) ::
|
|
|
|
{:ok, Activity.t(), Follower.t()} | {:error, atom()} | {:error, String.t()}
|
2019-12-03 11:29:51 +01:00
|
|
|
def reject(address) do
|
|
|
|
Logger.debug("We're trying to reject a relay subscription")
|
2021-09-24 16:46:42 +02:00
|
|
|
%Actor{} = local_actor = get_actor()
|
2019-12-03 11:29:51 +01:00
|
|
|
|
|
|
|
with {:ok, target_instance} <- fetch_actor(address),
|
2021-04-22 12:17:56 +02:00
|
|
|
{:ok, %Actor{} = target_actor} <-
|
|
|
|
ActivityPubActor.get_or_fetch_actor_by_url(target_instance),
|
2019-12-03 11:29:51 +01:00
|
|
|
{:ok, activity, follow} <- Follows.reject(target_actor, local_actor) do
|
|
|
|
{:ok, activity, follow}
|
2021-09-24 16:46:42 +02:00
|
|
|
else
|
|
|
|
{:error, e} ->
|
|
|
|
Logger.warn("Error while rejecting remote instance follow: #{inspect(e)}")
|
|
|
|
{:error, e}
|
2019-12-03 11:29:51 +01:00
|
|
|
end
|
|
|
|
end
|
2019-07-30 16:40:59 +02:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec refresh(String.t()) ::
|
|
|
|
{:ok, Oban.Job.t()}
|
|
|
|
| {:error, Ecto.Changeset.t()}
|
|
|
|
| {:error, :bad_url}
|
2021-09-28 19:40:37 +02:00
|
|
|
| {:error, ActivityPubActor.make_actor_errors()}
|
2021-09-24 16:46:42 +02:00
|
|
|
| {:error, :no_internal_relay_actor}
|
|
|
|
| {:error, :url_nil}
|
2020-10-21 11:02:28 +02:00
|
|
|
def refresh(address) do
|
|
|
|
Logger.debug("We're trying to refresh a remote instance")
|
|
|
|
|
|
|
|
with {:ok, target_instance} <- fetch_actor(address),
|
2021-05-12 18:18:20 +02:00
|
|
|
{:ok, %Actor{id: target_actor_id}} <-
|
2021-04-22 12:17:56 +02:00
|
|
|
ActivityPubActor.get_or_fetch_actor_by_url(target_instance) do
|
2021-05-12 18:18:20 +02:00
|
|
|
Background.enqueue("refresh_profile", %{
|
|
|
|
"actor_id" => target_actor_id
|
|
|
|
})
|
2021-09-24 16:46:42 +02:00
|
|
|
else
|
|
|
|
{:error, e} ->
|
|
|
|
Logger.warn("Error while refreshing remote instance: #{inspect(e)}")
|
|
|
|
{:error, e}
|
2020-10-21 11:02:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
@doc """
|
|
|
|
Publish an activity to all relays following this instance
|
|
|
|
"""
|
|
|
|
def publish(%Activity{data: %{"object" => object}} = _activity) do
|
|
|
|
with %Actor{id: actor_id} = actor <- get_actor(),
|
2019-12-03 11:29:51 +01:00
|
|
|
{object, object_id} <- fetch_object(object),
|
|
|
|
id <- "#{object_id}/announces/#{actor_id}" do
|
|
|
|
Logger.info("Publishing activity #{id} to all relays")
|
2021-09-28 19:40:37 +02:00
|
|
|
Actions.Announce.announce(actor, object, id, true, false)
|
2019-07-30 16:40:59 +02:00
|
|
|
else
|
|
|
|
e ->
|
|
|
|
Logger.error("Error while getting local instance actor: #{inspect(e)}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def publish(err) do
|
|
|
|
Logger.error("Tried to publish a bad activity")
|
|
|
|
Logger.debug(inspect(err))
|
|
|
|
nil
|
|
|
|
end
|
2019-12-03 11:29:51 +01:00
|
|
|
|
|
|
|
defp fetch_object(object) when is_map(object) do
|
|
|
|
with {:ok, object} <- Transmogrifier.fetch_obj_helper_as_activity_streams(object) do
|
|
|
|
{object, object["id"]}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-08 16:41:49 +02:00
|
|
|
defp fetch_object(object) when is_binary(object), do: {object, object}
|
2019-12-03 11:29:51 +01:00
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec fetch_actor(String.t()) ::
|
|
|
|
{:ok, String.t()} | {:error, WebFinger.finger_errors() | :bad_url}
|
2019-12-03 11:29:51 +01:00
|
|
|
# Dirty hack
|
|
|
|
defp fetch_actor("https://" <> address), do: fetch_actor(address)
|
|
|
|
defp fetch_actor("http://" <> address), do: fetch_actor(address)
|
|
|
|
|
|
|
|
defp fetch_actor(address) do
|
2022-04-20 18:35:18 +02:00
|
|
|
%URI{host: host} = uri = URI.parse("http://" <> address)
|
2019-12-03 11:29:51 +01:00
|
|
|
|
|
|
|
cond do
|
|
|
|
String.contains?(address, "@") ->
|
|
|
|
check_actor(address)
|
|
|
|
|
|
|
|
!is_nil(host) ->
|
2022-04-20 18:35:18 +02:00
|
|
|
uri |> create_full_domain_string() |> check_actor()
|
2019-12-03 11:29:51 +01:00
|
|
|
|
|
|
|
true ->
|
2021-09-10 11:36:05 +02:00
|
|
|
{:error, :bad_url}
|
2019-12-03 11:29:51 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec check_actor(String.t()) :: {:ok, String.t()} | {:error, WebFinger.finger_errors()}
|
2019-12-03 11:29:51 +01:00
|
|
|
defp check_actor(username_and_domain) do
|
|
|
|
case Actors.get_actor_by_name(username_and_domain) do
|
|
|
|
%Actor{url: url} -> {:ok, url}
|
2021-09-10 11:36:05 +02:00
|
|
|
nil -> WebFinger.finger(username_and_domain)
|
2019-12-03 11:29:51 +01:00
|
|
|
end
|
|
|
|
end
|
2019-07-30 16:40:59 +02:00
|
|
|
end
|