[Web.ActivityPub.ActivityPub]: Fix restrict_tag()
authorHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Fri, 21 Dec 2018 17:24:13 +0000 (18:24 +0100)
committerHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Sat, 26 Jan 2019 03:46:01 +0000 (04:46 +0100)
Thanks to Senko-san <kurisu@iscute.moe> for the help on array-matching

lib/pleroma/web/activity_pub/activity_pub.ex

index e0d020fabb58723dcfb99c33f64ee3ff2d256949..d414ecc46f4a94b00d91be5ba23e5ed92ca7f266 100644 (file)
@@ -426,18 +426,41 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
 
   defp restrict_since(query, _), do: query
 
-  defp restrict_tag(query, %{"tag" => tag, "tag_reject" => tag_reject}) when tag_reject != [] do
+  defp restrict_tag(query, %{"tag" => tag, "tag_reject" => tag_reject})
+       when is_list(tag) and tag_reject != [] do
     from(
       activity in query,
-      where: fragment("? <@ (? #> '{\"object\",\"tag\"}')", ^tag, activity.data),
-      where: fragment("? @> (? #> '{\"object\",\"tag\"}')", ^tag_reject, activity.data)
+      where:
+        fragment(
+          "? && ARRAY(SELECT jsonb_array_elements_text((? #> '{\"object\",\"tag\"}')))",
+          ^tag,
+          activity.data
+        ),
+      where:
+        fragment(
+          "(not ? && ARRAY(SELECT jsonb_array_elements_text((? #> '{\"object\",\"tag\"}'))))",
+          ^tag_reject,
+          activity.data
+        )
+    )
+  end
+
+  defp restrict_tag(query, %{"tag" => tag}) when is_list(tag) do
+    from(
+      activity in query,
+      where:
+        fragment(
+          "? && ARRAY(SELECT jsonb_array_elements_text((? #> '{\"object\",\"tag\"}')))",
+          ^tag,
+          activity.data
+        )
     )
   end
 
-  defp restrict_tag(query, %{"tag" => tag}) do
+  defp restrict_tag(query, %{"tag" => tag}) when is_binary(tag) do
     from(
       activity in query,
-      where: fragment("? && jsonb_array_elements_text((? #> '{\"object\",\"tag\"}'))", ^tag, activity.data)
+      where: fragment("? <@ (? #> '{\"object\",\"tag\"}')", ^tag, activity.data)
     )
   end