11e54b77d3472eda80f47e1ff7dea4f9d207325c
[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 %{} = _data <- Transmogrifier.prepare_outgoing(activity.data) do
30 true
31 else
32 _e -> false
33 end
34 end
35
36 @doc """
37 Publish a single message to a peer. Takes a struct with the following
38 parameters set:
39
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
44 """
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
48
49 digest = "SHA-256=" <> (:crypto.hash(:sha256, json) |> Base.encode64())
50
51 date =
52 NaiveDateTime.utc_now()
53 |> Timex.format!("{WDshort}, {0D} {Mshort} {YYYY} {h24}:{m}:{s} GMT")
54
55 signature =
56 Pleroma.Web.HTTPSignatures.sign(actor, %{
57 host: host,
58 "content-length": byte_size(json),
59 digest: digest,
60 date: date
61 })
62
63 with {:ok, %{status: code}} when code in 200..299 <-
64 result =
65 @httpoison.post(
66 inbox,
67 json,
68 [
69 {"Content-Type", "application/activity+json"},
70 {"Date", date},
71 {"signature", signature},
72 {"digest", digest}
73 ]
74 ) do
75 if !Map.has_key?(params, :unreachable_since) || params[:unreachable_since],
76 do: Instances.set_reachable(inbox)
77
78 result
79 else
80 {_post_result, response} ->
81 unless params[:unreachable_since], do: Instances.set_unreachable(inbox)
82 {:error, response}
83 end
84 end
85
86 defp should_federate?(inbox, public) do
87 if public do
88 true
89 else
90 inbox_info = URI.parse(inbox)
91 !Enum.member?(Pleroma.Config.get([:instance, :quarantined_instances], []), inbox_info.host)
92 end
93 end
94
95 @doc """
96 Publishes an activity to all relevant peers.
97 """
98 def publish(%User{} = actor, %Activity{} = activity) do
99 remote_followers =
100 if actor.follower_address in activity.recipients do
101 {:ok, followers} = User.get_followers(actor)
102 followers |> Enum.filter(&(!&1.local))
103 else
104 []
105 end
106
107 public = is_public?(activity)
108
109 if public && Config.get([:instance, :allow_relay]) do
110 Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
111 Relay.publish(activity)
112 end
113
114 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
115 json = Jason.encode!(data)
116
117 (Pleroma.Web.Salmon.remote_users(activity) ++ remote_followers)
118 |> Enum.filter(fn user -> User.ap_enabled?(user) end)
119 |> Enum.map(fn %{info: %{source_data: data}} ->
120 (is_map(data["endpoints"]) && Map.get(data["endpoints"], "sharedInbox")) || data["inbox"]
121 end)
122 |> Enum.uniq()
123 |> Enum.filter(fn inbox -> should_federate?(inbox, public) end)
124 |> Instances.filter_reachable()
125 |> Enum.each(fn {inbox, unreachable_since} ->
126 Pleroma.Web.Federator.Publisher.enqueue_one(
127 __MODULE__,
128 %{
129 inbox: inbox,
130 json: json,
131 actor: actor,
132 id: activity.data["id"],
133 unreachable_since: unreachable_since
134 }
135 )
136 end)
137 end
138 end