Merge branch 'develop' into test/activity_pub/transmogrifier.ex
authorMaksim Pechnikov <parallel588@gmail.com>
Thu, 19 Sep 2019 04:35:34 +0000 (07:35 +0300)
committerMaksim Pechnikov <parallel588@gmail.com>
Thu, 19 Sep 2019 04:35:34 +0000 (07:35 +0300)
1  2 
lib/pleroma/object/fetcher.ex
lib/pleroma/web/activity_pub/transmogrifier.ex
lib/pleroma/web/activity_pub/views/user_view.ex
test/web/activity_pub/transmogrifier_test.exs
test/web/activity_pub/views/user_view_test.exs

index 2217d1eb302546ad937e3b6994746063cc80e9d0,cea33b5af26e9ef3a56b12f688ca63a6de0c5a52..5e064fd8735a9ea9dbb48f0e13d872204db1b2b4
@@@ -12,9 -13,25 +13,26 @@@ defmodule Pleroma.Object.Fetcher d
    alias Pleroma.Web.OStatus
  
    require Logger
+   require Pleroma.Constants
  
-   @spec reinject_object(map()) :: {:ok, Object.t()} | {:error, any()}
-   defp reinject_object(data) do
+   defp touch_changeset(changeset) do
+     updated_at =
+       NaiveDateTime.utc_now()
+       |> NaiveDateTime.truncate(:second)
+     Ecto.Changeset.put_change(changeset, :updated_at, updated_at)
+   end
+   defp maybe_reinject_internal_fields(data, %{data: %{} = old_data}) do
+     internal_fields = Map.take(old_data, Pleroma.Constants.object_internal_fields())
+     Map.merge(data, internal_fields)
+   end
+   defp maybe_reinject_internal_fields(data, _), do: data
++  @spec reinject_object(struct(), map()) :: {:ok, Object.t()} | {:error, any()}
+   defp reinject_object(struct, data) do
      Logger.debug("Reinjecting object #{data["id"]}")
  
      with data <- Transmogrifier.fix_object(data),
    # TODO:
    # This will create a Create activity, which we need internally at the moment.
    def fetch_object_from_id(id, options \\ []) do
 -    if object = Object.get_cached_by_ap_id(id) do
 +    with {:fetch_object, nil} <- {:fetch_object, Object.get_cached_by_ap_id(id)},
 +         {:fetch, {:ok, data}} <- {:fetch, fetch_and_contain_remote_object_from_id(id)},
 +         {:normalize, nil} <- {:normalize, Object.normalize(data, false)},
 +         params <- prepare_activity_params(data),
 +         {:containment, :ok} <- {:containment, 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
 -      Logger.info("Fetching #{id} via AP")
 -
 -      with {:fetch, {:ok, data}} <- {:fetch, fetch_and_contain_remote_object_from_id(id)},
 -           {:normalize, nil} <- {:normalize, Object.normalize(data, false)},
 -           params <- %{
 -             "type" => "Create",
 -             "to" => data["to"],
 -             "cc" => data["cc"],
 -             # Should we seriously keep this attributedTo thing?
 -             "actor" => data["actor"] || data["attributedTo"],
 -             "object" => data
 -           },
 -           {:containment, :ok} <- {:containment, 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
 -        {:containment, _} ->
 -          {:error, "Object containment failed."}
 +      {:containment, _} ->
 +        {:error, "Object containment failed."}
  
 -        {:error, {:reject, nil}} ->
 -          {:reject, nil}
 +      {:error, {:reject, nil}} ->
 +        {:reject, nil}
  
 -        {:object, data, nil} ->
 -          reinject_object(%Object{}, data)
 +      {:object, data, nil} ->
-         reinject_object(data)
++        reinject_object(%Object{}, data)
  
 -        {:normalize, object = %Object{}} ->
 -          {:ok, object}
 +      {:normalize, object = %Object{}} ->
 +        {:ok, object}
  
 -        _e ->
 -          # Only fallback when receiving a fetch/normalization error with ActivityPub
 -          Logger.info("Couldn't get object via AP, trying out OStatus fetching...")
 +      {:fetch_object, %Object{} = object} ->
 +        {:ok, object}
  
 -          # FIXME: OStatus Object Containment?
 -          case OStatus.fetch_activity_from_url(id) do
 -            {:ok, [activity | _]} -> {:ok, Object.normalize(activity, false)}
 -            e -> e
 -          end
 -      end
 +      _e ->
 +        # 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
  
 +  defp prepare_activity_params(data) do
 +    %{
 +      "type" => "Create",
 +      "to" => data["to"],
 +      "cc" => data["cc"],
 +      # Should we seriously keep this attributedTo thing?
 +      "actor" => data["actor"] || data["attributedTo"],
 +      "object" => data
 +    }
 +  end
 +
    def fetch_object_from_id!(id, options \\ []) do
      with {:ok, object} <- fetch_object_from_id(id, options) do
        object