[#1505] Fixed `replies` serialization (included objects' ids instead of activities...
authorIvan Tashkinov <ivantashkinov@gmail.com>
Sun, 9 Feb 2020 07:17:21 +0000 (10:17 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Sun, 9 Feb 2020 07:17:21 +0000 (10:17 +0300)
lib/pleroma/activity.ex
lib/pleroma/object.ex
lib/pleroma/web/activity_pub/transmogrifier.ex
test/web/activity_pub/transmogrifier_test.exs
test/web/activity_pub/views/object_view_test.exs

index 10b6d7ebd0e2ac16e6fd75c350385b864aaa0108..72e2256eaf00d73bb5d2d6e3e060d63965979198 100644 (file)
@@ -330,23 +330,4 @@ defmodule Pleroma.Activity do
       _ -> nil
     end
   end
-
-  def replies(activity, opts \\ []) do
-    object = Object.normalize(activity)
-
-    query =
-      Activity
-      |> Queries.by_type("Create")
-      |> Queries.by_object_in_reply_to_id(object.data["id"], skip_preloading: true)
-      |> order_by([activity], asc: activity.id)
-
-    if opts[:self_only] do
-      where(query, [a], a.actor == ^activity.actor)
-    else
-      query
-    end
-  end
-
-  def self_replies(activity, opts \\ []),
-    do: replies(activity, Keyword.put(opts, :self_only, true))
 end
index 52556bf31546a3ff74f02663d6d2a39a6d2261f9..f316f8b36c062018892a343a846747a035541df4 100644 (file)
@@ -301,4 +301,26 @@ defmodule Pleroma.Object do
   def local?(%Object{data: %{"id" => id}}) do
     String.starts_with?(id, Pleroma.Web.base_url() <> "/")
   end
+
+  def replies(object, opts \\ []) do
+    object = Object.normalize(object)
+
+    query =
+      Object
+      |> where(
+        [o],
+        fragment("(?)->>'inReplyTo' = ?", o.data, ^object.data["id"])
+      )
+      |> order_by([o], asc: o.id)
+
+    if opts[:self_only] do
+      actor = object.data["actor"]
+      where(query, [o], fragment("(?)->>'actor' = ?", o.data, ^actor))
+    else
+      query
+    end
+  end
+
+  def self_replies(object, opts \\ []),
+    do: replies(object, Keyword.put(opts, :self_only, true))
 end
index 8266545d1ef0e923efa90defe91f246af9487b65..e89588f29f9e27f9dab8c574edba1a88de24e9a8 100644 (file)
@@ -407,7 +407,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
     with nil <- Activity.get_create_by_object_ap_id(object["id"]),
          {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
       options = Keyword.put(options, :depth, (options[:depth] || 0) + 1)
-      object = fix_object(data["object"], options)
+      object = fix_object(object, options)
 
       params = %{
         to: data["to"],
@@ -913,20 +913,20 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
   Serialized Mastodon-compatible `replies` collection containing _self-replies_.
   Based on Mastodon's ActivityPub::NoteSerializer#replies.
   """
-  def set_replies(obj) do
+  def set_replies(obj_data) do
     limit = Pleroma.Config.get([:activitypub, :note_replies_output_limit], 0)
 
     replies_uris =
       with true <- limit > 0 || nil,
-           %Activity{} = activity <- Activity.get_create_by_object_ap_id(obj["id"]) do
-        activity
-        |> Activity.self_replies()
-        |> select([a], fragment("?->>'id'", a.data))
+           %Object{} = object <- Object.get_cached_by_ap_id(obj_data["id"]) do
+        object
+        |> Object.self_replies()
+        |> select([o], fragment("?->>'id'", o.data))
         |> limit(^limit)
         |> Repo.all()
       end
 
-    set_replies(obj, replies_uris || [])
+    set_replies(obj_data, replies_uris || [])
   end
 
   defp set_replies(obj, replies_uris) when replies_uris in [nil, []] do
@@ -952,7 +952,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
     Map.merge(obj, %{"replies" => replies_collection})
   end
 
-  def replies(%{"replies" => replies = %{}}) do
+  def replies(%{"replies" => replies} = _object) when is_map(replies) do
     replies =
       if is_map(replies["first"]) do
         replies["first"]
index 3720dda2a5de10c28da9dac2b693f1d8f8dc5a3b..d373762ea9a9d61b1cf185ca5422018825ed1710 100644 (file)
@@ -2133,7 +2133,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
         })
 
       object = Object.normalize(activity)
-      replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.data["id"] end)
+      replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end)
 
       assert %{
                "type" => "Collection",
index 6784788cc2eae11fcc3b5752cfe42bb7e395c08d..a9197b0c579f1a591b452367ee59d1ca411035d0 100644 (file)
@@ -48,8 +48,8 @@ defmodule Pleroma.Web.ActivityPub.ObjectViewTest do
       {:ok, self_reply1} =
         CommonAPI.post(user, %{"status" => "self-reply 1", "in_reply_to_status_id" => activity.id})
 
+      replies_uris = [self_reply1.object.data["id"]]
       result = ObjectView.render("object.json", %{object: refresh_record(activity)})
-      replies_uris = [self_reply1.data["id"]]
 
       assert %{
                "type" => "Collection",