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
13 "#{Pleroma.Web.Endpoint.url()}/relay"
14 |> User.get_or_create_service_actor_by_ap_id()
17 @spec follow(String.t()) :: {:ok, Activity.t()} | {:error, any()}
18 def follow(target_instance) do
19 with %User{} = local_user <- get_actor(),
20 {:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_instance),
21 {:ok, activity} <- ActivityPub.follow(local_user, target_user) do
22 Logger.info("relay: followed instance: #{target_instance}; id=#{activity.data["id"]}")
25 error -> format_error(error)
29 @spec unfollow(String.t()) :: {:ok, Activity.t()} | {:error, any()}
30 def unfollow(target_instance) do
31 with %User{} = local_user <- get_actor(),
32 {:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_instance),
33 {:ok, activity} <- ActivityPub.unfollow(local_user, target_user) do
34 User.unfollow(local_user, target_user)
35 Logger.info("relay: unfollowed instance: #{target_instance}: id=#{activity.data["id"]}")
38 error -> format_error(error)
42 @spec publish(any()) :: {:ok, Activity.t(), Object.t()} | {:error, any()}
43 def publish(%Activity{data: %{"type" => "Create"}} = activity) do
44 with %User{} = user <- get_actor(),
45 %Object{} = object <- Object.normalize(activity) do
46 ActivityPub.announce(user, object, nil, true, false)
48 error -> format_error(error)
52 def publish(_), do: {:error, "Not implemented"}
54 @spec list() :: {:ok, [String.t()]} | {:error, any()}
56 with %User{following: following} = _user <- get_actor() do
59 |> Enum.map(fn entry -> URI.parse(entry).host end)
64 error -> format_error(error)
68 defp format_error({:error, error}), do: format_error(error)
70 defp format_error(error) do
71 Logger.error("error: #{inspect(error)}")