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.Relay
11 alias Pleroma.Web.ActivityPub.Transmogrifier
12 alias Pleroma.Web.ActivityPub.Utils
13 alias Pleroma.Web.ActivityPub.Visibility
14 alias Pleroma.Web.Federator.RetryQueue
15 alias Pleroma.Web.OStatus
16 alias Pleroma.Web.Salmon
17 alias Pleroma.Web.WebFinger
18 alias Pleroma.Web.Websub
22 @websub Application.get_env(:pleroma, :websub)
23 @ostatus Application.get_env(:pleroma, :ostatus)
27 Process.sleep(1000 * 60)
28 refresh_subscriptions()
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 publish_single_ap(params) do
46 PleromaJobQueue.enqueue(:federator_outgoing, __MODULE__, [:publish_single_ap, params])
49 def publish_single_websub(websub) do
50 PleromaJobQueue.enqueue(:federator_outgoing, __MODULE__, [:publish_single_websub, websub])
53 def verify_websub(websub) do
54 PleromaJobQueue.enqueue(:federator_outgoing, __MODULE__, [:verify_websub, websub])
57 def request_subscription(sub) do
58 PleromaJobQueue.enqueue(:federator_outgoing, __MODULE__, [:request_subscription, sub])
61 def refresh_subscriptions do
62 PleromaJobQueue.enqueue(:federator_outgoing, __MODULE__, [:refresh_subscriptions])
65 def publish_single_salmon(params) do
66 PleromaJobQueue.enqueue(:federator_outgoing, __MODULE__, [:publish_single_salmon, params])
69 # Job Worker Callbacks
71 def perform(:refresh_subscriptions) do
72 Logger.debug("Federator running refresh subscriptions")
73 Websub.refresh_subscriptions()
77 Process.sleep(1000 * 60 * 60 * 6)
78 refresh_subscriptions()
82 def perform(:request_subscription, websub) do
83 Logger.debug("Refreshing #{websub.topic}")
85 with {:ok, websub} <- Websub.request_subscription(websub) do
86 Logger.debug("Successfully refreshed #{websub.topic}")
88 _e -> Logger.debug("Couldn't refresh #{websub.topic}")
92 def perform(:publish, activity) do
93 Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
95 with actor when not is_nil(actor) <- User.get_cached_by_ap_id(activity.data["actor"]) do
96 {:ok, actor} = WebFinger.ensure_keys_present(actor)
98 if Visibility.is_public?(activity) do
99 if OStatus.is_representable?(activity) do
100 Logger.info(fn -> "Sending #{activity.data["id"]} out via WebSub" end)
101 Websub.publish(Pleroma.Web.OStatus.feed_path(actor), actor, activity)
103 Logger.info(fn -> "Sending #{activity.data["id"]} out via Salmon" end)
104 Pleroma.Web.Salmon.publish(actor, activity)
107 if Keyword.get(Application.get_env(:pleroma, :instance), :allow_relay) do
108 Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
109 Relay.publish(activity)
113 Logger.info(fn -> "Sending #{activity.data["id"]} out via AP" end)
114 Pleroma.Web.ActivityPub.ActivityPub.publish(actor, activity)
118 def perform(:verify_websub, websub) do
120 "Running WebSub verification for #{websub.id} (#{websub.topic}, #{websub.callback})"
123 @websub.verify(websub)
126 def perform(:incoming_doc, doc) do
127 Logger.info("Got document, trying to parse")
128 @ostatus.handle_incoming(doc)
131 def perform(:incoming_ap_doc, params) do
132 Logger.info("Handling incoming AP activity")
134 params = Utils.normalize_params(params)
136 # NOTE: we use the actor ID to do the containment, this is fine because an
137 # actor shouldn't be acting on objects outside their own AP server.
138 with {:ok, _user} <- ap_enabled_actor(params["actor"]),
139 nil <- Activity.normalize(params["id"]),
140 :ok <- Containment.contain_origin_from_id(params["actor"], params),
141 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
145 Logger.info("Already had #{params["id"]}")
149 # Just drop those for now
150 Logger.info("Unhandled activity")
151 Logger.info(Poison.encode!(params, pretty: 2))
156 def perform(:publish_single_salmon, params) do
157 Salmon.send_to_user(params)
160 def perform(:publish_single_ap, params) do
161 case ActivityPub.publish_one(params) do
166 RetryQueue.enqueue(params, ActivityPub)
171 :publish_single_websub,
172 %{xml: _xml, topic: _topic, callback: _callback, secret: _secret} = params
174 case Websub.publish_one(params) do
179 RetryQueue.enqueue(params, Websub)
183 def perform(type, _) do
184 Logger.debug(fn -> "Unknown task: #{type}" end)
185 {:error, "Don't know what to do with this"}
188 def ap_enabled_actor(id) do
189 user = User.get_cached_by_ap_id(id)
191 if User.ap_enabled?(user) do
194 ActivityPub.make_user_from_ap_id(id)