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
13 alias Pleroma.Web.OStatus
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) 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, String.starts_with?(id, Pleroma.Web.base_url() <> "/")},
53 {:ok, data} <- fetch_and_contain_remote_object_from_id(id),
54 {:ok, object} <- reinject_object(object, data) do
57 {:local, true} -> 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 {:ok, activity} <- Transmogrifier.handle_incoming(params, options),
71 {:object, _data, %Object{} = object} <-
72 {:object, data, Object.normalize(activity, false)} do
76 {:error, "Object containment failed."}
78 {:error, {:reject, nil}} ->
81 {:object, data, nil} ->
82 reinject_object(%Object{}, data)
84 {:normalize, object = %Object{}} ->
87 {:fetch_object, %Object{} = object} ->
91 # Only fallback when receiving a fetch/normalization error with ActivityPub
92 Logger.info("Couldn't get object via AP, trying out OStatus fetching...")
94 # FIXME: OStatus Object Containment?
95 case OStatus.fetch_activity_from_url(id) do
96 {:ok, [activity | _]} -> {:ok, Object.normalize(activity, false)}
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
122 defp make_signature(id, date) do
126 InternalFetchActor.get_actor()
128 "(request-target)": "get #{uri.path}",
133 [{:Signature, signature}]
136 defp sign_fetch(headers, id, date) do
137 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
138 headers ++ make_signature(id, date)
144 defp maybe_date_fetch(headers, date) do
145 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
146 headers ++ [{:Date, date}]
152 def fetch_and_contain_remote_object_from_id(id) when is_binary(id) do
153 Logger.info("Fetching object #{id} via AP")
155 date = Pleroma.Signature.signed_date()
158 [{:Accept, "application/activity+json"}]
159 |> maybe_date_fetch(date)
160 |> sign_fetch(id, date)
162 Logger.debug("Fetch headers: #{inspect(headers)}")
164 with true <- String.starts_with?(id, "http"),
165 {:ok, %{body: body, status: code}} when code in 200..299 <- HTTP.get(id, headers),
166 {:ok, data} <- Jason.decode(body),
167 :ok <- Containment.contain_origin_from_id(id, data) do
170 {:ok, %{status: code}} when code in [404, 410] ->
171 {:error, "Object has been deleted"}
178 def fetch_and_contain_remote_object_from_id(%{"id" => id}),
179 do: fetch_and_contain_remote_object_from_id(id)
181 def fetch_and_contain_remote_object_from_id(_id), do: {:error, "id must be a string"}