1 defmodule Pleroma.Object.Fetcher do
3 alias Pleroma.Object.Containment
4 alias Pleroma.Web.ActivityPub.Transmogrifier
5 alias Pleroma.Web.OStatus
9 @httpoison Application.get_env(:pleroma, :httpoison)
12 # This will create a Create activity, which we need internally at the moment.
13 def fetch_object_from_id(id) do
14 if object = Object.get_cached_by_ap_id(id) do
17 Logger.info("Fetching #{id} via AP")
19 with {:ok, data} <- fetch_and_contain_remote_object_from_id(id),
20 nil <- Object.normalize(data, false),
25 "actor" => data["actor"] || data["attributedTo"],
28 :ok <- Containment.contain_origin(id, params),
29 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
30 {:ok, Object.normalize(activity, false)}
32 {:error, {:reject, nil}} ->
39 Logger.info("Couldn't get object via AP, trying out OStatus fetching...")
41 case OStatus.fetch_activity_from_url(id) do
42 {:ok, [activity | _]} -> {:ok, Object.normalize(activity.data["object"], false)}
49 def fetch_object_from_id!(id) do
50 with {:ok, object} <- fetch_object_from_id(id) do
58 def fetch_and_contain_remote_object_from_id(id) do
59 Logger.info("Fetching object #{id} via AP")
61 with true <- String.starts_with?(id, "http"),
62 {:ok, %{body: body, status: code}} when code in 200..299 <-
65 [{:Accept, "application/activity+json"}]
67 {:ok, data} <- Jason.decode(body),
68 :ok <- Containment.contain_origin_from_id(id, data) do