[#878] Merge remote-tracking branch 'remotes/upstream/develop' into 878-activity...
authorIvan Tashkinov <ivant.business@gmail.com>
Tue, 9 Jul 2019 18:46:16 +0000 (21:46 +0300)
committerIvan Tashkinov <ivant.business@gmail.com>
Tue, 9 Jul 2019 18:46:16 +0000 (21:46 +0300)
# Conflicts:
# lib/pleroma/object.ex
# test/web/activity_pub/transmogrifier_test.exs
# test/web/ostatus/ostatus_test.exs

1  2 
lib/pleroma/object.ex
test/web/activity_pub/activity_pub_controller_test.exs
test/web/activity_pub/transmogrifier_test.exs
test/web/mastodon_api/status_view_test.exs
test/web/ostatus/ostatus_controller_test.exs
test/web/ostatus/ostatus_test.exs
test/web/twitter_api/twitter_api_controller_test.exs

index a4dbc39479a9b8392ff98abb53e24b78a3a4649d,b8647dd26ed9f8aa9bc9a2f661e8eee34a28525c..c8d339c190ba048bc2248f29dfac1807c634cbd9
@@@ -44,34 -44,36 +44,34 @@@ defmodule Pleroma.Object d
      Repo.one(from(object in Object, where: fragment("(?)->>'id' = ?", object.data, ^ap_id)))
    end
  
-   def normalize(_, fetch_remote \\ true)
 +  defp warn_on_no_object_preloaded(ap_id) do
 +    "Object.normalize() called without preloaded object (#{ap_id}). Consider preloading the object"
 +    |> Logger.debug()
 +
 +    Logger.debug("Backtrace: #{inspect(Process.info(:erlang.self(), :current_stacktrace))}")
 +  end
 +
+   def normalize(_, fetch_remote \\ true, options \\ [])
 +
    # If we pass an Activity to Object.normalize(), we can try to use the preloaded object.
    # Use this whenever possible, especially when walking graphs in an O(N) loop!
-   def normalize(%Object{} = object, _), do: object
-   def normalize(%Activity{object: %Object{} = object}, _), do: object
+   def normalize(%Object{} = object, _, _), do: object
+   def normalize(%Activity{object: %Object{} = object}, _, _), do: object
  
    # A hack for fake activities
-   def normalize(%Activity{data: %{"object" => %{"fake" => true} = data}}, _) do
+   def normalize(%Activity{data: %{"object" => %{"fake" => true} = data}}, _, _) do
      %Object{id: "pleroma:fake_object_id", data: data}
    end
  
 -  # Catch and log Object.normalize() calls where the Activity's child object is not
 -  # preloaded.
 +  # No preloaded object
-   def normalize(%Activity{data: %{"object" => %{"id" => ap_id}}}, fetch_remote) do
+   def normalize(%Activity{data: %{"object" => %{"id" => ap_id}}}, fetch_remote, _) do
 -    Logger.debug(
 -      "Object.normalize() called without preloaded object (#{ap_id}).  Consider preloading the object!"
 -    )
 -
 -    Logger.debug("Backtrace: #{inspect(Process.info(:erlang.self(), :current_stacktrace))}")
 -
 +    warn_on_no_object_preloaded(ap_id)
      normalize(ap_id, fetch_remote)
    end
  
-   def normalize(%Activity{data: %{"object" => ap_id}}, fetch_remote) do
 +  # No preloaded object
 -    Logger.debug(
 -      "Object.normalize() called without preloaded object (#{ap_id}).  Consider preloading the object!"
 -    )
 -
 -    Logger.debug("Backtrace: #{inspect(Process.info(:erlang.self(), :current_stacktrace))}")
 -
+   def normalize(%Activity{data: %{"object" => ap_id}}, fetch_remote, _) do
 +    warn_on_no_object_preloaded(ap_id)
      normalize(ap_id, fetch_remote)
    end
  
index e0ab7b4c6260450677de30d8de22bfe40c78ed58,a914d3c4c77065462384820c36d7bf01003cb2c4..d152169b80d8ec63483bd95ceddfede97d2925fd
@@@ -46,12 -47,10 +47,9 @@@ defmodule Pleroma.Web.ActivityPub.Trans
          data["object"]
          |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
  
-       data =
-         data
-         |> Map.put("object", object)
+       data = Map.put(data, "object", object)
        {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
-       returned_object = Object.normalize(returned_activity)
 -
 -      returned_object = Object.normalize(returned_activity.data["object"], false)
++      returned_object = Object.normalize(returned_activity, false)
  
        assert activity =
                 Activity.get_create_by_object_ap_id(
        assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
      end
  
 -        returned_object = Object.normalize(returned_activity.data["object"], false)
+     test "it does not fetch replied-to activities beyond max_replies_depth" do
+       data =
+         File.read!("test/fixtures/mastodon-post-activity.json")
+         |> Poison.decode!()
+       object =
+         data["object"]
+         |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
+       data = Map.put(data, "object", object)
+       with_mock Pleroma.Web.Federator,
+         allowed_incoming_reply_depth?: fn _ -> false end do
+         {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
++        returned_object = Object.normalize(returned_activity, false)
+         refute Activity.get_create_by_object_ap_id(
+                  "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
+                )
+         assert returned_object.data["inReplyToAtomUri"] ==
+                  "https://shitposter.club/notice/2827873"
+       end
+     end
      test "it does not crash if the object in inReplyTo can't be fetched" do
        data =
          File.read!("test/fixtures/mastodon-post-activity.json")
index e9ca31bc4d68973a9e754230ab01e17a84764a61,acce330080dc394b05f81fe5785d49105ebaf672..4e8f3a0fc64cf689455c81430671cc413e57a16b
@@@ -268,10 -268,13 +270,13 @@@ defmodule Pleroma.Web.OStatusTest d
      assert favorited_activity.local
    end
  
-   test "handle incoming replies" do
+   test_with_mock "handle incoming replies, fetching replied-to activities if we don't have them",
+                  OStatus,
+                  [:passthrough],
+                  [] do
      incoming = File.read!("test/fixtures/incoming_note_activity_answer.xml")
      {:ok, [activity]} = OStatus.handle_incoming(incoming)
-     object = Object.normalize(activity)
 -    object = Object.normalize(activity.data["object"], false)
++    object = Object.normalize(activity, false)
  
      assert activity.data["type"] == "Create"
      assert object.data["type"] == "Note"
      assert object.data["id"] == "tag:gs.example.org:4040,2017-04-25:noticeId=55:objectType=note"
  
      assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["to"]
 -      object = Object.normalize(activity.data["object"], false)
+     assert called(OStatus.fetch_activity_from_url(object.data["inReplyTo"], :_))
+   end
+   test_with_mock "handle incoming replies, not fetching replied-to activities beyond max_replies_depth",
+                  OStatus,
+                  [:passthrough],
+                  [] do
+     incoming = File.read!("test/fixtures/incoming_note_activity_answer.xml")
+     with_mock Pleroma.Web.Federator,
+       allowed_incoming_reply_depth?: fn _ -> false end do
+       {:ok, [activity]} = OStatus.handle_incoming(incoming)
++      object = Object.normalize(activity, false)
+       refute called(OStatus.fetch_activity_from_url(object.data["inReplyTo"], :_))
+     end
    end
  
    test "handle incoming follows" do