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
|
|
|
|
|
|
|
|
defmodule Mobilizon.Service.ActivityPub.Relay do
|
|
|
|
@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
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Service.ActivityPub
|
2019-09-22 18:29:13 +02:00
|
|
|
alias Mobilizon.Service.ActivityPub.{Activity, Transmogrifier}
|
2019-09-13 01:35:03 +02:00
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
alias MobilizonWeb.API.Follows
|
2019-09-13 01:35:03 +02:00
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
def get_actor do
|
|
|
|
with {:ok, %Actor{} = actor} <-
|
2019-09-11 03:16:37 +02:00
|
|
|
Actors.get_or_create_actor_by_url("#{MobilizonWeb.Endpoint.url()}/relay") do
|
2019-07-30 16:40:59 +02:00
|
|
|
actor
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def follow(target_instance) do
|
|
|
|
with %Actor{} = local_actor <- get_actor(),
|
2019-10-25 17:43:37 +02:00
|
|
|
{:ok, %Actor{} = target_actor} <- ActivityPub.get_or_fetch_actor_by_url(target_instance),
|
2019-07-30 16:40:59 +02:00
|
|
|
{:ok, activity} <- Follows.follow(local_actor, target_actor) do
|
|
|
|
Logger.info("Relay: followed instance #{target_instance}; id=#{activity.data["id"]}")
|
|
|
|
{:ok, activity}
|
|
|
|
else
|
|
|
|
e ->
|
|
|
|
Logger.warn("Error while following remote instance: #{inspect(e)}")
|
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def unfollow(target_instance) do
|
|
|
|
with %Actor{} = local_actor <- get_actor(),
|
2019-10-25 17:43:37 +02:00
|
|
|
{:ok, %Actor{} = target_actor} <- ActivityPub.get_or_fetch_actor_by_url(target_instance),
|
2019-07-30 16:40:59 +02:00
|
|
|
{:ok, activity} <- Follows.unfollow(local_actor, target_actor) do
|
|
|
|
Logger.info("Relay: unfollowed instance #{target_instance}: id=#{activity.data["id"]}")
|
|
|
|
{:ok, activity}
|
|
|
|
else
|
|
|
|
e ->
|
|
|
|
Logger.warn("Error while unfollowing remote instance: #{inspect(e)}")
|
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def accept(target_instance) do
|
|
|
|
with %Actor{} = local_actor <- get_actor(),
|
2019-10-25 17:43:37 +02:00
|
|
|
{:ok, %Actor{} = target_actor} <- ActivityPub.get_or_fetch_actor_by_url(target_instance),
|
2019-07-30 16:40:59 +02:00
|
|
|
{:ok, activity} <- Follows.accept(target_actor, local_actor) do
|
|
|
|
{:ok, activity}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# def reject(target_instance) do
|
|
|
|
# with %Actor{} = local_actor <- get_actor(),
|
2019-10-25 17:43:37 +02:00
|
|
|
# {:ok, %Actor{} = target_actor} <- Activity.get_or_fetch_actor_by_url(target_instance),
|
2019-07-30 16:40:59 +02:00
|
|
|
# {:ok, activity} <- Follows.reject(target_actor, local_actor) do
|
|
|
|
# {:ok, activity}
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
|
|
|
@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(),
|
|
|
|
{:ok, object} <-
|
2019-09-22 18:29:13 +02:00
|
|
|
Transmogrifier.fetch_obj_helper_as_activity_streams(object) do
|
2019-07-30 16:40:59 +02:00
|
|
|
ActivityPub.announce(actor, object, "#{object["id"]}/announces/#{actor_id}", true, false)
|
|
|
|
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
|
|
|
|
end
|