optimise notifications query
authorFloatingGhost <hannah@coffee-and-dreams.uk>
Tue, 11 Oct 2022 10:40:43 +0000 (11:40 +0100)
committerFloatingGhost <hannah@coffee-and-dreams.uk>
Tue, 11 Oct 2022 10:40:43 +0000 (11:40 +0100)
CHANGELOG.md
lib/pleroma/following_relationship.ex
lib/pleroma/notification.ex
lib/pleroma/web/mastodon_api/mastodon_api.ex
test/pleroma/notification_test.exs

index ece6af0d2883cec5c0fd01f1948980541b513958..8a675a32eafd70eaf2bb58b484e508aaaad7d02a 100644 (file)
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
 
 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 
+## Unreleased
+
+## Changes
+- Follows no longer override domain blocks, a domain block is final
+
 ## 2022.10
 
 ### Added
index b101b9ee7d81e04499e274b2e4a265e2372aed6f..42db9463db6508d6400af3fca8221c58e118a61e 100644 (file)
@@ -240,30 +240,6 @@ defmodule Pleroma.FollowingRelationship do
     end)
   end
 
-  @doc """
-  For a query with joined activity,
-  keeps rows where activity's actor is followed by user -or- is NOT domain-blocked by user.
-  """
-  def keep_following_or_not_domain_blocked(query, user) do
-    where(
-      query,
-      [_, activity],
-      fragment(
-        # "(actor's domain NOT in domain_blocks) OR (actor IS in followed AP IDs)"
-        """
-        NOT (substring(? from '.*://([^/]*)') = ANY(?)) OR
-          ? = ANY(SELECT ap_id FROM users AS u INNER JOIN following_relationships AS fr
-            ON u.id = fr.following_id WHERE fr.follower_id = ? AND fr.state = ?)
-        """,
-        activity.actor,
-        ^user.domain_blocks,
-        activity.actor,
-        ^User.binary_id(user.id),
-        ^accept_state_code()
-      )
-    )
-  end
-
   defp validate_not_self_relationship(%Changeset{} = changeset) do
     changeset
     |> validate_follower_id_following_id_inequality()
index 593448713a59ff6e7aeb1a2f48c75243991ecbce..3995be01f7a2d8d217704c99e32d921108aa8883 100644 (file)
@@ -138,7 +138,24 @@ defmodule Pleroma.Notification do
 
     query
     |> where([n, a], a.actor not in ^blocked_ap_ids)
-    |> FollowingRelationship.keep_following_or_not_domain_blocked(user)
+    |> restrict_domain_blocked(user)
+  end
+
+  defp restrict_domain_blocked(query, user) do
+    where(
+      query,
+      [_, activity],
+      fragment(
+        # "(actor's domain NOT in domain_blocks)"
+        """
+        NOT (
+          substring(? from '.*://([^/]*)') = ANY(?)
+        )
+        """,
+        activity.actor,
+        ^user.domain_blocks
+      )
+    )
   end
 
   defp exclude_blockers(query, user) do
index 23846b36a0a3101784b2e27358b5de14407c26c3..69bc2f0d6e49227b461a3f7f39ad92beee9eba97 100644 (file)
@@ -63,11 +63,16 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPI do
   def get_notifications(user, params \\ %{}) do
     options = cast_params(params)
 
-    user
-    |> Notification.for_user_query(options)
-    |> restrict(:include_types, options)
-    |> restrict(:exclude_types, options)
-    |> restrict(:account_ap_id, options)
+    query =
+      user
+      |> Notification.for_user_query(options)
+      |> restrict(:include_types, options)
+      |> restrict(:exclude_types, options)
+      |> restrict(:account_ap_id, options)
+
+    IO.inspect(Pleroma.Repo.to_sql(:all, query))
+
+    query
     |> Pagination.fetch_paginated(params)
   end
 
index 68330465b9eb007a1024063e28f8e2f65b3e3993..721836a2c1e83ca2ff0d067d6e57ed64187817d9 100644 (file)
@@ -1149,18 +1149,6 @@ defmodule Pleroma.NotificationTest do
       assert Notification.for_user(user) == []
     end
 
-    test "it returns notifications for domain-blocked but followed user" do
-      user = insert(:user)
-      blocked = insert(:user, ap_id: "http://some-domain.com")
-
-      {:ok, user} = User.block_domain(user, "some-domain.com")
-      {:ok, _, _} = User.follow(user, blocked)
-
-      {:ok, _activity} = CommonAPI.post(blocked, %{status: "hey @#{user.nickname}"})
-
-      assert length(Notification.for_user(user)) == 1
-    end
-
     test "it doesn't return notifications for muted thread", %{user: user} do
       another_user = insert(:user)