1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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
15 require Pleroma.Constants
17 defp touch_changeset(changeset) do
19 NaiveDateTime.utc_now()
20 |> NaiveDateTime.truncate(:second)
22 Ecto.Changeset.put_change(changeset, :updated_at, updated_at)
25 defp maybe_reinject_internal_fields(data, %{data: %{} = old_data}) do
26 internal_fields = Map.take(old_data, Pleroma.Constants.object_internal_fields())
28 Map.merge(data, internal_fields)
31 defp maybe_reinject_internal_fields(data, _), do: data
33 @spec reinject_object(struct(), map()) :: {:ok, Object.t()} | {:error, any()}
34 defp reinject_object(struct, data) do
35 Logger.debug("Reinjecting object #{data["id"]}")
37 with data <- Transmogrifier.fix_object(data),
38 data <- maybe_reinject_internal_fields(data, struct),
39 changeset <- Object.change(struct, %{data: data}),
40 changeset <- touch_changeset(changeset),
41 {:ok, object} <- Repo.insert_or_update(changeset),
42 {:ok, object} <- Object.set_cache(object) do
46 Logger.error("Error while processing object: #{inspect(e)}")
51 def refetch_object(%Object{data: %{"id" => id}} = object) do
52 with {:local, false} <- {:local, Object.local?(object)},
53 {:ok, data} <- fetch_and_contain_remote_object_from_id(id),
54 {:ok, object} <- reinject_object(object, data) do
57 {:local, true} -> {:ok, object}
63 # This will create a Create activity, which we need internally at the moment.
64 def fetch_object_from_id(id, options \\ []) do
65 with {:fetch_object, nil} <- {:fetch_object, Object.get_cached_by_ap_id(id)},
66 {:fetch, {:ok, data}} <- {:fetch, fetch_and_contain_remote_object_from_id(id)},
67 {:normalize, nil} <- {:normalize, Object.normalize(data, false)},
68 params <- prepare_activity_params(data),
69 {:containment, :ok} <- {:containment, Containment.contain_origin(id, params)},
70 {:transmogrifier, {:ok, activity}} <-
71 {:transmogrifier, Transmogrifier.handle_incoming(params, options)},
72 {:object, _data, %Object{} = object} <-
73 {:object, data, Object.normalize(activity, false)} do
77 {:error, "Object containment failed."}
79 {:transmogrifier, {:error, {:reject, nil}}} ->
82 {:transmogrifier, _} ->
83 {:error, "Transmogrifier failure."}
85 {:object, data, nil} ->
86 reinject_object(%Object{}, data)
88 {:normalize, object = %Object{}} ->
91 {:fetch_object, %Object{} = object} ->
94 {:fetch, {:error, error}} ->
102 defp prepare_activity_params(data) do
107 # Should we seriously keep this attributedTo thing?
108 "actor" => data["actor"] || data["attributedTo"],
113 def fetch_object_from_id!(id, options \\ []) do
114 with {:ok, object} <- fetch_object_from_id(id, options) do
117 {:error, %Tesla.Mock.Error{}} ->
120 {:error, "Object has been deleted"} ->
124 Logger.error("Error while fetching #{id}: #{inspect(e)}")
129 defp make_signature(id, date) do
133 InternalFetchActor.get_actor()
135 "(request-target)": "get #{uri.path}",
140 [{:Signature, signature}]
143 defp sign_fetch(headers, id, date) do
144 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
145 headers ++ make_signature(id, date)
151 defp maybe_date_fetch(headers, date) do
152 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
153 headers ++ [{:Date, date}]
159 def fetch_and_contain_remote_object_from_id(id) when is_binary(id) do
160 Logger.debug("Fetching object #{id} via AP")
162 date = Pleroma.Signature.signed_date()
165 [{:Accept, "application/activity+json"}]
166 |> maybe_date_fetch(date)
167 |> sign_fetch(id, date)
169 Logger.debug("Fetch headers: #{inspect(headers)}")
171 with {:scheme, true} <- {:scheme, String.starts_with?(id, "http")},
172 {:ok, %{body: body, status: code}} when code in 200..299 <- HTTP.get(id, headers),
173 {:ok, data} <- Jason.decode(body),
174 :ok <- Containment.contain_origin_from_id(id, data) do
177 {:ok, %{status: code}} when code in [404, 410] ->
178 {:error, "Object has been deleted"}
181 {:error, "Unsupported URI scheme"}
191 def fetch_and_contain_remote_object_from_id(%{"id" => id}),
192 do: fetch_and_contain_remote_object_from_id(id)
194 def fetch_and_contain_remote_object_from_id(_id), do: {:error, "id must be a string"}