Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma 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 "#{Pleroma.Web.Endpoint.url()}/relay"
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 @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} <- ActivityPub.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()) :: {:ok, Activity.t()} | {:error, any()}
34 def unfollow(target_instance) do
35 with %User{} = local_user <- get_actor(),
36 {:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_instance),
37 {:ok, activity} <- ActivityPub.unfollow(local_user, target_user) do
38 User.unfollow(local_user, target_user)
39 Logger.info("relay: unfollowed instance: #{target_instance}: id=#{activity.data["id"]}")
40 {:ok, activity}
41 else
42 error -> format_error(error)
43 end
44 end
45
46 @spec publish(any()) :: {:ok, Activity.t(), Object.t()} | {:error, any()}
47 def publish(%Activity{data: %{"type" => "Create"}} = activity) do
48 with %User{} = user <- get_actor(),
49 %Object{} = object <- Object.normalize(activity) do
50 ActivityPub.announce(user, object, nil, true, false)
51 else
52 error -> format_error(error)
53 end
54 end
55
56 def publish(_), do: {:error, "Not implemented"}
57
58 @spec list() :: {:ok, [String.t()]} | {:error, any()}
59 def list do
60 with %User{following: following} = _user <- get_actor() do
61 list =
62 following
63 |> Enum.map(fn entry -> URI.parse(entry).host end)
64 |> Enum.uniq()
65
66 {:ok, list}
67 else
68 error -> format_error(error)
69 end
70 end
71
72 defp format_error({:error, error}), do: format_error(error)
73
74 defp format_error(error) do
75 Logger.error("error: #{inspect(error)}")
76 {:error, error}
77 end
78 end