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
9 alias Pleroma.Instances
11 alias Pleroma.Web.ActivityPub.Relay
12 alias Pleroma.Web.ActivityPub.Transmogrifier
14 import Pleroma.Web.ActivityPub.Visibility
16 @behaviour Pleroma.Web.Federator.Publisher
21 ActivityPub outgoing federation module.
25 Determine if an activity can be represented by running it through Transmogrifier.
27 def is_representable?(%Activity{} = activity) do
28 with {:ok, _data} <- Transmogrifier.prepare_outgoing(activity.data) do
37 Publish a single message to a peer. Takes a struct with the following
40 * `inbox`: the inbox to publish to
41 * `json`: the JSON message body representing the ActivityPub message
42 * `actor`: the actor which is signing the message
43 * `id`: the ActivityStreams URI of the message
45 def publish_one(%{inbox: inbox, json: json, actor: %User{} = actor, id: id} = params) do
46 Logger.info("Federating #{id} to #{inbox}")
47 host = URI.parse(inbox).host
49 digest = "SHA-256=" <> (:crypto.hash(:sha256, json) |> Base.encode64())
52 NaiveDateTime.utc_now()
53 |> Timex.format!("{WDshort}, {0D} {Mshort} {YYYY} {h24}:{m}:{s} GMT")
56 Pleroma.Signature.sign(actor, %{
58 "content-length": byte_size(json),
63 with {:ok, %{status: code}} when code in 200..299 <-
69 {"Content-Type", "application/activity+json"},
71 {"signature", signature},
75 if !Map.has_key?(params, :unreachable_since) || params[:unreachable_since],
76 do: Instances.set_reachable(inbox)
80 {_post_result, response} ->
81 unless params[:unreachable_since], do: Instances.set_unreachable(inbox)
86 defp should_federate?(inbox, public) do
90 %{host: host} = URI.parse(inbox)
92 quarantined_instances =
93 Config.get([:instance, :quarantined_instances], [])
94 |> Pleroma.Web.ActivityPub.MRF.subdomains_regex()
96 !Pleroma.Web.ActivityPub.MRF.subdomain_match?(quarantined_instances, host)
100 @spec recipients(User.t(), Activity.t()) :: list(User.t()) | []
101 defp recipients(actor, activity) do
103 if actor.follower_address in activity.recipients do
104 User.get_external_followers(actor)
109 Pleroma.Web.Salmon.remote_users(actor, activity) ++ followers
112 defp get_cc_ap_ids(ap_id, recipients) do
113 host = Map.get(URI.parse(ap_id), :host)
116 |> Enum.filter(fn %User{ap_id: ap_id} -> Map.get(URI.parse(ap_id), :host) == host end)
117 |> Enum.map(& &1.ap_id)
120 @as_public "https://www.w3.org/ns/activitystreams#Public"
122 defp maybe_use_sharedinbox(%User{info: %{source_data: data}}),
123 do: (is_map(data["endpoints"]) && Map.get(data["endpoints"], "sharedInbox")) || data["inbox"]
126 Determine a user inbox to use based on heuristics. These heuristics
127 are based on an approximation of the ``sharedInbox`` rules in the
128 [ActivityPub specification][ap-sharedinbox].
130 Please do not edit this function (or its children) without reading
131 the spec, as editing the code is likely to introduce some breakage
132 without some familiarity.
134 [ap-sharedinbox]: https://www.w3.org/TR/activitypub/#shared-inbox-delivery
137 %Activity{data: activity_data},
138 %User{info: %{source_data: data}} = user
140 to = activity_data["to"] || []
141 cc = activity_data["cc"] || []
142 type = activity_data["type"]
146 maybe_use_sharedinbox(user)
148 @as_public in to || @as_public in cc ->
149 maybe_use_sharedinbox(user)
151 length(to) + length(cc) > 1 ->
152 maybe_use_sharedinbox(user)
160 Publishes an activity with BCC to all relevant peers.
163 def publish(actor, %{data: %{"bcc" => bcc}} = activity) when is_list(bcc) and bcc != [] do
164 public = is_public?(activity)
165 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
167 recipients = recipients(actor, activity)
170 |> Enum.filter(&User.ap_enabled?/1)
171 |> Enum.map(fn %{info: %{source_data: data}} -> data["inbox"] end)
172 |> Enum.filter(fn inbox -> should_federate?(inbox, public) end)
173 |> Instances.filter_reachable()
174 |> Enum.each(fn {inbox, unreachable_since} ->
175 %User{ap_id: ap_id} =
176 Enum.find(recipients, fn %{info: %{source_data: data}} -> data["inbox"] == inbox end)
178 # Get all the recipients on the same host and add them to cc. Otherwise, a remote
179 # instance would only accept a first message for the first recipient and ignore the rest.
180 cc = get_cc_ap_ids(ap_id, recipients)
187 Pleroma.Web.Federator.Publisher.enqueue_one(__MODULE__, %{
191 id: activity.data["id"],
192 unreachable_since: unreachable_since
198 Publishes an activity to all relevant peers.
200 def publish(%User{} = actor, %Activity{} = activity) do
201 public = is_public?(activity)
203 if public && Config.get([:instance, :allow_relay]) do
204 Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
205 Relay.publish(activity)
208 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
209 json = Jason.encode!(data)
211 recipients(actor, activity)
212 |> Enum.filter(fn user -> User.ap_enabled?(user) end)
213 |> Enum.map(fn %User{} = user ->
214 determine_inbox(activity, user)
217 |> Enum.filter(fn inbox -> should_federate?(inbox, public) end)
218 |> Instances.filter_reachable()
219 |> Enum.each(fn {inbox, unreachable_since} ->
220 Pleroma.Web.Federator.Publisher.enqueue_one(
226 id: activity.data["id"],
227 unreachable_since: unreachable_since
233 def gather_webfinger_links(%User{} = user) do
235 %{"rel" => "self", "type" => "application/activity+json", "href" => user.ap_id},
238 "type" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"",
244 def gather_nodeinfo_protocol_names, do: ["activitypub"]