switch to pleroma/http_signatures library
[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.Signature.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 @doc """
97 Publishes an activity to all relevant peers.
98 """
99 def publish(%User{} = actor, %Activity{} = activity) do
100 remote_followers =
101 if actor.follower_address in activity.recipients do
102 {:ok, followers} = User.get_followers(actor)
103 followers |> Enum.filter(&(!&1.local))
104 else
105 []
106 end
107
108 public = is_public?(activity)
109
110 if public && Config.get([:instance, :allow_relay]) do
111 Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
112 Relay.publish(activity)
113 end
114
115 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
116 json = Jason.encode!(data)
117
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"]
122 end)
123 |> Enum.uniq()
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(
128 __MODULE__,
129 %{
130 inbox: inbox,
131 json: json,
132 actor: actor,
133 id: activity.data["id"],
134 unreachable_since: unreachable_since
135 }
136 )
137 end)
138 end
139
140 def gather_webfinger_links(%User{} = user) do
141 [
142 %{"rel" => "self", "type" => "application/activity+json", "href" => user.ap_id},
143 %{
144 "rel" => "self",
145 "type" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"",
146 "href" => user.ap_id
147 }
148 ]
149 end
150
151 def gather_nodeinfo_protocol_names, do: ["activitypub"]
152 end