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.Web.ActivityPub.Transmogrifier
10 alias Pleroma.Web.OStatus
14 defp reinject_object(data) do
15 Logger.debug("Reinjecting object #{data["id"]}")
17 with data <- Transmogrifier.fix_object(data),
18 {:ok, object} <- Object.create(data) do
22 Logger.error("Error while processing object: #{inspect(e)}")
28 # This will create a Create activity, which we need internally at the moment.
29 def fetch_object_from_id(id, options \\ []) do
30 if object = Object.get_cached_by_ap_id(id) do
33 Logger.info("Fetching #{id} via AP")
35 with {:ok, data} <- fetch_and_contain_remote_object_from_id(id),
36 nil <- Object.normalize(data, false),
41 # TODO: Should we seriously keep this attributedTo thing?
42 "actor" => data["actor"] || data["attributedTo"],
45 :ok <- Containment.contain_origin(id, params),
46 {:ok, activity} <- Transmogrifier.handle_incoming(params, options),
47 {:object, _data, %Object{} = object} <-
48 {:object, data, Object.normalize(activity, false)} do
51 {:error, {:reject, nil}} ->
54 {:object, data, nil} ->
61 {:error, "Object containment failed."}
64 Logger.info("Couldn't get object via AP, trying out OStatus fetching...")
66 case OStatus.fetch_activity_from_url(id) do
67 {:ok, [activity | _]} -> {:ok, Object.normalize(activity, false)}
74 def fetch_object_from_id!(id, options \\ []) do
75 with {:ok, object} <- fetch_object_from_id(id, options) do
83 def fetch_and_contain_remote_object_from_id(id) do
84 Logger.info("Fetching object #{id} via AP")
86 with true <- String.starts_with?(id, "http"),
87 {:ok, %{body: body, status: code}} when code in 200..299 <-
90 [{:Accept, "application/activity+json"}]
92 {:ok, data} <- Jason.decode(body),
93 :ok <- Containment.contain_origin_from_id(id, data) do
96 {:ok, %{status: code}} when code in [404, 410] ->
97 {:error, "Object has been deleted"}