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 @max_replies_depth 100
27 @doc "Addresses [memory leaks on recursive replies fetching](https://git.pleroma.social/pleroma/pleroma/issues/161)"
28 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
29 def max_replies_depth, do: @max_replies_depth
33 def incoming_doc(doc) do
34 PleromaJobQueue.enqueue(:federator_incoming, __MODULE__, [:incoming_doc, doc])
37 def incoming_ap_doc(params) do
38 PleromaJobQueue.enqueue(:federator_incoming, __MODULE__, [:incoming_ap_doc, params])
41 def publish(activity, priority \\ 1) do
42 PleromaJobQueue.enqueue(:federator_outgoing, __MODULE__, [:publish, activity], priority)
45 def verify_websub(websub) do
46 PleromaJobQueue.enqueue(:federator_outgoing, __MODULE__, [:verify_websub, websub])
49 def request_subscription(sub) do
50 PleromaJobQueue.enqueue(:federator_outgoing, __MODULE__, [:request_subscription, sub])
53 def refresh_subscriptions do
54 PleromaJobQueue.enqueue(:federator_outgoing, __MODULE__, [:refresh_subscriptions])
57 # Job Worker Callbacks
59 def perform(:refresh_subscriptions) do
60 Logger.debug("Federator running refresh subscriptions")
61 Websub.refresh_subscriptions()
65 Process.sleep(1000 * 60 * 60 * 6)
66 refresh_subscriptions()
70 def perform(:request_subscription, websub) do
71 Logger.debug("Refreshing #{websub.topic}")
73 with {:ok, websub} <- Websub.request_subscription(websub) do
74 Logger.debug("Successfully refreshed #{websub.topic}")
76 _e -> Logger.debug("Couldn't refresh #{websub.topic}")
80 def perform(:publish, activity) do
81 Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
83 with %User{} = actor <- User.get_cached_by_ap_id(activity.data["actor"]),
84 {:ok, actor} <- User.ensure_keys_present(actor) do
85 Publisher.publish(actor, activity)
89 def perform(:verify_websub, websub) do
91 "Running WebSub verification for #{websub.id} (#{websub.topic}, #{websub.callback})"
97 def perform(:incoming_doc, doc) do
98 Logger.info("Got document, trying to parse")
99 OStatus.handle_incoming(doc)
102 def perform(:incoming_ap_doc, params) do
103 Logger.info("Handling incoming AP activity")
105 params = Utils.normalize_params(params)
107 # NOTE: we use the actor ID to do the containment, this is fine because an
108 # actor shouldn't be acting on objects outside their own AP server.
109 with {:ok, _user} <- ap_enabled_actor(params["actor"]),
110 nil <- Activity.normalize(params["id"]),
111 :ok <- Containment.contain_origin_from_id(params["actor"], params),
112 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
116 Logger.info("Already had #{params["id"]}")
120 # Just drop those for now
121 Logger.info("Unhandled activity")
122 Logger.info(Jason.encode!(params, pretty: true))
128 :publish_single_websub,
129 %{xml: _xml, topic: _topic, callback: _callback, secret: _secret} = params
131 case Websub.publish_one(params) do
136 RetryQueue.enqueue(params, Websub)
140 def perform(type, _) do
141 Logger.debug(fn -> "Unknown task: #{type}" end)
142 {:error, "Don't know what to do with this"}
145 def ap_enabled_actor(id) do
146 user = User.get_cached_by_ap_id(id)
148 if User.ap_enabled?(user) do
151 ActivityPub.make_user_from_ap_id(id)