Restrict `get_existing_votes` to only get Create activities
[akkoma] / lib / pleroma / web / activity_pub / utils.ex
index 9646bbee9ec421aa97c99486a797fb1cfc011669..faae7e747f4a100755c5e6cf78a1b22cae80e236 100644 (file)
@@ -789,4 +789,22 @@ defmodule Pleroma.Web.ActivityPub.Utils do
         [to, cc, recipients]
     end
   end
+
+  def get_existing_votes(actor, %{data: %{"id" => id}}) do
+    query =
+      from(
+        [activity, object: object] in Activity.with_preloaded_object(Activity),
+        where: fragment("(?)->>'type' = 'Create'", activity.data),
+        where: fragment("(?)->>'actor' = ?", activity.data, ^actor),
+        where:
+          fragment(
+            "(?)->>'inReplyTo' = ?",
+            object.data,
+            ^to_string(id)
+          ),
+        where: fragment("(?)->>'type' = 'Answer'", object.data)
+      )
+
+    Repo.all(query)
+  end
 end