1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.Relay do
8 alias Pleroma.Web.ActivityPub.ActivityPub
9 alias Pleroma.Web.ActivityPub.Visibility
10 alias Pleroma.Web.CommonAPI
15 @spec ap_id() :: String.t()
16 def ap_id, do: "#{Pleroma.Web.Endpoint.url()}/#{@nickname}"
18 @spec get_actor() :: User.t() | nil
19 def get_actor, do: User.get_or_create_service_actor_by_ap_id(ap_id(), @nickname)
21 @spec follow(String.t()) :: {:ok, Activity.t()} | {:error, any()}
22 def follow(target_instance) do
23 with %User{} = local_user <- get_actor(),
24 {:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_instance),
25 {:ok, _, _, activity} <- CommonAPI.follow(local_user, target_user) do
26 Logger.info("relay: followed instance: #{target_instance}; id=#{activity.data["id"]}")
29 error -> format_error(error)
33 @spec unfollow(String.t(), map()) :: {:ok, Activity.t()} | {:error, any()}
34 def unfollow(target_instance, opts \\ %{}) do
35 with %User{} = local_user <- get_actor(),
36 {:ok, target_user} <- fetch_target_user(target_instance, opts),
37 {:ok, activity} <- ActivityPub.unfollow(local_user, target_user) do
38 case target_user.id do
39 nil -> User.update_following_count(local_user)
40 _ -> User.unfollow(local_user, target_user)
43 Logger.info("relay: unfollowed instance: #{target_instance}: id=#{activity.data["id"]}")
46 error -> format_error(error)
50 defp fetch_target_user(ap_id, opts) do
51 case {opts[:force], User.get_or_fetch_by_ap_id(ap_id)} do
52 {_, {:ok, %User{} = user}} -> {:ok, user}
53 {true, _} -> {:ok, %User{ap_id: ap_id}}
58 @spec publish(any()) :: {:ok, Activity.t()} | {:error, any()}
59 def publish(%Activity{data: %{"type" => "Create"}} = activity) do
60 with %User{} = user <- get_actor(),
61 true <- Visibility.is_public?(activity) do
62 CommonAPI.repeat(activity.id, user)
64 error -> format_error(error)
68 def publish(_), do: {:error, "Not implemented"}
70 @spec list() :: {:ok, [%{actor: String.t(), followed_back: boolean()}]} | {:error, any()}
72 with %User{} = user <- get_actor() do
76 |> Enum.map(fn actor -> %{actor: actor, followed_back: true} end)
80 |> Pleroma.Activity.following_requests_for_actor()
81 |> Enum.map(fn activity -> %{actor: activity.data["object"], followed_back: false} end)
84 {:ok, accepted ++ without_accept}
86 error -> format_error(error)
90 @spec following() :: [String.t()]
96 defp following(user) do
98 |> User.following_ap_ids()
102 defp format_error({:error, error}), do: format_error(error)
104 defp format_error(error) do
105 Logger.error("error: #{inspect(error)}")