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 {:fetch, {:ok, data}} <- {:fetch, fetch_and_contain_remote_object_from_id(id)},
36 {:normalize, nil} <- {:normalize, Object.normalize(data, false)},
41 # Should we seriously keep this attributedTo thing?
42 "actor" => data["actor"] || data["attributedTo"],
45 {:containment, :ok} <- {:containment, 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
52 {:error, "Object containment failed."}
54 {:error, {:reject, nil}} ->
57 {:object, data, nil} ->
60 {:normalize, object = %Object{}} ->
64 # Only fallback when receiving a fetch/normalization error with ActivityPub
65 Logger.info("Couldn't get object via AP, trying out OStatus fetching...")
67 # FIXME: OStatus Object Containment?
68 case OStatus.fetch_activity_from_url(id) do
69 {:ok, [activity | _]} -> {:ok, Object.normalize(activity, false)}
76 def fetch_object_from_id!(id, options \\ []) do
77 with {:ok, object} <- fetch_object_from_id(id, options) do
85 def fetch_and_contain_remote_object_from_id(id) do
86 Logger.info("Fetching object #{id} via AP")
88 with true <- String.starts_with?(id, "http"),
89 {:ok, %{body: body, status: code}} when code in 200..299 <-
92 [{:Accept, "application/activity+json"}]
94 {:ok, data} <- Jason.decode(body),
95 :ok <- Containment.contain_origin_from_id(id, data) do
98 {:ok, %{status: code}} when code in [404, 410] ->
99 {:error, "Object has been deleted"}