1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Federator do
7 alias Pleroma.Object.Containment
9 alias Pleroma.Web.ActivityPub.ActivityPub
10 alias Pleroma.Web.ActivityPub.Transmogrifier
11 alias Pleroma.Web.ActivityPub.Utils
12 alias Pleroma.Web.Federator.Publisher
13 alias Pleroma.Web.Federator.RetryQueue
14 alias Pleroma.Web.OStatus
15 alias Pleroma.Web.Websub
21 Process.sleep(1000 * 60)
22 refresh_subscriptions()
25 @doc "Addresses [memory leaks on recursive replies fetching](https://git.pleroma.social/pleroma/pleroma/issues/161)"
26 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
27 def allowed_incoming_reply_depth?(depth) do
28 max_replies_depth = Pleroma.Config.get([:instance, :federation_incoming_replies_max_depth])
30 if max_replies_depth do
31 (depth || 1) <= max_replies_depth
39 def incoming_doc(doc) do
40 PleromaJobQueue.enqueue(:federator_incoming, __MODULE__, [:incoming_doc, doc])
43 def incoming_ap_doc(params) do
44 PleromaJobQueue.enqueue(:federator_incoming, __MODULE__, [:incoming_ap_doc, params])
47 def publish(activity, priority \\ 1) do
48 PleromaJobQueue.enqueue(:federator_outgoing, __MODULE__, [:publish, activity], priority)
51 def verify_websub(websub) do
52 PleromaJobQueue.enqueue(:federator_outgoing, __MODULE__, [:verify_websub, websub])
55 def request_subscription(sub) do
56 PleromaJobQueue.enqueue(:federator_outgoing, __MODULE__, [:request_subscription, sub])
59 def refresh_subscriptions do
60 PleromaJobQueue.enqueue(:federator_outgoing, __MODULE__, [:refresh_subscriptions])
63 # Job Worker Callbacks
65 def perform(:refresh_subscriptions) do
66 Logger.debug("Federator running refresh subscriptions")
67 Websub.refresh_subscriptions()
71 Process.sleep(1000 * 60 * 60 * 6)
72 refresh_subscriptions()
76 def perform(:request_subscription, websub) do
77 Logger.debug("Refreshing #{websub.topic}")
79 with {:ok, websub} <- Websub.request_subscription(websub) do
80 Logger.debug("Successfully refreshed #{websub.topic}")
82 _e -> Logger.debug("Couldn't refresh #{websub.topic}")
86 def perform(:publish, activity) do
87 Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
89 with %User{} = actor <- User.get_cached_by_ap_id(activity.data["actor"]),
90 {:ok, actor} <- User.ensure_keys_present(actor) do
91 Publisher.publish(actor, activity)
95 def perform(:verify_websub, websub) do
97 "Running WebSub verification for #{websub.id} (#{websub.topic}, #{websub.callback})"
100 Websub.verify(websub)
103 def perform(:incoming_doc, doc) do
104 Logger.info("Got document, trying to parse")
105 OStatus.handle_incoming(doc)
108 def perform(:incoming_ap_doc, params) do
109 Logger.info("Handling incoming AP activity")
111 params = Utils.normalize_params(params)
113 # NOTE: we use the actor ID to do the containment, this is fine because an
114 # actor shouldn't be acting on objects outside their own AP server.
115 with {:ok, _user} <- ap_enabled_actor(params["actor"]),
116 nil <- Activity.normalize(params["id"]),
117 :ok <- Containment.contain_origin_from_id(params["actor"], params),
118 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
122 Logger.info("Already had #{params["id"]}")
126 # Just drop those for now
127 Logger.info("Unhandled activity")
128 Logger.info(Jason.encode!(params, pretty: true))
134 :publish_single_websub,
135 %{xml: _xml, topic: _topic, callback: _callback, secret: _secret} = params
137 case Websub.publish_one(params) do
142 RetryQueue.enqueue(params, Websub)
146 def perform(type, _) do
147 Logger.debug(fn -> "Unknown task: #{type}" end)
148 {:error, "Don't know what to do with this"}
151 def ap_enabled_actor(id) do
152 user = User.get_cached_by_ap_id(id)
154 if User.ap_enabled?(user) do
157 ActivityPub.make_user_from_ap_id(id)