Merge branch 'digest-template' 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 "#{Pleroma.Web.Endpoint.url()}/relay"
14 |> User.get_or_create_service_actor_by_ap_id()
15 end
16
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"]}")
23 {:ok, activity}
24 else
25 {:error, _} = error ->
26 Logger.error("error: #{inspect(error)}")
27 error
28
29 e ->
30 Logger.error("error: #{inspect(e)}")
31 {:error, e}
32 end
33 end
34
35 @spec unfollow(String.t()) :: {:ok, Activity.t()} | {:error, any()}
36 def unfollow(target_instance) do
37 with %User{} = local_user <- get_actor(),
38 {:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_instance),
39 {:ok, activity} <- ActivityPub.unfollow(local_user, target_user) do
40 Logger.info("relay: unfollowed instance: #{target_instance}: id=#{activity.data["id"]}")
41 {:ok, activity}
42 else
43 {:error, _} = error ->
44 Logger.error("error: #{inspect(error)}")
45 error
46
47 e ->
48 Logger.error("error: #{inspect(e)}")
49 {:error, e}
50 end
51 end
52
53 @spec publish(any()) :: {:ok, Activity.t(), Object.t()} | {:error, any()}
54 def publish(%Activity{data: %{"type" => "Create"}} = activity) do
55 with %User{} = user <- get_actor(),
56 %Object{} = object <- Object.normalize(activity) do
57 ActivityPub.announce(user, object, nil, true, false)
58 else
59 e ->
60 Logger.error("error: #{inspect(e)}")
61 {:error, inspect(e)}
62 end
63 end
64
65 def publish(_), do: {:error, "Not implemented"}
66 end