From: Ivan Tashkinov Date: Tue, 9 Jul 2019 18:46:16 +0000 (+0300) Subject: [#878] Merge remote-tracking branch 'remotes/upstream/develop' into 878-activity... X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=829e99722396c2372770f50d9fc7cfa40c441005;p=akkoma [#878] Merge remote-tracking branch 'remotes/upstream/develop' into 878-activity-object-decoupling-in-tests # Conflicts: # lib/pleroma/object.ex # test/web/activity_pub/transmogrifier_test.exs # test/web/ostatus/ostatus_test.exs --- 829e99722396c2372770f50d9fc7cfa40c441005 diff --cc lib/pleroma/object.ex index a4dbc3947,b8647dd26..c8d339c19 --- a/lib/pleroma/object.ex +++ b/lib/pleroma/object.ex @@@ -44,34 -44,36 +44,34 @@@ defmodule Pleroma.Object d Repo.one(from(object in Object, where: fragment("(?)->>'id' = ?", object.data, ^ap_id))) end + 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) + 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 + # No preloaded object - def normalize(%Activity{data: %{"object" => ap_id}}, fetch_remote) do + def normalize(%Activity{data: %{"object" => 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 diff --cc test/web/activity_pub/transmogrifier_test.exs index e0ab7b4c6,a914d3c4c..d152169b8 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@@ -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( @@@ -61,6 -60,32 +59,32 @@@ assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873" end + 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.data["object"], false) ++ 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") diff --cc test/web/ostatus/ostatus_test.exs index e9ca31bc4,acce33008..4e8f3a0fc --- a/test/web/ostatus/ostatus_test.exs +++ b/test/web/ostatus/ostatus_test.exs @@@ -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" @@@ -284,6 -287,23 +289,23 @@@ 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"] + + 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.data["object"], false) ++ object = Object.normalize(activity, false) + + refute called(OStatus.fetch_activity_from_url(object.data["inReplyTo"], :_)) + end end test "handle incoming follows" do