1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Object.Fetcher do
9 alias Pleroma.Object.Containment
11 alias Pleroma.Signature
12 alias Pleroma.Web.ActivityPub.InternalFetchActor
13 alias Pleroma.Web.ActivityPub.ObjectValidator
14 alias Pleroma.Web.ActivityPub.Transmogrifier
15 alias Pleroma.Web.Federator
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, fetch: 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, fetch: false)} do
96 {:allowed_depth, false} ->
97 {:error, "Max thread distance exceeded."}
100 {:error, "Object containment failed."}
102 {:transmogrifier, {:error, {:reject, e}}} ->
105 {:transmogrifier, {:reject, e}} ->
108 {:transmogrifier, _} = e ->
111 {:object, data, nil} ->
112 reinject_object(%Object{}, data)
114 {:normalize, object = %Object{}} ->
117 {:fetch_object, %Object{} = object} ->
120 {:fetch, {:error, error}} ->
128 defp prepare_activity_params(data) do
131 # Should we seriously keep this attributedTo thing?
132 "actor" => data["actor"] || data["attributedTo"],
135 |> Maps.put_if_present("to", data["to"])
136 |> Maps.put_if_present("cc", data["cc"])
137 |> Maps.put_if_present("bto", data["bto"])
138 |> Maps.put_if_present("bcc", data["bcc"])
141 def fetch_object_from_id!(id, options \\ []) do
142 with {:ok, object} <- fetch_object_from_id(id, options) do
145 {:error, %Tesla.Mock.Error{}} ->
148 {:error, "Object has been deleted"} ->
152 Logger.info("Rejected #{id} while fetching: #{inspect(reason)}")
156 Logger.error("Error while fetching #{id}: #{inspect(e)}")
161 defp make_signature(id, date) do
165 InternalFetchActor.get_actor()
167 "(request-target)": "get #{uri.path}",
172 {"signature", signature}
175 defp sign_fetch(headers, id, date) do
176 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
177 [make_signature(id, date) | headers]
183 defp maybe_date_fetch(headers, date) do
184 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
185 [{"date", date} | headers]
191 def fetch_and_contain_remote_object_from_id(id)
193 def fetch_and_contain_remote_object_from_id(%{"id" => id}),
194 do: fetch_and_contain_remote_object_from_id(id)
196 def fetch_and_contain_remote_object_from_id(id) when is_binary(id) do
197 Logger.debug("Fetching object #{id} via AP")
199 with {:scheme, true} <- {:scheme, String.starts_with?(id, "http")},
200 {:ok, body} <- get_object(id),
201 {:ok, data} <- safe_json_decode(body),
202 :ok <- Containment.contain_origin_from_id(id, data) do
206 {:error, "Unsupported URI scheme"}
216 def fetch_and_contain_remote_object_from_id(_id),
217 do: {:error, "id must be a string"}
219 defp get_object(id) do
220 date = Pleroma.Signature.signed_date()
223 [{"accept", "application/activity+json"}]
224 |> maybe_date_fetch(date)
225 |> sign_fetch(id, date)
227 case HTTP.get(id, headers) do
228 {:ok, %{body: body, status: code, headers: headers}} when code in 200..299 ->
229 case List.keyfind(headers, "content-type", 0) do
231 case Plug.Conn.Utils.media_type(content_type) do
232 {:ok, "application", "activity+json", _} ->
235 {:ok, "application", "ld+json",
236 %{"profile" => "https://www.w3.org/ns/activitystreams"}} ->
240 {:error, {:content_type, content_type}}
244 {:error, {:content_type, nil}}
247 {:ok, %{status: code}} when code in [404, 410] ->
248 {:error, "Object has been deleted"}
258 defp safe_json_decode(nil), do: {:ok, nil}
259 defp safe_json_decode(json), do: Jason.decode(json)