1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Object.Fetcher do
8 alias Pleroma.Object.Containment
10 alias Pleroma.Signature
11 alias Pleroma.Web.ActivityPub.InternalFetchActor
12 alias Pleroma.Web.ActivityPub.ObjectValidator
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14 alias Pleroma.Web.Federator
15 alias Pleroma.Web.FedSockets
18 require Pleroma.Constants
20 defp touch_changeset(changeset) do
22 NaiveDateTime.utc_now()
23 |> NaiveDateTime.truncate(:second)
25 Ecto.Changeset.put_change(changeset, :updated_at, updated_at)
28 defp maybe_reinject_internal_fields(%{data: %{} = old_data}, new_data) do
29 internal_fields = Map.take(old_data, Pleroma.Constants.object_internal_fields())
31 Map.merge(new_data, internal_fields)
34 defp maybe_reinject_internal_fields(_, new_data), do: new_data
36 @spec reinject_object(struct(), map()) :: {:ok, Object.t()} | {:error, any()}
37 defp reinject_object(%Object{data: %{"type" => "Question"}} = object, new_data) do
38 Logger.debug("Reinjecting object #{new_data["id"]}")
40 with data <- maybe_reinject_internal_fields(object, new_data),
41 {:ok, data, _} <- ObjectValidator.validate(data, %{}),
42 changeset <- Object.change(object, %{data: data}),
43 changeset <- touch_changeset(changeset),
44 {:ok, object} <- Repo.insert_or_update(changeset),
45 {:ok, object} <- Object.set_cache(object) do
49 Logger.error("Error while processing object: #{inspect(e)}")
54 defp reinject_object(%Object{} = object, new_data) do
55 Logger.debug("Reinjecting object #{new_data["id"]}")
57 with new_data <- Transmogrifier.fix_object(new_data),
58 data <- maybe_reinject_internal_fields(object, new_data),
59 changeset <- Object.change(object, %{data: data}),
60 changeset <- touch_changeset(changeset),
61 {:ok, object} <- Repo.insert_or_update(changeset),
62 {:ok, object} <- Object.set_cache(object) do
66 Logger.error("Error while processing object: #{inspect(e)}")
71 def refetch_object(%Object{data: %{"id" => id}} = object) do
72 with {:local, false} <- {:local, Object.local?(object)},
73 {:ok, new_data} <- fetch_and_contain_remote_object_from_id(id),
74 {:ok, object} <- reinject_object(object, new_data) do
77 {:local, true} -> {:ok, object}
82 # Note: will create a Create activity, which we need internally at the moment.
83 def fetch_object_from_id(id, options \\ []) do
84 with {_, nil} <- {:fetch_object, Object.get_cached_by_ap_id(id)},
85 {_, true} <- {:allowed_depth, Federator.allowed_thread_distance?(options[:depth])},
86 {_, {:ok, data}} <- {:fetch, fetch_and_contain_remote_object_from_id(id)},
87 {_, nil} <- {:normalize, Object.normalize(data, false)},
88 params <- prepare_activity_params(data),
89 {_, :ok} <- {:containment, Containment.contain_origin(id, params)},
90 {_, {:ok, activity}} <-
91 {:transmogrifier, Transmogrifier.handle_incoming(params, options)},
92 {_, _data, %Object{} = object} <-
93 {:object, data, Object.normalize(activity, false)} do
96 {:allowed_depth, false} ->
97 {:error, "Max thread distance exceeded."}
100 {:error, "Object containment failed."}
102 {:transmogrifier, {:error, {:reject, e}}} ->
105 {:transmogrifier, _} = e ->
108 {:object, data, nil} ->
109 reinject_object(%Object{}, data)
111 {:normalize, object = %Object{}} ->
114 {:fetch_object, %Object{} = object} ->
117 {:fetch, {:error, error}} ->
125 defp prepare_activity_params(data) do
128 "to" => data["to"] || [],
129 "cc" => data["cc"] || [],
130 # Should we seriously keep this attributedTo thing?
131 "actor" => data["actor"] || data["attributedTo"],
136 def fetch_object_from_id!(id, options \\ []) do
137 with {:ok, object} <- fetch_object_from_id(id, options) do
140 {:error, %Tesla.Mock.Error{}} ->
143 {:error, "Object has been deleted"} ->
147 Logger.info("Rejected #{id} while fetching: #{inspect(reason)}")
151 Logger.error("Error while fetching #{id}: #{inspect(e)}")
156 defp make_signature(id, date) do
160 InternalFetchActor.get_actor()
162 "(request-target)": "get #{uri.path}",
167 {"signature", signature}
170 defp sign_fetch(headers, id, date) do
171 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
172 [make_signature(id, date) | headers]
178 defp maybe_date_fetch(headers, date) do
179 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
180 [{"date", date} | headers]
186 def fetch_and_contain_remote_object_from_id(prm, opts \\ [])
188 def fetch_and_contain_remote_object_from_id(%{"id" => id}, opts),
189 do: fetch_and_contain_remote_object_from_id(id, opts)
191 def fetch_and_contain_remote_object_from_id(id, opts) when is_binary(id) do
192 Logger.debug("Fetching object #{id} via AP")
194 with {:scheme, true} <- {:scheme, String.starts_with?(id, "http")},
195 {:ok, body} <- get_object(id, opts),
196 {:ok, data} <- safe_json_decode(body),
197 :ok <- Containment.contain_origin_from_id(id, data) do
201 {:error, "Unsupported URI scheme"}
211 def fetch_and_contain_remote_object_from_id(_id, _opts),
212 do: {:error, "id must be a string"}
214 defp get_object(id, opts) do
215 with false <- Keyword.get(opts, :force_http, false),
216 {:ok, fedsocket} <- FedSockets.get_or_create_fed_socket(id) do
217 Logger.debug("fetching via fedsocket - #{inspect(id)}")
218 FedSockets.fetch(fedsocket, id)
221 Logger.debug("fetching via http - #{inspect(id)}")
226 defp get_object_http(id) do
227 date = Pleroma.Signature.signed_date()
230 [{"accept", "application/activity+json"}]
231 |> maybe_date_fetch(date)
232 |> sign_fetch(id, date)
234 case HTTP.get(id, headers) do
235 {:ok, %{body: body, status: code}} when code in 200..299 ->
238 {:ok, %{status: code}} when code in [404, 410] ->
239 {:error, "Object has been deleted"}
249 defp safe_json_decode(nil), do: {:ok, nil}
250 defp safe_json_decode(json), do: Jason.decode(json)