relay.ex 1.61 KB
Newer Older
1
# Pleroma: A lightweight social networking server
kaniini's avatar
kaniini committed
2
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 4
# SPDX-License-Identifier: AGPL-3.0-only

5
defmodule Pleroma.Web.ActivityPub.Relay do
Haelwenn's avatar
Haelwenn committed
6 7 8
  alias Pleroma.User
  alias Pleroma.Object
  alias Pleroma.Activity
9 10
  alias Pleroma.Web.ActivityPub.ActivityPub
  require Logger
11 12 13 14

  def get_actor do
    User.get_or_create_instance_user()
  end
15 16 17 18 19 20

  def follow(target_instance) do
    with %User{} = local_user <- get_actor(),
         %User{} = target_user <- User.get_or_fetch_by_ap_id(target_instance),
         {:ok, activity} <- ActivityPub.follow(local_user, target_user) do
      Logger.info("relay: followed instance: #{target_instance}; id=#{activity.data["id"]}")
21
      {:ok, activity}
22
    else
23 24
      e ->
        Logger.error("error: #{inspect(e)}")
25
        {:error, e}
26 27 28 29 30 31 32 33
    end
  end

  def unfollow(target_instance) do
    with %User{} = local_user <- get_actor(),
         %User{} = target_user <- User.get_or_fetch_by_ap_id(target_instance),
         {:ok, activity} <- ActivityPub.unfollow(local_user, target_user) do
      Logger.info("relay: unfollowed instance: #{target_instance}: id=#{activity.data["id"]}")
34
      {:ok, activity}
35
    else
36 37
      e ->
        Logger.error("error: #{inspect(e)}")
38
        {:error, e}
39 40
    end
  end
41

kaniini's avatar
kaniini committed
42
  def publish(%Activity{data: %{"type" => "Create"}} = activity) do
43 44
    with %User{} = user <- get_actor(),
         %Object{} = object <- Object.normalize(activity.data["object"]["id"]) do
45
      ActivityPub.announce(user, object, nil, true, false)
46 47 48 49
    else
      e -> Logger.error("error: #{inspect(e)}")
    end
  end
kaniini's avatar
kaniini committed
50 51

  def publish(_), do: nil
52
end