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
8 alias Pleroma.Web.WebFinger
9 alias Pleroma.Web.Websub
10 alias Pleroma.Web.Salmon
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.Relay
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.ActivityPub.Utils
15 alias Pleroma.Web.Federator.RetryQueue
16 alias Pleroma.Web.OStatus
21 @websub Application.get_env(:pleroma, :websub)
22 @ostatus Application.get_env(:pleroma, :ostatus)
26 Process.sleep(1000 * 60 * 1)
27 refresh_subscriptions()
32 def incoming_doc(doc) do
33 Jobs.enqueue(:federator_incoming, __MODULE__, [:incoming_doc, doc])
36 def incoming_ap_doc(params) do
37 Jobs.enqueue(:federator_incoming, __MODULE__, [:incoming_ap_doc, params])
40 def publish(activity, priority \\ 1) do
41 Jobs.enqueue(:federator_outgoing, __MODULE__, [:publish, activity], priority)
44 def publish_single_ap(params) do
45 Jobs.enqueue(:federator_outgoing, __MODULE__, [:publish_single_ap, params])
48 def publish_single_websub(websub) do
49 Jobs.enqueue(:federator_outgoing, __MODULE__, [:publish_single_websub, websub])
52 def verify_websub(websub) do
53 Jobs.enqueue(:federator_outgoing, __MODULE__, [:verify_websub, websub])
56 def request_subscription(sub) do
57 Jobs.enqueue(:federator_outgoing, __MODULE__, [:request_subscription, sub])
60 def refresh_subscriptions() do
61 Jobs.enqueue(:federator_outgoing, __MODULE__, [:refresh_subscriptions])
64 def publish_single_salmon(params) do
65 Jobs.enqueue(:federator_outgoing, __MODULE__, [:publish_single_salmon, params])
68 # Job Worker Callbacks
70 def perform(:refresh_subscriptions) do
71 Logger.debug("Federator running refresh subscriptions")
72 Websub.refresh_subscriptions()
76 Process.sleep(1000 * 60 * 60 * 6)
77 refresh_subscriptions()
81 def perform(:request_subscription, websub) do
82 Logger.debug("Refreshing #{websub.topic}")
84 with {:ok, websub} <- Websub.request_subscription(websub) do
85 Logger.debug("Successfully refreshed #{websub.topic}")
87 _e -> Logger.debug("Couldn't refresh #{websub.topic}")
91 def perform(:publish, activity) do
92 Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
94 with actor when not is_nil(actor) <- User.get_cached_by_ap_id(activity.data["actor"]) do
95 {:ok, actor} = WebFinger.ensure_keys_present(actor)
97 if ActivityPub.is_public?(activity) do
98 if OStatus.is_representable?(activity) do
99 Logger.info(fn -> "Sending #{activity.data["id"]} out via WebSub" end)
100 Websub.publish(Pleroma.Web.OStatus.feed_path(actor), actor, activity)
102 Logger.info(fn -> "Sending #{activity.data["id"]} out via Salmon" end)
103 Pleroma.Web.Salmon.publish(actor, activity)
106 if Keyword.get(Application.get_env(:pleroma, :instance), :allow_relay) do
107 Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
108 Relay.publish(activity)
112 Logger.info(fn -> "Sending #{activity.data["id"]} out via AP" end)
113 Pleroma.Web.ActivityPub.ActivityPub.publish(actor, activity)
117 def perform(:verify_websub, websub) do
119 "Running WebSub verification for #{websub.id} (#{websub.topic}, #{websub.callback})"
122 @websub.verify(websub)
125 def perform(:incoming_doc, doc) do
126 Logger.info("Got document, trying to parse")
127 @ostatus.handle_incoming(doc)
130 def perform(:incoming_ap_doc, params) do
131 Logger.info("Handling incoming AP activity")
133 params = Utils.normalize_params(params)
135 # NOTE: we use the actor ID to do the containment, this is fine because an
136 # actor shouldn't be acting on objects outside their own AP server.
137 with {:ok, _user} <- ap_enabled_actor(params["actor"]),
138 nil <- Activity.normalize(params["id"]),
139 :ok <- Transmogrifier.contain_origin_from_id(params["actor"], params),
140 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
144 Logger.info("Already had #{params["id"]}")
148 # Just drop those for now
149 Logger.info("Unhandled activity")
150 Logger.info(Poison.encode!(params, pretty: 2))
155 def perform(:publish_single_salmon, params) do
156 Salmon.send_to_user(params)
159 def perform(:publish_single_ap, params) do
160 case ActivityPub.publish_one(params) do
165 RetryQueue.enqueue(params, ActivityPub)
170 :publish_single_websub,
171 %{xml: _xml, topic: _topic, callback: _callback, secret: _secret} = params
173 case Websub.publish_one(params) do
178 RetryQueue.enqueue(params, Websub)
182 def perform(type, _) do
183 Logger.debug(fn -> "Unknown task: #{type}" end)
184 {:error, "Don't know what to do with this"}
187 def ap_enabled_actor(id) do
188 user = User.get_by_ap_id(id)
190 if User.ap_enabled?(user) do
193 ActivityPub.make_user_from_ap_id(id)