Merge remote-tracking branch 'pleroma/develop' into instance-deletion
[akkoma] / lib / pleroma / web / activity_pub / relay.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.Relay do
6 alias Pleroma.Activity
7 alias Pleroma.User
8 alias Pleroma.Web.ActivityPub.ActivityPub
9 alias Pleroma.Web.ActivityPub.Visibility
10 alias Pleroma.Web.CommonAPI
11 require Logger
12
13 @nickname "relay"
14
15 @spec ap_id() :: String.t()
16 def ap_id, do: "#{Pleroma.Web.Endpoint.url()}/#{@nickname}"
17
18 @spec get_actor() :: User.t() | nil
19 def get_actor, do: User.get_or_create_service_actor_by_ap_id(ap_id(), @nickname)
20
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"]}")
27 {:ok, activity}
28 else
29 error -> format_error(error)
30 end
31 end
32
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)
41 end
42
43 Logger.info("relay: unfollowed instance: #{target_instance}: id=#{activity.data["id"]}")
44 {:ok, activity}
45 else
46 error -> format_error(error)
47 end
48 end
49
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}}
54 {_, error} -> error
55 end
56 end
57
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)
63 else
64 error -> format_error(error)
65 end
66 end
67
68 def publish(_), do: {:error, "Not implemented"}
69
70 @spec list() :: {:ok, [%{actor: String.t(), followed_back: boolean()}]} | {:error, any()}
71 def list do
72 with %User{} = user <- get_actor() do
73 accepted =
74 user
75 |> following()
76 |> Enum.map(fn actor -> %{actor: actor, followed_back: true} end)
77
78 without_accept =
79 user
80 |> Pleroma.Activity.following_requests_for_actor()
81 |> Enum.map(fn activity -> %{actor: activity.data["object"], followed_back: false} end)
82 |> Enum.uniq()
83
84 {:ok, accepted ++ without_accept}
85 else
86 error -> format_error(error)
87 end
88 end
89
90 @spec following() :: [String.t()]
91 def following do
92 get_actor()
93 |> following()
94 end
95
96 defp following(user) do
97 user
98 |> User.following_ap_ids()
99 |> Enum.uniq()
100 end
101
102 defp format_error({:error, error}), do: format_error(error)
103
104 defp format_error(error) do
105 Logger.error("error: #{inspect(error)}")
106 {:error, error}
107 end
108 end