Merge branch 'feature/omit-relay-user' into 'develop'
[akkoma] / lib / pleroma / web / activity_pub / relay.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.Object
8 alias Pleroma.User
9 alias Pleroma.Web.ActivityPub.ActivityPub
10 require Logger
11
12 def get_actor do
13 actor =
14 relay_ap_id()
15 |> User.get_or_create_service_actor_by_ap_id()
16
17 {:ok, actor} = User.set_invisible(actor, true)
18 actor
19 end
20
21 def relay_ap_id do
22 "#{Pleroma.Web.Endpoint.url()}/relay"
23 end
24
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"]}")
31 {:ok, activity}
32 else
33 error -> format_error(error)
34 end
35 end
36
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"]}")
44 {:ok, activity}
45 else
46 error -> format_error(error)
47 end
48 end
49
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)
55 else
56 error -> format_error(error)
57 end
58 end
59
60 def publish(_), do: {:error, "Not implemented"}
61
62 @spec list() :: {:ok, [String.t()]} | {:error, any()}
63 def list do
64 with %User{} = user <- get_actor() do
65 list =
66 user
67 |> User.following()
68 |> Enum.map(fn entry -> URI.parse(entry).host end)
69 |> Enum.uniq()
70
71 {:ok, list}
72 else
73 error -> format_error(error)
74 end
75 end
76
77 defp format_error({:error, error}), do: format_error(error)
78
79 defp format_error(error) do
80 Logger.error("error: #{inspect(error)}")
81 {:error, error}
82 end
83 end