1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.Publisher do
10 alias Pleroma.Instances
14 alias Pleroma.Web.ActivityPub.Relay
15 alias Pleroma.Web.ActivityPub.Transmogrifier
16 alias Pleroma.Web.FedSockets
18 require Pleroma.Constants
20 import Pleroma.Web.ActivityPub.Visibility
22 @behaviour Pleroma.Web.Federator.Publisher
27 ActivityPub outgoing federation module.
31 Determine if an activity can be represented by running it through Transmogrifier.
33 def is_representable?(%Activity{} = activity) do
34 with {:ok, _data} <- Transmogrifier.prepare_outgoing(activity.data) do
43 Publish a single message to a peer. Takes a struct with the following
46 * `inbox`: the inbox to publish to
47 * `json`: the JSON message body representing the ActivityPub message
48 * `actor`: the actor which is signing the message
49 * `id`: the ActivityStreams URI of the message
51 def publish_one(%{inbox: inbox, json: json, actor: %User{} = actor, id: id} = params) do
52 Logger.debug("Federating #{id} to #{inbox}")
54 case FedSockets.get_or_create_fed_socket(inbox) do
56 Logger.debug("publishing via fedsockets - #{inspect(inbox)}")
57 FedSockets.publish(fedsocket, json)
60 Logger.debug("publishing via http - #{inspect(inbox)}")
61 http_publish(inbox, actor, json, params)
65 def publish_one(%{actor_id: actor_id} = params) do
66 actor = User.get_cached_by_id(actor_id)
69 |> Map.delete(:actor_id)
70 |> Map.put(:actor, actor)
74 defp http_publish(inbox, actor, json, params) do
75 uri = %{path: path} = URI.parse(inbox)
76 digest = "SHA-256=" <> (:crypto.hash(:sha256, json) |> Base.encode64())
78 date = Pleroma.Signature.signed_date()
81 Pleroma.Signature.sign(actor, %{
82 "(request-target)": "post #{path}",
83 host: signature_host(uri),
84 "content-length": byte_size(json),
89 with {:ok, %{status: code}} when code in 200..299 <-
95 {"Content-Type", "application/activity+json"},
97 {"signature", signature},
101 if not Map.has_key?(params, :unreachable_since) || params[:unreachable_since] do
102 Instances.set_reachable(inbox)
107 {_post_result, response} ->
108 unless params[:unreachable_since], do: Instances.set_unreachable(inbox)
113 defp signature_host(%URI{port: port, scheme: scheme, host: host}) do
114 if port == URI.default_port(scheme) do
121 defp should_federate?(inbox, public) do
125 %{host: host} = URI.parse(inbox)
127 quarantined_instances =
128 Config.get([:instance, :quarantined_instances], [])
129 |> Pleroma.Web.ActivityPub.MRF.subdomains_regex()
131 !Pleroma.Web.ActivityPub.MRF.subdomain_match?(quarantined_instances, host)
135 @spec recipients(User.t(), Activity.t()) :: list(User.t()) | []
136 defp recipients(actor, activity) do
138 if actor.follower_address in activity.recipients do
139 User.get_external_followers(actor)
145 with %Activity{data: %{"type" => "Delete"}} <- activity,
146 %Object{id: object_id} <- Object.normalize(activity),
147 fetchers <- User.get_delivered_users_by_object_id(object_id),
148 _ <- Delivery.delete_all_by_object_id(object_id) do
155 Pleroma.Web.Federator.Publisher.remote_users(actor, activity) ++ followers ++ fetchers
158 defp get_cc_ap_ids(ap_id, recipients) do
159 host = Map.get(URI.parse(ap_id), :host)
162 |> Enum.filter(fn %User{ap_id: ap_id} -> Map.get(URI.parse(ap_id), :host) == host end)
163 |> Enum.map(& &1.ap_id)
166 defp maybe_use_sharedinbox(%User{shared_inbox: nil, inbox: inbox}), do: inbox
167 defp maybe_use_sharedinbox(%User{shared_inbox: shared_inbox}), do: shared_inbox
170 Determine a user inbox to use based on heuristics. These heuristics
171 are based on an approximation of the ``sharedInbox`` rules in the
172 [ActivityPub specification][ap-sharedinbox].
174 Please do not edit this function (or its children) without reading
175 the spec, as editing the code is likely to introduce some breakage
176 without some familiarity.
178 [ap-sharedinbox]: https://www.w3.org/TR/activitypub/#shared-inbox-delivery
181 %Activity{data: activity_data},
182 %User{inbox: inbox} = user
184 to = activity_data["to"] || []
185 cc = activity_data["cc"] || []
186 type = activity_data["type"]
190 maybe_use_sharedinbox(user)
192 Pleroma.Constants.as_public() in to || Pleroma.Constants.as_public() in cc ->
193 maybe_use_sharedinbox(user)
195 length(to) + length(cc) > 1 ->
196 maybe_use_sharedinbox(user)
204 Publishes an activity with BCC to all relevant peers.
207 def publish(%User{} = actor, %{data: %{"bcc" => bcc}} = activity)
208 when is_list(bcc) and bcc != [] do
209 public = is_public?(activity)
210 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
212 recipients = recipients(actor, activity)
216 |> Enum.filter(&User.ap_enabled?/1)
217 |> Enum.map(fn actor -> actor.inbox end)
218 |> Enum.filter(fn inbox -> should_federate?(inbox, public) end)
219 |> Instances.filter_reachable()
222 Enum.each(inboxes, fn {inbox, unreachable_since} ->
223 %User{ap_id: ap_id} = Enum.find(recipients, fn actor -> actor.inbox == inbox end)
225 # Get all the recipients on the same host and add them to cc. Otherwise, a remote
226 # instance would only accept a first message for the first recipient and ignore the rest.
227 cc = get_cc_ap_ids(ap_id, recipients)
234 Pleroma.Web.Federator.Publisher.enqueue_one(__MODULE__, %{
238 id: activity.data["id"],
239 unreachable_since: unreachable_since
245 # Publishes an activity to all relevant peers.
246 def publish(%User{} = actor, %Activity{} = activity) do
247 public = is_public?(activity)
249 if public && Config.get([:instance, :allow_relay]) do
250 Logger.debug(fn -> "Relaying #{activity.data["id"]} out" end)
251 Relay.publish(activity)
254 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
255 json = Jason.encode!(data)
257 recipients(actor, activity)
258 |> Enum.filter(fn user -> User.ap_enabled?(user) end)
259 |> Enum.map(fn %User{} = user ->
260 determine_inbox(activity, user)
263 |> Enum.filter(fn inbox -> should_federate?(inbox, public) end)
264 |> Instances.filter_reachable()
265 |> Enum.each(fn {inbox, unreachable_since} ->
266 Pleroma.Web.Federator.Publisher.enqueue_one(
272 id: activity.data["id"],
273 unreachable_since: unreachable_since
279 def gather_webfinger_links(%User{} = user) do
281 %{"rel" => "self", "type" => "application/activity+json", "href" => user.ap_id},
284 "type" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"",
288 "rel" => "http://ostatus.org/schema/1.0/subscribe",
289 "template" => "#{Pleroma.Web.base_url()}/ostatus_subscribe?acct={uri}"
294 def gather_nodeinfo_protocol_names, do: ["activitypub"]