1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.Relay do
9 alias Pleroma.Web.ActivityPub.ActivityPub
15 |> User.get_or_create_service_actor_by_ap_id()
17 {:ok, actor} = User.set_invisible(actor, true)
22 "#{Pleroma.Web.Endpoint.url()}/relay"
25 @spec follow(String.t()) :: {:ok, Activity.t()} | {:error, any()}
26 def follow(target_instance) do
27 with %User{} = local_user <- get_actor(),
28 {:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_instance),
29 {:ok, activity} <- ActivityPub.follow(local_user, target_user) do
30 Logger.info("relay: followed instance: #{target_instance}; id=#{activity.data["id"]}")
33 error -> format_error(error)
37 @spec unfollow(String.t()) :: {:ok, Activity.t()} | {:error, any()}
38 def unfollow(target_instance) do
39 with %User{} = local_user <- get_actor(),
40 {:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_instance),
41 {:ok, activity} <- ActivityPub.unfollow(local_user, target_user) do
42 User.unfollow(local_user, target_user)
43 Logger.info("relay: unfollowed instance: #{target_instance}: id=#{activity.data["id"]}")
46 error -> format_error(error)
50 @spec publish(any()) :: {:ok, Activity.t(), Object.t()} | {:error, any()}
51 def publish(%Activity{data: %{"type" => "Create"}} = activity) do
52 with %User{} = user <- get_actor(),
53 %Object{} = object <- Object.normalize(activity) do
54 ActivityPub.announce(user, object, nil, true, false)
56 error -> format_error(error)
60 def publish(_), do: {:error, "Not implemented"}
62 @spec list() :: {:ok, [String.t()]} | {:error, any()}
64 with %User{} = user <- get_actor() do
68 |> Enum.map(fn entry -> URI.parse(entry).host end)
73 error -> format_error(error)
77 defp format_error({:error, error}), do: format_error(error)
79 defp format_error(error) do
80 Logger.error("error: #{inspect(error)}")