activity: add get_by_ap_id_with_object()
authorWilliam Pitcock <nenolod@dereferenced.org>
Sat, 23 Mar 2019 02:49:10 +0000 (02:49 +0000)
committerWilliam Pitcock <nenolod@dereferenced.org>
Sat, 23 Mar 2019 03:05:13 +0000 (03:05 +0000)
lib/pleroma/activity.ex

index a762d66ef7a4764120da793f56e7893dba8bc1aa..fcdac3a3f249705ba87ca04003d58ac0e35c91f8 100644 (file)
@@ -78,6 +78,23 @@ defmodule Pleroma.Activity do
     )
   end
 
+  def get_by_ap_id_with_object(ap_id) do
+    Repo.one(
+      from(
+        activity in Activity,
+        where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)),
+        inner_join: o in Object,
+        on:
+          fragment(
+            "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
+            o.data,
+            activity.data
+          ),
+        preload: [object: o]
+      )
+    )
+  end
+
   def get_by_id(id) do
     Repo.get(Activity, id)
   end
@@ -181,8 +198,8 @@ defmodule Pleroma.Activity do
     |> Repo.one()
   end
 
-  def normalize(obj) when is_map(obj), do: Activity.get_by_ap_id(obj["id"])
-  def normalize(ap_id) when is_binary(ap_id), do: Activity.get_by_ap_id(ap_id)
+  def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
+  def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
   def normalize(_), do: nil
 
   def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do