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
8 alias Pleroma.Instances
10 alias Pleroma.Web.ActivityPub.Relay
11 alias Pleroma.Web.ActivityPub.Transmogrifier
13 import Pleroma.Web.ActivityPub.Visibility
15 @behaviour Pleroma.Web.Federator.Publisher
19 @httpoison Application.get_env(:pleroma, :httpoison)
22 ActivityPub outgoing federation module.
26 Determine if an activity can be represented by running it through Transmogrifier.
28 def is_representable?(%Activity{} = activity) do
29 with {:ok, _data} <- Transmogrifier.prepare_outgoing(activity.data) do
38 Publish a single message to a peer. Takes a struct with the following
41 * `inbox`: the inbox to publish to
42 * `json`: the JSON message body representing the ActivityPub message
43 * `actor`: the actor which is signing the message
44 * `id`: the ActivityStreams URI of the message
46 def publish_one(%{inbox: inbox, json: json, actor: %User{} = actor, id: id} = params) do
47 Logger.info("Federating #{id} to #{inbox}")
48 host = URI.parse(inbox).host
50 digest = "SHA-256=" <> (:crypto.hash(:sha256, json) |> Base.encode64())
53 NaiveDateTime.utc_now()
54 |> Timex.format!("{WDshort}, {0D} {Mshort} {YYYY} {h24}:{m}:{s} GMT")
57 Pleroma.Signature.sign(actor, %{
59 "content-length": byte_size(json),
64 with {:ok, %{status: code}} when code in 200..299 <-
70 {"Content-Type", "application/activity+json"},
72 {"signature", signature},
76 if !Map.has_key?(params, :unreachable_since) || params[:unreachable_since],
77 do: Instances.set_reachable(inbox)
81 {_post_result, response} ->
82 unless params[:unreachable_since], do: Instances.set_unreachable(inbox)
87 defp should_federate?(inbox, public) do
91 inbox_info = URI.parse(inbox)
92 !Enum.member?(Pleroma.Config.get([:instance, :quarantined_instances], []), inbox_info.host)
96 defp recipients(actor, activity) do
98 if actor.follower_address in activity.recipients do
99 {:ok, followers} = User.get_followers(actor)
100 Enum.filter(followers, &(!&1.local))
105 Pleroma.Web.Salmon.remote_users(actor, activity) ++ followers
108 defp get_cc_ap_ids(ap_id, recipients) do
109 host = Map.get(URI.parse(ap_id), :host)
112 |> Enum.filter(fn %User{ap_id: ap_id} -> Map.get(URI.parse(ap_id), :host) == host end)
113 |> Enum.map(& &1.ap_id)
117 Publishes an activity with BCC to all relevant peers.
120 def publish(actor, %{data: %{"bcc" => bcc}} = activity) when is_list(bcc) and bcc != [] do
121 public = is_public?(activity)
122 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
124 recipients = recipients(actor, activity)
127 |> Enum.filter(&User.ap_enabled?/1)
128 |> Enum.map(fn %{info: %{source_data: data}} -> data["inbox"] end)
129 |> Enum.filter(fn inbox -> should_federate?(inbox, public) end)
130 |> Instances.filter_reachable()
131 |> Enum.each(fn {inbox, unreachable_since} ->
132 %User{ap_id: ap_id} =
133 Enum.find(recipients, fn %{info: %{source_data: data}} -> data["inbox"] == inbox end)
135 cc = get_cc_ap_ids(ap_id, recipients)
140 |> Map.put("directMessage", true)
143 Pleroma.Web.Federator.Publisher.enqueue_one(__MODULE__, %{
147 id: activity.data["id"],
148 unreachable_since: unreachable_since
154 Publishes an activity to all relevant peers.
156 def publish(%User{} = actor, %Activity{} = activity) do
157 public = is_public?(activity)
159 if public && Config.get([:instance, :allow_relay]) do
160 Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
161 Relay.publish(activity)
164 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
165 json = Jason.encode!(data)
167 recipients(actor, activity)
168 |> Enum.filter(fn user -> User.ap_enabled?(user) end)
169 |> Enum.map(fn %{info: %{source_data: data}} ->
170 (is_map(data["endpoints"]) && Map.get(data["endpoints"], "sharedInbox")) || data["inbox"]
173 |> Enum.filter(fn inbox -> should_federate?(inbox, public) end)
174 |> Instances.filter_reachable()
175 |> Enum.each(fn {inbox, unreachable_since} ->
176 Pleroma.Web.Federator.Publisher.enqueue_one(
182 id: activity.data["id"],
183 unreachable_since: unreachable_since
189 def gather_webfinger_links(%User{} = user) do
191 %{"rel" => "self", "type" => "application/activity+json", "href" => user.ap_id},
194 "type" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"",
200 def gather_nodeinfo_protocol_names, do: ["activitypub"]