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
9 alias Pleroma.Signature
10 alias Pleroma.Web.ActivityPub.InternalFetchActor
11 alias Pleroma.Web.ActivityPub.Transmogrifier
12 alias Pleroma.Web.OStatus
16 @spec reinject_object(map()) :: {:ok, Object.t()} | {:error, any()}
17 defp reinject_object(data) do
18 Logger.debug("Reinjecting object #{data["id"]}")
20 with data <- Transmogrifier.fix_object(data),
21 {:ok, object} <- Object.create(data) do
25 Logger.error("Error while processing object: #{inspect(e)}")
31 # This will create a Create activity, which we need internally at the moment.
32 def fetch_object_from_id(id, options \\ []) do
33 with {:fetch_object, nil} <- {:fetch_object, Object.get_cached_by_ap_id(id)},
34 {:fetch, {:ok, data}} <- {:fetch, fetch_and_contain_remote_object_from_id(id)},
35 {:normalize, nil} <- {:normalize, Object.normalize(data, false)},
36 params <- prepare_activity_params(data),
37 {:containment, :ok} <- {:containment, Containment.contain_origin(id, params)},
38 {:ok, activity} <- Transmogrifier.handle_incoming(params, options),
39 {:object, _data, %Object{} = object} <-
40 {:object, data, Object.normalize(activity, false)} do
44 {:error, "Object containment failed."}
46 {:error, {:reject, nil}} ->
49 {:object, data, nil} ->
52 {:normalize, object = %Object{}} ->
55 {:fetch_object, %Object{} = object} ->
59 # Only fallback when receiving a fetch/normalization error with ActivityPub
60 Logger.info("Couldn't get object via AP, trying out OStatus fetching...")
62 # FIXME: OStatus Object Containment?
63 case OStatus.fetch_activity_from_url(id) do
64 {:ok, [activity | _]} -> {:ok, Object.normalize(activity, false)}
72 defp prepare_activity_params(data) do
77 # Should we seriously keep this attributedTo thing?
78 "actor" => data["actor"] || data["attributedTo"],
83 def fetch_object_from_id!(id, options \\ []) do
84 with {:ok, object} <- fetch_object_from_id(id, options) do
92 defp make_signature(id, date) do
96 InternalFetchActor.get_actor()
98 "(request-target)": "get #{uri.path}",
103 [{:Signature, signature}]
106 defp sign_fetch(headers, id, date) do
107 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
108 headers ++ make_signature(id, date)
114 defp maybe_date_fetch(headers, date) do
115 if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
116 headers ++ [{:Date, date}]
122 def fetch_and_contain_remote_object_from_id(id) when is_binary(id) do
123 Logger.info("Fetching object #{id} via AP")
125 date = Pleroma.Signature.signed_date()
128 [{:Accept, "application/activity+json"}]
129 |> maybe_date_fetch(date)
130 |> sign_fetch(id, date)
132 Logger.debug("Fetch headers: #{inspect(headers)}")
134 with true <- String.starts_with?(id, "http"),
135 {:ok, %{body: body, status: code}} when code in 200..299 <- HTTP.get(id, headers),
136 {:ok, data} <- Jason.decode(body),
137 :ok <- Containment.contain_origin_from_id(id, data) do
140 {:ok, %{status: code}} when code in [404, 410] ->
141 {:error, "Object has been deleted"}
148 def fetch_and_contain_remote_object_from_id(%{"id" => id}),
149 do: fetch_and_contain_remote_object_from_id(id)
151 def fetch_and_contain_remote_object_from_id(_id), do: {:error, "id must be a string"}