federator.ex 1.1 KB
Newer Older
1 2 3 4
defmodule Pleroma.Web.Federator do
  alias Pleroma.User
  require Logger

lain's avatar
lain committed
5
  @websub Application.get_env(:pleroma, :websub)
6 7 8 9

  def handle(:publish, activity) do
    Logger.debug("Running publish for #{activity.data["id"]}")
    with actor when not is_nil(actor) <- User.get_cached_by_ap_id(activity.data["actor"]) do
lain's avatar
lain committed
10
      Logger.debug("Sending #{activity.data["id"]} out via websub")
11
      Pleroma.Web.Websub.publish(Pleroma.Web.OStatus.feed_path(actor), actor, activity)
lain's avatar
lain committed
12 13 14

      Logger.debug("Sending #{activity.data["id"]} out via salmon")
      Pleroma.Web.Salmon.publish(actor, activity)
15 16 17 18 19
    end
  end

  def handle(:verify_websub, websub) do
    Logger.debug("Running websub verification for #{websub.id} (#{websub.topic}, #{websub.callback})")
lain's avatar
lain committed
20
    @websub.verify(websub)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  end

  def handle(type, payload) do
    Logger.debug("Unknown task: #{type}")
    {:error, "Don't know what do do with this"}
  end

  def enqueue(type, payload) do
    # for now, just run immediately in a new process.
    if Mix.env == :test do
      handle(type, payload)
    else
      spawn(fn -> handle(type, payload) end)
    end
  end
end