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.Transmogrifier
13 alias Pleroma.Web.Federator
16 require Pleroma.Constants
18 defp touch_changeset(changeset) do
20 NaiveDateTime.utc_now()
21 |> NaiveDateTime.truncate(:second)
23 Ecto.Changeset.put_change(changeset, :updated_at, updated_at)
26 defp maybe_reinject_internal_fields(data, %{data: %{} = old_data}) do
27 internal_fields = Map.take(old_data, Pleroma.Constants.object_internal_fields())
29 Map.merge(data, internal_fields)
32 defp maybe_reinject_internal_fields(data, _), do: data
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"]}")
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
47 Logger.error("Error while processing object: #{inspect(e)}")
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
58 {:local, true} -> {:ok, object}
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
77 {:allowed_depth, false} ->
78 {:error, "Max thread distance exceeded."}
81 {:error, "Object containment failed."}
83 {:transmogrifier, {:error, {:reject, nil}}} ->
86 {:transmogrifier, _} ->
87 {:error, "Transmogrifier failure."}
89 {:object, data, nil} ->
90 reinject_object(%Object{}, data)
92 {:normalize, object = %Object{}} ->
95 {:fetch_object, %Object{} = object} ->
98 {:fetch, {:error, error}} ->
106 defp prepare_activity_params(data) do
111 # Should we seriously keep this attributedTo thing?
112 "actor" => data["actor"] || data["attributedTo"],
117 def fetch_object_from_id!(id, options \\ []) do
118 with {:ok, object} <- fetch_object_from_id(id, options) do
121 {:error, %Tesla.Mock.Error{}} ->
124 {:error, "Object has been deleted"} ->
128 Logger.error("Error while fetching #{id}: #{inspect(e)}")
133 defp make_signature(id, date) do
137 InternalFetchActor.get_actor()
139 "(request-target)": "get #{uri.path}",
144 [{"signature", signature}]
147 defp sign_fetch(headers, id, date) do
148 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
149 headers ++ make_signature(id, date)
155 defp maybe_date_fetch(headers, date) do
156 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
157 headers ++ [{"date", date}]
163 def fetch_and_contain_remote_object_from_id(id) when is_binary(id) do
164 Logger.debug("Fetching object #{id} via AP")
166 date = Pleroma.Signature.signed_date()
169 [{"accept", "application/activity+json"}]
170 |> maybe_date_fetch(date)
171 |> sign_fetch(id, date)
173 Logger.debug("Fetch headers: #{inspect(headers)}")
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
181 {:ok, %{status: code}} when code in [404, 410] ->
182 {:error, "Object has been deleted"}
185 {:error, "Unsupported URI scheme"}
195 def fetch_and_contain_remote_object_from_id(%{"id" => id}),
196 do: fetch_and_contain_remote_object_from_id(id)
198 def fetch_and_contain_remote_object_from_id(_id), do: {:error, "id must be a string"}