Merge remote-tracking branch 'pleroma/develop' into feature/addressable-lists
[akkoma] / lib / pleroma / web / activity_pub / publisher.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.Publisher do
6 alias Pleroma.Activity
7 alias Pleroma.Config
8 alias Pleroma.Instances
9 alias Pleroma.User
10 alias Pleroma.Web.ActivityPub.Relay
11 alias Pleroma.Web.ActivityPub.Transmogrifier
12
13 import Pleroma.Web.ActivityPub.Visibility
14
15 @behaviour Pleroma.Web.Federator.Publisher
16
17 require Logger
18
19 @httpoison Application.get_env(:pleroma, :httpoison)
20
21 @moduledoc """
22 ActivityPub outgoing federation module.
23 """
24
25 @doc """
26 Determine if an activity can be represented by running it through Transmogrifier.
27 """
28 def is_representable?(%Activity{} = activity) do
29 with {:ok, _data} <- Transmogrifier.prepare_outgoing(activity.data) do
30 true
31 else
32 _e ->
33 false
34 end
35 end
36
37 @doc """
38 Publish a single message to a peer. Takes a struct with the following
39 parameters set:
40
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
45 """
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
49
50 digest = "SHA-256=" <> (:crypto.hash(:sha256, json) |> Base.encode64())
51
52 date =
53 NaiveDateTime.utc_now()
54 |> Timex.format!("{WDshort}, {0D} {Mshort} {YYYY} {h24}:{m}:{s} GMT")
55
56 signature =
57 Pleroma.Web.HTTPSignatures.sign(actor, %{
58 host: host,
59 "content-length": byte_size(json),
60 digest: digest,
61 date: date
62 })
63
64 with {:ok, %{status: code}} when code in 200..299 <-
65 result =
66 @httpoison.post(
67 inbox,
68 json,
69 [
70 {"Content-Type", "application/activity+json"},
71 {"Date", date},
72 {"signature", signature},
73 {"digest", digest}
74 ]
75 ) do
76 if !Map.has_key?(params, :unreachable_since) || params[:unreachable_since],
77 do: Instances.set_reachable(inbox)
78
79 result
80 else
81 {_post_result, response} ->
82 unless params[:unreachable_since], do: Instances.set_unreachable(inbox)
83 {:error, response}
84 end
85 end
86
87 defp should_federate?(inbox, public) do
88 if public do
89 true
90 else
91 inbox_info = URI.parse(inbox)
92 !Enum.member?(Pleroma.Config.get([:instance, :quarantined_instances], []), inbox_info.host)
93 end
94 end
95
96 defp recipients(actor, activity) do
97 followers =
98 if actor.follower_address in activity.recipients do
99 {:ok, followers} = User.get_followers(actor)
100 Enum.filter(followers, &(!&1.local))
101 else
102 []
103 end
104
105 Pleroma.Web.Salmon.remote_users(actor, activity) ++ followers
106 end
107
108 defp get_cc_ap_ids(ap_id, recipients) do
109 host = Map.get(URI.parse(ap_id), :host)
110
111 recipients
112 |> Enum.filter(fn %User{ap_id: ap_id} -> Map.get(URI.parse(ap_id), :host) == host end)
113 |> Enum.map(& &1.ap_id)
114 end
115
116 @doc """
117 Publishes an activity with BCC to all relevant peers.
118 """
119
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)
123
124 recipients = recipients(actor, activity)
125
126 recipients
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)
134
135 cc = get_cc_ap_ids(ap_id, recipients)
136
137 json =
138 data
139 |> Map.put("cc", cc)
140 |> Map.put("directMessage", true)
141 |> Jason.encode!()
142
143 Pleroma.Web.Federator.Publisher.enqueue_one(__MODULE__, %{
144 inbox: inbox,
145 json: json,
146 actor: actor,
147 id: activity.data["id"],
148 unreachable_since: unreachable_since
149 })
150 end)
151 end
152
153 @doc """
154 Publishes an activity to all relevant peers.
155 """
156 def publish(%User{} = actor, %Activity{} = activity) do
157 public = is_public?(activity)
158
159 if public && Config.get([:instance, :allow_relay]) do
160 Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
161 Relay.publish(activity)
162 end
163
164 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
165 json = Jason.encode!(data)
166
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"]
171 end)
172 |> Enum.uniq()
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(
177 __MODULE__,
178 %{
179 inbox: inbox,
180 json: json,
181 actor: actor,
182 id: activity.data["id"],
183 unreachable_since: unreachable_since
184 }
185 )
186 end)
187 end
188
189 def gather_webfinger_links(%User{} = user) do
190 [
191 %{"rel" => "self", "type" => "application/activity+json", "href" => user.ap_id},
192 %{
193 "rel" => "self",
194 "type" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"",
195 "href" => user.ap_id
196 }
197 ]
198 end
199
200 def gather_nodeinfo_protocol_names, do: ["activitypub"]
201 end