[#1364] Resolved merge conflicts with `develop`. Refactoring.
authorIvan Tashkinov <ivantashkinov@gmail.com>
Mon, 13 Apr 2020 10:24:31 +0000 (13:24 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Mon, 13 Apr 2020 10:24:31 +0000 (13:24 +0300)
lib/pleroma/following_relationship.ex
lib/pleroma/notification.ex

index 219a64352df614c824c705bf922161af04343f23..3a3082e728037e265bc56a55883f8df7f5e36fe9 100644 (file)
@@ -10,11 +10,12 @@ defmodule Pleroma.FollowingRelationship do
 
   alias Ecto.Changeset
   alias FlakeId.Ecto.CompatType
+  alias Pleroma.FollowingRelationship.State
   alias Pleroma.Repo
   alias Pleroma.User
 
   schema "following_relationships" do
-    field(:state, Pleroma.FollowingRelationship.State, default: :follow_pending)
+    field(:state, State, default: :follow_pending)
 
     belongs_to(:follower, User, type: CompatType)
     belongs_to(:following, User, type: CompatType)
@@ -22,6 +23,11 @@ defmodule Pleroma.FollowingRelationship do
     timestamps()
   end
 
+  @doc "Returns underlying integer code for state atom"
+  def state_int_code(state_atom), do: State.__enum_map__() |> Keyword.fetch!(state_atom)
+
+  def accept_state_code, do: state_int_code(:follow_accept)
+
   def changeset(%__MODULE__{} = following_relationship, attrs) do
     following_relationship
     |> cast(attrs, [:state])
@@ -86,7 +92,7 @@ defmodule Pleroma.FollowingRelationship do
     __MODULE__
     |> join(:inner, [r], u in User, on: r.follower_id == u.id)
     |> where([r], r.following_id == ^user.id)
-    |> where([r], r.state == "accept")
+    |> where([r], r.state == ^:follow_accept)
   end
 
   def followers_ap_ids(%User{} = user, from_ap_ids \\ nil) do
@@ -198,6 +204,30 @@ 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 da05ff2e45e84571108ba75c890d72db6cc2e9d9..b76dd176c0be6c5b9bfe9121f6f79ce694d67b7b 100644 (file)
@@ -88,19 +88,7 @@ defmodule Pleroma.Notification do
 
     query
     |> where([n, a], a.actor not in ^blocked_ap_ids)
-    |> where(
-      [n, a],
-      fragment(
-        # "NOT (actor's domain 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 = 'accept')",
-        a.actor,
-        ^user.domain_blocks,
-        a.actor,
-        ^User.binary_id(user.id)
-      )
-    )
+    |> FollowingRelationship.keep_following_or_not_domain_blocked(user)
   end
 
   defp exclude_notification_muted(query, _, %{@include_muted_option => true}) do