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) do
45 Logger.error("Error while processing object: #{inspect(e)}")
50 def refetch_object(%Object{data: %{"id" => id}} = object) do
51 with {:local, false} <- {:local, String.starts_with?(id, Pleroma.Web.base_url() <> "/")},
52 {:ok, data} <- fetch_and_contain_remote_object_from_id(id),
53 {:ok, object} <- reinject_object(object, data) do
56 {:local, true} -> object
62 # This will create a Create activity, which we need internally at the moment.
63 def fetch_object_from_id(id, options \\ []) do
64 with {:fetch_object, nil} <- {:fetch_object, Object.get_cached_by_ap_id(id)},
65 {:fetch, {:ok, data}} <- {:fetch, fetch_and_contain_remote_object_from_id(id)},
66 {:normalize, nil} <- {:normalize, Object.normalize(data, false)},
67 params <- prepare_activity_params(data),
68 {:containment, :ok} <- {:containment, Containment.contain_origin(id, params)},
69 {:ok, activity} <- Transmogrifier.handle_incoming(params, options),
70 {:object, _data, %Object{} = object} <-
71 {:object, data, Object.normalize(activity, false)} do
75 {:error, "Object containment failed."}
77 {:error, {:reject, nil}} ->
80 {:object, data, nil} ->
81 reinject_object(%Object{}, data)
83 {:normalize, object = %Object{}} ->
86 {:fetch_object, %Object{} = object} ->
94 defp prepare_activity_params(data) do
99 # Should we seriously keep this attributedTo thing?
100 "actor" => data["actor"] || data["attributedTo"],
105 def fetch_object_from_id!(id, options \\ []) do
106 with {:ok, object} <- fetch_object_from_id(id, options) do
114 defp make_signature(id, date) do
118 InternalFetchActor.get_actor()
120 "(request-target)": "get #{uri.path}",
125 [{:Signature, signature}]
128 defp sign_fetch(headers, id, date) do
129 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
130 headers ++ make_signature(id, date)
136 defp maybe_date_fetch(headers, date) do
137 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
138 headers ++ [{:Date, date}]
144 def fetch_and_contain_remote_object_from_id(id) when is_binary(id) do
145 Logger.info("Fetching object #{id} via AP")
147 date = Pleroma.Signature.signed_date()
150 [{:Accept, "application/activity+json"}]
151 |> maybe_date_fetch(date)
152 |> sign_fetch(id, date)
154 Logger.debug("Fetch headers: #{inspect(headers)}")
156 with true <- String.starts_with?(id, "http"),
157 {:ok, %{body: body, status: code}} when code in 200..299 <- HTTP.get(id, headers),
158 {:ok, data} <- Jason.decode(body),
159 :ok <- Containment.contain_origin_from_id(id, data) do
162 {:ok, %{status: code}} when code in [404, 410] ->
163 {:error, "Object has been deleted"}
170 def fetch_and_contain_remote_object_from_id(%{"id" => id}),
171 do: fetch_and_contain_remote_object_from_id(id)
173 def fetch_and_contain_remote_object_from_id(_id), do: {:error, "id must be a string"}