2018-12-23 20:04:54 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2018-12-31 15:41:47 +00:00
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 20:04:54 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-04-26 16:33:10 +00:00
|
|
|
defmodule Pleroma.Web.Federator do
|
2019-08-09 17:08:01 +00:00
|
|
|
alias Pleroma.Workers.Publisher, as: PublisherWorker
|
|
|
|
alias Pleroma.Workers.Receiver, as: ReceiverWorker
|
|
|
|
alias Pleroma.Workers.Subscriber, as: SubscriberWorker
|
2019-02-06 19:20:02 +00:00
|
|
|
|
2017-04-26 16:33:10 +00:00
|
|
|
require Logger
|
|
|
|
|
2019-03-05 03:18:43 +00:00
|
|
|
def init do
|
2019-01-28 15:17:17 +00:00
|
|
|
# 1 minute
|
2019-08-09 17:08:01 +00:00
|
|
|
refresh_subscriptions(schedule_in: 60)
|
2018-05-07 16:11:37 +00:00
|
|
|
end
|
|
|
|
|
2019-06-29 17:04:50 +00:00
|
|
|
@doc "Addresses [memory leaks on recursive replies fetching](https://git.pleroma.social/pleroma/pleroma/issues/161)"
|
|
|
|
# credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
|
2019-06-30 12:58:50 +00:00
|
|
|
def allowed_incoming_reply_depth?(depth) do
|
|
|
|
max_replies_depth = Pleroma.Config.get([:instance, :federation_incoming_replies_max_depth])
|
|
|
|
|
|
|
|
if max_replies_depth do
|
|
|
|
(depth || 1) <= max_replies_depth
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2019-06-29 17:04:50 +00:00
|
|
|
|
2019-01-28 15:17:17 +00:00
|
|
|
# Client API
|
|
|
|
|
|
|
|
def incoming_doc(doc) do
|
2019-08-09 17:08:01 +00:00
|
|
|
%{"op" => "incoming_doc", "body" => doc}
|
|
|
|
|> ReceiverWorker.new(worker_args(:federator_incoming))
|
|
|
|
|> Pleroma.Repo.insert()
|
2019-01-28 15:17:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def incoming_ap_doc(params) do
|
2019-08-09 17:08:01 +00:00
|
|
|
%{"op" => "incoming_ap_doc", "params" => params}
|
|
|
|
|> ReceiverWorker.new(worker_args(:federator_incoming))
|
|
|
|
|> Pleroma.Repo.insert()
|
2019-01-28 15:17:17 +00:00
|
|
|
end
|
|
|
|
|
2019-08-09 17:08:01 +00:00
|
|
|
def publish(%{id: "pleroma:fakeid"} = activity) do
|
|
|
|
PublisherWorker.perform_publish(activity)
|
2019-01-28 15:17:17 +00:00
|
|
|
end
|
|
|
|
|
2019-08-09 17:08:01 +00:00
|
|
|
def publish(activity) do
|
|
|
|
%{"op" => "publish", "activity_id" => activity.id}
|
|
|
|
|> PublisherWorker.new(worker_args(:federator_outgoing))
|
|
|
|
|> Pleroma.Repo.insert()
|
2017-08-02 10:34:48 +00:00
|
|
|
end
|
|
|
|
|
2019-08-09 17:08:01 +00:00
|
|
|
def verify_websub(websub) do
|
|
|
|
%{"op" => "verify_websub", "websub_id" => websub.id}
|
|
|
|
|> SubscriberWorker.new(worker_args(:federator_outgoing))
|
|
|
|
|> Pleroma.Repo.insert()
|
2017-05-06 10:34:40 +00:00
|
|
|
end
|
|
|
|
|
2019-08-09 17:08:01 +00:00
|
|
|
def request_subscription(websub) do
|
|
|
|
%{"op" => "request_subscription", "websub_id" => websub.id}
|
|
|
|
|> SubscriberWorker.new(worker_args(:federator_outgoing))
|
|
|
|
|> Pleroma.Repo.insert()
|
2018-02-21 07:51:03 +00:00
|
|
|
end
|
|
|
|
|
2019-08-09 17:08:01 +00:00
|
|
|
def refresh_subscriptions(worker_args \\ []) do
|
|
|
|
%{"op" => "refresh_subscriptions"}
|
|
|
|
|> SubscriberWorker.new(worker_args ++ [max_attempts: 1] ++ worker_args(:federator_outgoing))
|
|
|
|
|> Pleroma.Repo.insert()
|
2017-04-26 16:33:10 +00:00
|
|
|
end
|
|
|
|
|
2019-08-09 17:08:01 +00:00
|
|
|
defp worker_args(queue) do
|
|
|
|
if max_attempts = Pleroma.Config.get([:workers, :retries, queue]) do
|
|
|
|
[max_attempts: max_attempts]
|
2018-02-21 07:51:03 +00:00
|
|
|
else
|
2019-08-09 17:08:01 +00:00
|
|
|
[]
|
2018-02-21 07:51:03 +00:00
|
|
|
end
|
|
|
|
end
|
2017-04-26 16:33:10 +00:00
|
|
|
end
|