From 47dc52a75882497d00338d07a24ce978cc0f8300 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Fri, 25 May 2018 05:19:11 +0000 Subject: [PATCH 1/2] activitypub utils: optimize block and follow activity lookup multi-field @> comparison is very expensive, so only use @> for the field where it matters this makes the query take only a few usec to execute verses many msec on a busy instance --- lib/pleroma/web/activity_pub/utils.ex | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 831e13b7e..cb2e1e078 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -240,11 +240,16 @@ def fetch_latest_follow(%User{ap_id: follower_id}, %User{ap_id: followed_id}) do activity in Activity, where: fragment( - "? @> ?", - activity.data, - ^%{type: "Follow", object: followed_id} + "? ->> 'type' = 'Follow'", + activity.data ), where: activity.actor == ^follower_id, + where: + fragment( + "? @> ?", + activity.data, + ^%{object: followed_id} + ), order_by: [desc: :id], limit: 1 ) @@ -365,11 +370,16 @@ def fetch_latest_block(%User{ap_id: blocker_id}, %User{ap_id: blocked_id}) do activity in Activity, where: fragment( - "? @> ?", - activity.data, - ^%{type: "Block", object: blocked_id} + "? ->> 'type' = 'Block'", + activity.data ), where: activity.actor == ^blocker_id, + where: + fragment( + "? @> ?", + activity.data, + ^%{object: blocked_id} + ), order_by: [desc: :id], limit: 1 ) From 5f06a1a3c51fcc2b3782879ed900e13266bfd18e Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Fri, 25 May 2018 03:14:49 +0000 Subject: [PATCH 2/2] testsuite: fix module name for CommonAPI.Test (was duplicated with CommonAPI.UtilsTest) --- test/web/common_api/common_api_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/web/common_api/common_api_test.exs b/test/web/common_api/common_api_test.exs index b597e6e0a..a5da271b3 100644 --- a/test/web/common_api/common_api_test.exs +++ b/test/web/common_api/common_api_test.exs @@ -1,4 +1,4 @@ -defmodule Pleroma.Web.CommonAPI.UtilsTest do +defmodule Pleroma.Web.CommonAPI.Test do use Pleroma.DataCase alias Pleroma.Web.CommonAPI