Merge branch 'update/adminfe-20200331' into 'develop'
[akkoma] / lib / pleroma / object / fetcher.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.Repo
10 alias Pleroma.Signature
11 alias Pleroma.Web.ActivityPub.InternalFetchActor
12 alias Pleroma.Web.ActivityPub.Transmogrifier
13 alias Pleroma.Web.Federator
14
15 require Logger
16 require Pleroma.Constants
17
18 defp touch_changeset(changeset) do
19 updated_at =
20 NaiveDateTime.utc_now()
21 |> NaiveDateTime.truncate(:second)
22
23 Ecto.Changeset.put_change(changeset, :updated_at, updated_at)
24 end
25
26 defp maybe_reinject_internal_fields(data, %{data: %{} = old_data}) do
27 internal_fields = Map.take(old_data, Pleroma.Constants.object_internal_fields())
28
29 Map.merge(data, internal_fields)
30 end
31
32 defp maybe_reinject_internal_fields(data, _), do: data
33
34 @spec reinject_object(struct(), map()) :: {:ok, Object.t()} | {:error, any()}
35 defp reinject_object(struct, data) do
36 Logger.debug("Reinjecting object #{data["id"]}")
37
38 with data <- Transmogrifier.fix_object(data),
39 data <- maybe_reinject_internal_fields(data, struct),
40 changeset <- Object.change(struct, %{data: data}),
41 changeset <- touch_changeset(changeset),
42 {:ok, object} <- Repo.insert_or_update(changeset),
43 {:ok, object} <- Object.set_cache(object) do
44 {:ok, object}
45 else
46 e ->
47 Logger.error("Error while processing object: #{inspect(e)}")
48 {:error, e}
49 end
50 end
51
52 def refetch_object(%Object{data: %{"id" => id}} = object) do
53 with {:local, false} <- {:local, Object.local?(object)},
54 {:ok, data} <- fetch_and_contain_remote_object_from_id(id),
55 {:ok, object} <- reinject_object(object, data) do
56 {:ok, object}
57 else
58 {:local, true} -> {:ok, object}
59 e -> {:error, e}
60 end
61 end
62
63 # Note: will create a Create activity, which we need internally at the moment.
64 def fetch_object_from_id(id, options \\ []) do
65 with {_, nil} <- {:fetch_object, Object.get_cached_by_ap_id(id)},
66 {_, true} <- {:allowed_depth, Federator.allowed_thread_distance?(options[:depth])},
67 {_, {:ok, data}} <- {:fetch, fetch_and_contain_remote_object_from_id(id)},
68 {_, nil} <- {:normalize, Object.normalize(data, false)},
69 params <- prepare_activity_params(data),
70 {_, :ok} <- {:containment, Containment.contain_origin(id, params)},
71 {_, {:ok, activity}} <-
72 {:transmogrifier, Transmogrifier.handle_incoming(params, options)},
73 {_, _data, %Object{} = object} <-
74 {:object, data, Object.normalize(activity, false)} do
75 {:ok, object}
76 else
77 {:allowed_depth, false} ->
78 {:error, "Max thread distance exceeded."}
79
80 {:containment, _} ->
81 {:error, "Object containment failed."}
82
83 {:transmogrifier, {:error, {:reject, nil}}} ->
84 {:reject, nil}
85
86 {:transmogrifier, _} ->
87 {:error, "Transmogrifier failure."}
88
89 {:object, data, nil} ->
90 reinject_object(%Object{}, data)
91
92 {:normalize, object = %Object{}} ->
93 {:ok, object}
94
95 {:fetch_object, %Object{} = object} ->
96 {:ok, object}
97
98 {:fetch, {:error, error}} ->
99 {:error, error}
100
101 e ->
102 e
103 end
104 end
105
106 defp prepare_activity_params(data) do
107 %{
108 "type" => "Create",
109 "to" => data["to"],
110 "cc" => data["cc"],
111 # Should we seriously keep this attributedTo thing?
112 "actor" => data["actor"] || data["attributedTo"],
113 "object" => data
114 }
115 end
116
117 def fetch_object_from_id!(id, options \\ []) do
118 with {:ok, object} <- fetch_object_from_id(id, options) do
119 object
120 else
121 {:error, %Tesla.Mock.Error{}} ->
122 nil
123
124 {:error, "Object has been deleted"} ->
125 nil
126
127 e ->
128 Logger.error("Error while fetching #{id}: #{inspect(e)}")
129 nil
130 end
131 end
132
133 defp make_signature(id, date) do
134 uri = URI.parse(id)
135
136 signature =
137 InternalFetchActor.get_actor()
138 |> Signature.sign(%{
139 "(request-target)": "get #{uri.path}",
140 host: uri.host,
141 date: date
142 })
143
144 [{"signature", signature}]
145 end
146
147 defp sign_fetch(headers, id, date) do
148 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
149 headers ++ make_signature(id, date)
150 else
151 headers
152 end
153 end
154
155 defp maybe_date_fetch(headers, date) do
156 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
157 headers ++ [{"date", date}]
158 else
159 headers
160 end
161 end
162
163 def fetch_and_contain_remote_object_from_id(id) when is_binary(id) do
164 Logger.debug("Fetching object #{id} via AP")
165
166 date = Pleroma.Signature.signed_date()
167
168 headers =
169 [{"accept", "application/activity+json"}]
170 |> maybe_date_fetch(date)
171 |> sign_fetch(id, date)
172
173 Logger.debug("Fetch headers: #{inspect(headers)}")
174
175 with {:scheme, true} <- {:scheme, String.starts_with?(id, "http")},
176 {:ok, %{body: body, status: code}} when code in 200..299 <- HTTP.get(id, headers),
177 {:ok, data} <- Jason.decode(body),
178 :ok <- Containment.contain_origin_from_id(id, data) do
179 {:ok, data}
180 else
181 {:ok, %{status: code}} when code in [404, 410] ->
182 {:error, "Object has been deleted"}
183
184 {:scheme, _} ->
185 {:error, "Unsupported URI scheme"}
186
187 {:error, e} ->
188 {:error, e}
189
190 e ->
191 {:error, e}
192 end
193 end
194
195 def fetch_and_contain_remote_object_from_id(%{"id" => id}),
196 do: fetch_and_contain_remote_object_from_id(id)
197
198 def fetch_and_contain_remote_object_from_id(_id), do: {:error, "id must be a string"}
199 end