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, _} = 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 # Should we seriously keep this attributedTo thing?
129 "actor" => data["actor"] || data["attributedTo"],
132 |> Maps.put_if_present("to", data["to"])
133 |> Maps.put_if_present("cc", data["cc"])
134 |> Maps.put_if_present("bto", data["bto"])
135 |> Maps.put_if_present("bcc", data["bcc"])
138 def fetch_object_from_id!(id, options \\ []) do
139 with {:ok, object} <- fetch_object_from_id(id, options) do
142 {:error, %Tesla.Mock.Error{}} ->
145 {:error, "Object has been deleted"} ->
149 Logger.info("Rejected #{id} while fetching: #{inspect(reason)}")
153 Logger.error("Error while fetching #{id}: #{inspect(e)}")
158 defp make_signature(id, date) do
162 InternalFetchActor.get_actor()
164 "(request-target)": "get #{uri.path}",
169 {"signature", signature}
172 defp sign_fetch(headers, id, date) do
173 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
174 [make_signature(id, date) | headers]
180 defp maybe_date_fetch(headers, date) do
181 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
182 [{"date", date} | headers]
188 def fetch_and_contain_remote_object_from_id(id)
190 def fetch_and_contain_remote_object_from_id(%{"id" => id}),
191 do: fetch_and_contain_remote_object_from_id(id)
193 def fetch_and_contain_remote_object_from_id(id) when is_binary(id) do
194 Logger.debug("Fetching object #{id} via AP")
196 with {:scheme, true} <- {:scheme, String.starts_with?(id, "http")},
197 {:ok, body} <- get_object(id),
198 {:ok, data} <- safe_json_decode(body),
199 :ok <- Containment.contain_origin_from_id(id, data) do
203 {:error, "Unsupported URI scheme"}
213 def fetch_and_contain_remote_object_from_id(_id),
214 do: {:error, "id must be a string"}
216 defp get_object(id) do
217 date = Pleroma.Signature.signed_date()
220 [{"accept", "application/activity+json"}]
221 |> maybe_date_fetch(date)
222 |> sign_fetch(id, date)
224 case HTTP.get(id, headers) do
225 {:ok, %{body: body, status: code, headers: headers}} when code in 200..299 ->
226 case List.keyfind(headers, "content-type", 0) do
228 case Plug.Conn.Utils.media_type(content_type) do
229 {:ok, "application", "activity+json", _} ->
232 {:ok, "application", "ld+json",
233 %{"profile" => "https://www.w3.org/ns/activitystreams"}} ->
237 {:error, {:content_type, content_type}}
241 {:error, {:content_type, nil}}
244 {:ok, %{status: code}} when code in [404, 410] ->
245 {:error, "Object has been deleted"}
255 defp safe_json_decode(nil), do: {:ok, nil}
256 defp safe_json_decode(json), do: Jason.decode(json)