Object.Fetcher: Fallback to OStatus only if AP actually fails
authorHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Sun, 14 Jul 2019 10:13:11 +0000 (12:13 +0200)
committerHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Sun, 14 Jul 2019 15:05:31 +0000 (17:05 +0200)
lib/pleroma/object/fetcher.ex

index 82250ab8de98038703488d3e98fbe4439f392306..14454ce9d4d3dd82d648d39e2346854b022526b6 100644 (file)
@@ -31,42 +31,52 @@ defmodule Pleroma.Object.Fetcher do
       {:ok, object}
     else
       Logger.info("Fetching #{id} via AP")
+      {status, data} = fetch_and_contain_remote_object_from_id(id)
+      object = Object.normalize(data, false)
 
-      with {:ok, data} <- fetch_and_contain_remote_object_from_id(id),
-           nil <- Object.normalize(data, false),
-           params <- %{
-             "type" => "Create",
-             "to" => data["to"],
-             "cc" => data["cc"],
-             # TODO: Should we seriously keep this attributedTo thing?
-             "actor" => data["actor"] || data["attributedTo"],
-             "object" => data
-           },
-           :ok <- Containment.contain_origin(id, params),
-           {:ok, activity} <- Transmogrifier.handle_incoming(params, options),
-           {:object, _data, %Object{} = object} <-
-             {:object, data, Object.normalize(activity, false)} do
-        {:ok, object}
-      else
-        {:error, {:reject, nil}} ->
-          {:reject, nil}
+      if status == :ok and object == nil do
+        with params <- %{
+               "type" => "Create",
+               "to" => data["to"],
+               "cc" => data["cc"],
+               # Should we seriously keep this attributedTo thing?
+               "actor" => data["actor"] || data["attributedTo"],
+               "object" => data
+             },
+             :ok <- Containment.contain_origin(id, params),
+             {:ok, activity} <- Transmogrifier.handle_incoming(params, options),
+             {:object, _data, %Object{} = object} <-
+               {:object, data, Object.normalize(activity, false)} do
+          {:ok, object}
+        else
+          {:error, {:reject, nil}} ->
+            {:reject, nil}
 
-        {:object, data, nil} ->
-          reinject_object(data)
+          {:object, data, nil} ->
+            reinject_object(data)
 
-        object = %Object{} ->
-          {:ok, object}
+          object = %Object{} ->
+            {:ok, object}
 
-        :error ->
-          {:error, "Object containment failed."}
+          :error ->
+            {:error, "Object containment failed."}
 
-        _e ->
+          e ->
+            e
+        end
+      else
+        if status == :ok and object != nil do
+          {:ok, object}
+        else
+          # Only fallback when receiving a fetch/normalization error with ActivityPub
           Logger.info("Couldn't get object via AP, trying out OStatus fetching...")
 
+          # FIXME: OStatus Object Containment?
           case OStatus.fetch_activity_from_url(id) do
             {:ok, [activity | _]} -> {:ok, Object.normalize(activity, false)}
             e -> e
           end
+        end
       end
     end
   end