Optimize conversation fetching.
authorRoger Braun <roger@rogerbraun.net>
Tue, 12 Sep 2017 11:48:48 +0000 (13:48 +0200)
committerRoger Braun <roger@rogerbraun.net>
Tue, 12 Sep 2017 11:48:48 +0000 (13:48 +0200)
lib/pleroma/web/activity_pub/activity_pub.ex
priv/repo/migrations/20170912114248_add_context_index.exs [new file with mode: 0644]

index e3dce9cba03a6da4d15193d94e286fc0ff200995..5acbb859eda689ae2aad298356fe3ad535edd076 100644 (file)
@@ -96,7 +96,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
 
   def fetch_activities_for_context(context) do
     query = from activity in Activity,
-      where: fragment("? @> ?", activity.data, ^%{ type: "Create", context: context }),
+      where: fragment("?->>'type' = ? and ?->>'context' = ?", activity.data, "Create", activity.data, ^context),
       order_by: [desc: :inserted_at]
     Repo.all(query)
   end
diff --git a/priv/repo/migrations/20170912114248_add_context_index.exs b/priv/repo/migrations/20170912114248_add_context_index.exs
new file mode 100644 (file)
index 0000000..83c5851
--- /dev/null
@@ -0,0 +1,8 @@
+defmodule Pleroma.Repo.Migrations.AddContextIndex do
+  use Ecto.Migration
+  @disable_ddl_transaction true
+
+  def change do
+    create index(:activities, ["(data->>'type')", "(data->>'context')"], name: :activities_context_index, concurrently: true)
+  end
+end