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.Web.HTTPSignatures.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)
97 Publishes an activity to all relevant peers.
99 def publish(%User{} = actor, %Activity{} = activity) do
101 if actor.follower_address in activity.recipients do
102 {:ok, followers} = User.get_followers(actor)
103 followers |> Enum.filter(&(!&1.local))
108 public = is_public?(activity)
110 if public && Config.get([:instance, :allow_relay]) do
111 Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
112 Relay.publish(activity)
115 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
116 json = Jason.encode!(data)
118 (Pleroma.Web.Salmon.remote_users(activity) ++ remote_followers)
119 |> Enum.filter(fn user -> User.ap_enabled?(user) end)
120 |> Enum.map(fn %{info: %{source_data: data}} ->
121 (is_map(data["endpoints"]) && Map.get(data["endpoints"], "sharedInbox")) || data["inbox"]
124 |> Enum.filter(fn inbox -> should_federate?(inbox, public) end)
125 |> Instances.filter_reachable()
126 |> Enum.each(fn {inbox, unreachable_since} ->
127 Pleroma.Web.Federator.Publisher.enqueue_one(
133 id: activity.data["id"],
134 unreachable_since: unreachable_since
140 def gather_webfinger_links(%User{} = user) do
142 %{"rel" => "self", "type" => "application/activity+json", "href" => user.ap_id},
145 "type" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"",