1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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
17 require Pleroma.Constants
19 import Pleroma.Web.ActivityPub.Visibility
21 @behaviour Pleroma.Web.Federator.Publisher
26 ActivityPub outgoing federation module.
30 Determine if an activity can be represented by running it through Transmogrifier.
32 def is_representable?(%Activity{} = activity) do
33 with {:ok, _data} <- Transmogrifier.prepare_outgoing(activity.data) do
42 Publish a single message to a peer. Takes a struct with the following
45 * `inbox`: the inbox to publish to
46 * `json`: the JSON message body representing the ActivityPub message
47 * `actor`: the actor which is signing the message
48 * `id`: the ActivityStreams URI of the message
50 def publish_one(%{inbox: inbox, json: json, actor: %User{} = actor, id: id} = params) do
51 Logger.debug("Federating #{id} to #{inbox}")
52 uri = %{path: path} = URI.parse(inbox)
53 digest = "SHA-256=" <> (:crypto.hash(:sha256, json) |> Base.encode64())
55 date = Pleroma.Signature.signed_date()
58 Pleroma.Signature.sign(actor, %{
59 "(request-target)": "post #{path}",
60 host: signature_host(uri),
61 "content-length": byte_size(json),
66 with {:ok, %{status: code}} = result when code in 200..299 <-
71 {"Content-Type", "application/activity+json"},
73 {"signature", signature},
77 if not Map.has_key?(params, :unreachable_since) || params[:unreachable_since] do
78 Instances.set_reachable(inbox)
83 {_post_result, response} ->
84 unless params[:unreachable_since], do: Instances.set_unreachable(inbox)
89 def publish_one(%{actor_id: actor_id} = params) do
90 actor = User.get_cached_by_id(actor_id)
93 |> Map.delete(:actor_id)
94 |> Map.put(:actor, actor)
98 defp signature_host(%URI{port: port, scheme: scheme, host: host}) do
99 if port == URI.default_port(scheme) do
106 defp blocked_instances do
107 Config.get([:instance, :quarantined_instances], []) ++
108 Config.get([:mrf_simple, :reject], [])
111 def should_federate?(url) do
112 %{host: host} = URI.parse(url)
114 quarantined_instances =
116 |> Pleroma.Web.ActivityPub.MRF.instance_list_from_tuples()
117 |> Pleroma.Web.ActivityPub.MRF.subdomains_regex()
119 !Pleroma.Web.ActivityPub.MRF.subdomain_match?(quarantined_instances, host)
122 @spec recipients(User.t(), Activity.t()) :: list(User.t()) | []
123 defp recipients(actor, activity) do
125 if actor.follower_address in activity.recipients do
126 User.get_external_followers(actor)
132 with %Activity{data: %{"type" => "Delete"}} <- activity,
133 %Object{id: object_id} <- Object.normalize(activity, fetch: false),
134 fetchers <- User.get_delivered_users_by_object_id(object_id),
135 _ <- Delivery.delete_all_by_object_id(object_id) do
142 Pleroma.Web.Federator.Publisher.remote_users(actor, activity) ++ followers ++ fetchers
145 defp get_cc_ap_ids(ap_id, recipients) do
146 host = Map.get(URI.parse(ap_id), :host)
149 |> Enum.filter(fn %User{ap_id: ap_id} -> Map.get(URI.parse(ap_id), :host) == host end)
150 |> Enum.map(& &1.ap_id)
153 defp maybe_use_sharedinbox(%User{shared_inbox: nil, inbox: inbox}), do: inbox
154 defp maybe_use_sharedinbox(%User{shared_inbox: shared_inbox}), do: shared_inbox
157 Determine a user inbox to use based on heuristics. These heuristics
158 are based on an approximation of the ``sharedInbox`` rules in the
159 [ActivityPub specification][ap-sharedinbox].
161 Please do not edit this function (or its children) without reading
162 the spec, as editing the code is likely to introduce some breakage
163 without some familiarity.
165 [ap-sharedinbox]: https://www.w3.org/TR/activitypub/#shared-inbox-delivery
168 %Activity{data: activity_data},
169 %User{inbox: inbox} = user
171 to = activity_data["to"] || []
172 cc = activity_data["cc"] || []
173 type = activity_data["type"]
177 maybe_use_sharedinbox(user)
179 Pleroma.Constants.as_public() in to || Pleroma.Constants.as_public() in cc ->
180 maybe_use_sharedinbox(user)
182 length(to) + length(cc) > 1 ->
183 maybe_use_sharedinbox(user)
191 Publishes an activity with BCC to all relevant peers.
194 def publish(%User{} = actor, %{data: %{"bcc" => bcc}} = activity)
195 when is_list(bcc) and bcc != [] do
196 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
198 recipients = recipients(actor, activity)
202 |> Enum.filter(&User.ap_enabled?/1)
203 |> Enum.map(fn actor -> actor.inbox end)
204 |> Enum.filter(fn inbox -> should_federate?(inbox) end)
205 |> Instances.filter_reachable()
208 Enum.each(inboxes, fn {inbox, unreachable_since} ->
209 %User{ap_id: ap_id} = Enum.find(recipients, fn actor -> actor.inbox == inbox end)
211 # Get all the recipients on the same host and add them to cc. Otherwise, a remote
212 # instance would only accept a first message for the first recipient and ignore the rest.
213 cc = get_cc_ap_ids(ap_id, recipients)
220 Pleroma.Web.Federator.Publisher.enqueue_one(__MODULE__, %{
224 id: activity.data["id"],
225 unreachable_since: unreachable_since
231 # Publishes an activity to all relevant peers.
232 def publish(%User{} = actor, %Activity{} = activity) do
233 public = is_public?(activity)
235 if public && Config.get([:instance, :allow_relay]) do
236 Logger.debug(fn -> "Relaying #{activity.data["id"]} out" end)
237 Relay.publish(activity)
240 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
241 json = Jason.encode!(data)
243 recipients(actor, activity)
244 |> Enum.filter(fn user -> User.ap_enabled?(user) end)
245 |> Enum.map(fn %User{} = user ->
246 determine_inbox(activity, user)
249 |> Enum.filter(fn inbox -> should_federate?(inbox) end)
250 |> Instances.filter_reachable()
251 |> Enum.each(fn {inbox, unreachable_since} ->
252 Pleroma.Web.Federator.Publisher.enqueue_one(
258 id: activity.data["id"],
259 unreachable_since: unreachable_since
265 def gather_webfinger_links(%User{} = user) do
267 %{"rel" => "self", "type" => "application/activity+json", "href" => user.ap_id},
270 "type" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"",
274 "rel" => "http://ostatus.org/schema/1.0/subscribe",
275 "template" => "#{Pleroma.Web.Endpoint.url()}/ostatus_subscribe?acct={uri}"
280 def gather_nodeinfo_protocol_names, do: ["activitypub"]