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.ActivityPub.Publisher do
10 alias Pleroma.Instances
13 alias Pleroma.Web.ActivityPub.Relay
14 alias Pleroma.Web.ActivityPub.Transmogrifier
16 require Pleroma.Constants
18 import Pleroma.Web.ActivityPub.Visibility
20 @behaviour Pleroma.Web.Federator.Publisher
25 ActivityPub outgoing federation module.
29 Determine if an activity can be represented by running it through Transmogrifier.
31 def is_representable?(%Activity{} = activity) do
32 with {:ok, _data} <- Transmogrifier.prepare_outgoing(activity.data) do
41 Publish a single message to a peer. Takes a struct with the following
44 * `inbox`: the inbox to publish to
45 * `json`: the JSON message body representing the ActivityPub message
46 * `actor`: the actor which is signing the message
47 * `id`: the ActivityStreams URI of the message
49 def publish_one(%{inbox: inbox, json: json, actor: %User{} = actor, id: id} = params) do
50 Logger.info("Federating #{id} to #{inbox}")
51 %{host: host, 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}",
61 "content-length": byte_size(json),
66 with {:ok, %{status: code}} when code in 200..299 <-
72 {"Content-Type", "application/activity+json"},
74 {"signature", signature},
78 if !Map.has_key?(params, :unreachable_since) || params[:unreachable_since],
79 do: 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 should_federate?(inbox, public) do
102 %{host: host} = URI.parse(inbox)
104 quarantined_instances =
105 Config.get([:instance, :quarantined_instances], [])
106 |> Pleroma.Web.ActivityPub.MRF.subdomains_regex()
108 !Pleroma.Web.ActivityPub.MRF.subdomain_match?(quarantined_instances, host)
112 @spec recipients(User.t(), Activity.t()) :: list(User.t()) | []
113 defp recipients(actor, activity) do
115 if actor.follower_address in activity.recipients do
116 User.get_external_followers(actor)
122 with %Activity{data: %{"type" => "Delete"}} <- activity,
123 %Object{id: object_id} <- Object.normalize(activity),
124 fetchers <- User.get_delivered_users_by_object_id(object_id),
125 _ <- Delivery.delete_all_by_object_id(object_id) do
132 Pleroma.Web.Salmon.remote_users(actor, activity) ++ followers ++ fetchers
135 defp get_cc_ap_ids(ap_id, recipients) do
136 host = Map.get(URI.parse(ap_id), :host)
139 |> Enum.filter(fn %User{ap_id: ap_id} -> Map.get(URI.parse(ap_id), :host) == host end)
140 |> Enum.map(& &1.ap_id)
143 defp maybe_use_sharedinbox(%User{info: %{source_data: data}}),
144 do: (is_map(data["endpoints"]) && Map.get(data["endpoints"], "sharedInbox")) || data["inbox"]
147 Determine a user inbox to use based on heuristics. These heuristics
148 are based on an approximation of the ``sharedInbox`` rules in the
149 [ActivityPub specification][ap-sharedinbox].
151 Please do not edit this function (or its children) without reading
152 the spec, as editing the code is likely to introduce some breakage
153 without some familiarity.
155 [ap-sharedinbox]: https://www.w3.org/TR/activitypub/#shared-inbox-delivery
158 %Activity{data: activity_data},
159 %User{info: %{source_data: data}} = user
161 to = activity_data["to"] || []
162 cc = activity_data["cc"] || []
163 type = activity_data["type"]
167 maybe_use_sharedinbox(user)
169 Pleroma.Constants.as_public() in to || Pleroma.Constants.as_public() in cc ->
170 maybe_use_sharedinbox(user)
172 length(to) + length(cc) > 1 ->
173 maybe_use_sharedinbox(user)
181 Publishes an activity with BCC to all relevant peers.
184 def publish(%User{} = actor, %{data: %{"bcc" => bcc}} = activity)
185 when is_list(bcc) and bcc != [] do
186 public = is_public?(activity)
187 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
189 recipients = recipients(actor, activity)
192 |> Enum.filter(&User.ap_enabled?/1)
193 |> Enum.map(fn %{info: %{source_data: data}} -> data["inbox"] end)
194 |> Enum.filter(fn inbox -> should_federate?(inbox, public) end)
195 |> Instances.filter_reachable()
196 |> Enum.each(fn {inbox, unreachable_since} ->
197 %User{ap_id: ap_id} =
198 Enum.find(recipients, fn %{info: %{source_data: data}} -> data["inbox"] == inbox end)
200 # Get all the recipients on the same host and add them to cc. Otherwise, a remote
201 # instance would only accept a first message for the first recipient and ignore the rest.
202 cc = get_cc_ap_ids(ap_id, recipients)
209 Pleroma.Web.Federator.Publisher.enqueue_one(__MODULE__, %{
213 id: activity.data["id"],
214 unreachable_since: unreachable_since
220 Publishes an activity to all relevant peers.
222 def publish(%User{} = actor, %Activity{} = activity) do
223 public = is_public?(activity)
225 if public && Config.get([:instance, :allow_relay]) do
226 Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
227 Relay.publish(activity)
230 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
231 json = Jason.encode!(data)
233 recipients(actor, activity)
234 |> Enum.filter(fn user -> User.ap_enabled?(user) end)
235 |> Enum.map(fn %User{} = user ->
236 determine_inbox(activity, user)
239 |> Enum.filter(fn inbox -> should_federate?(inbox, public) end)
240 |> Instances.filter_reachable()
241 |> Enum.each(fn {inbox, unreachable_since} ->
242 Pleroma.Web.Federator.Publisher.enqueue_one(
248 id: activity.data["id"],
249 unreachable_since: unreachable_since
255 def gather_webfinger_links(%User{} = user) do
257 %{"rel" => "self", "type" => "application/activity+json", "href" => user.ap_id},
260 "type" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"",
266 def gather_nodeinfo_protocol_names, do: ["activitypub"]