Merge branch 'develop' into test/activity_pub/transmogrifier.ex
[akkoma] / lib / pleroma / object / fetcher.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.Object.Fetcher do
6 alias Pleroma.HTTP
7 alias Pleroma.Object
8 alias Pleroma.Object.Containment
9 alias Pleroma.Signature
10 alias Pleroma.Web.ActivityPub.InternalFetchActor
11 alias Pleroma.Web.ActivityPub.Transmogrifier
12 alias Pleroma.Web.OStatus
13
14 require Logger
15
16 @spec reinject_object(map()) :: {:ok, Object.t()} | {:error, any()}
17 defp reinject_object(data) do
18 Logger.debug("Reinjecting object #{data["id"]}")
19
20 with data <- Transmogrifier.fix_object(data),
21 {:ok, object} <- Object.create(data) do
22 {:ok, object}
23 else
24 e ->
25 Logger.error("Error while processing object: #{inspect(e)}")
26 {:error, e}
27 end
28 end
29
30 # TODO:
31 # This will create a Create activity, which we need internally at the moment.
32 def fetch_object_from_id(id, options \\ []) do
33 with {:fetch_object, nil} <- {:fetch_object, Object.get_cached_by_ap_id(id)},
34 {:fetch, {:ok, data}} <- {:fetch, fetch_and_contain_remote_object_from_id(id)},
35 {:normalize, nil} <- {:normalize, Object.normalize(data, false)},
36 params <- prepare_activity_params(data),
37 {:containment, :ok} <- {:containment, Containment.contain_origin(id, params)},
38 {:ok, activity} <- Transmogrifier.handle_incoming(params, options),
39 {:object, _data, %Object{} = object} <-
40 {:object, data, Object.normalize(activity, false)} do
41 {:ok, object}
42 else
43 {:containment, _} ->
44 {:error, "Object containment failed."}
45
46 {:error, {:reject, nil}} ->
47 {:reject, nil}
48
49 {:object, data, nil} ->
50 reinject_object(data)
51
52 {:normalize, object = %Object{}} ->
53 {:ok, object}
54
55 {:fetch_object, %Object{} = object} ->
56 {:ok, object}
57
58 _e ->
59 # Only fallback when receiving a fetch/normalization error with ActivityPub
60 Logger.info("Couldn't get object via AP, trying out OStatus fetching...")
61
62 # FIXME: OStatus Object Containment?
63 case OStatus.fetch_activity_from_url(id) do
64 {:ok, [activity | _]} -> {:ok, Object.normalize(activity, false)}
65 e -> e
66 end
67 end
68
69 # end
70 end
71
72 defp prepare_activity_params(data) do
73 %{
74 "type" => "Create",
75 "to" => data["to"],
76 "cc" => data["cc"],
77 # Should we seriously keep this attributedTo thing?
78 "actor" => data["actor"] || data["attributedTo"],
79 "object" => data
80 }
81 end
82
83 def fetch_object_from_id!(id, options \\ []) do
84 with {:ok, object} <- fetch_object_from_id(id, options) do
85 object
86 else
87 _e ->
88 nil
89 end
90 end
91
92 defp make_signature(id, date) do
93 uri = URI.parse(id)
94
95 signature =
96 InternalFetchActor.get_actor()
97 |> Signature.sign(%{
98 "(request-target)": "get #{uri.path}",
99 host: uri.host,
100 date: date
101 })
102
103 [{:Signature, signature}]
104 end
105
106 defp sign_fetch(headers, id, date) do
107 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
108 headers ++ make_signature(id, date)
109 else
110 headers
111 end
112 end
113
114 defp maybe_date_fetch(headers, date) do
115 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
116 headers ++ [{:Date, date}]
117 else
118 headers
119 end
120 end
121
122 def fetch_and_contain_remote_object_from_id(id) when is_binary(id) do
123 Logger.info("Fetching object #{id} via AP")
124
125 date = Pleroma.Signature.signed_date()
126
127 headers =
128 [{:Accept, "application/activity+json"}]
129 |> maybe_date_fetch(date)
130 |> sign_fetch(id, date)
131
132 Logger.debug("Fetch headers: #{inspect(headers)}")
133
134 with true <- String.starts_with?(id, "http"),
135 {:ok, %{body: body, status: code}} when code in 200..299 <- HTTP.get(id, headers),
136 {:ok, data} <- Jason.decode(body),
137 :ok <- Containment.contain_origin_from_id(id, data) do
138 {:ok, data}
139 else
140 {:ok, %{status: code}} when code in [404, 410] ->
141 {:error, "Object has been deleted"}
142
143 e ->
144 {:error, e}
145 end
146 end
147
148 def fetch_and_contain_remote_object_from_id(%{"id" => id}),
149 do: fetch_and_contain_remote_object_from_id(id)
150
151 def fetch_and_contain_remote_object_from_id(_id), do: {:error, "id must be a string"}
152 end