From 1a9bb4daa0df06ac0f3d06ddacec71fa25f64db5 Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Fri, 21 Dec 2018 18:24:13 +0100 Subject: [PATCH] [Web.ActivityPub.ActivityPub]: Fix restrict_tag() Thanks to Senko-san for the help on array-matching --- lib/pleroma/web/activity_pub/activity_pub.ex | 33 +++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index e0d020fab..d414ecc46 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -426,18 +426,41 @@ defp restrict_since(query, %{"since_id" => since_id}) 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}) do + defp restrict_tag(query, %{"tag" => tag}) when is_list(tag) do from( activity in query, - where: fragment("? && jsonb_array_elements_text((? #> '{\"object\",\"tag\"}'))", ^tag, activity.data) + where: + fragment( + "? && ARRAY(SELECT jsonb_array_elements_text((? #> '{\"object\",\"tag\"}')))", + ^tag, + activity.data + ) + ) + end + + defp restrict_tag(query, %{"tag" => tag}) when is_binary(tag) do + from( + activity in query, + where: fragment("? <@ (? #> '{\"object\",\"tag\"}')", ^tag, activity.data) ) end