Formatting and add tests for blocks/unblocks
[akkoma] / lib / pleroma / user.ex
index 20767499966cbea34408f43c3a3dad6ad385df8f..6a8129ac84b2572432d80e2cd77d76b27cbdcd6b 100644 (file)
@@ -21,6 +21,7 @@ defmodule Pleroma.User do
     field(:local, :boolean, default: true)
     field(:info, :map, default: %{})
     field(:follower_address, :string)
+    field(:search_distance, :float, virtual: true)
     has_many(:notifications, Notification)
 
     timestamps()
@@ -399,16 +400,24 @@ defmodule Pleroma.User do
       User.get_or_fetch_by_nickname(query)
     end
 
-    q =
+    inner =
       from(
         u in User,
-        where:
-          fragment(
-            "(to_tsvector('english', ?) || to_tsvector('english', ?)) @@ plainto_tsquery('english', ?)",
-            u.nickname,
-            u.name,
-            ^query
-          ),
+        select_merge: %{
+          search_distance:
+            fragment(
+              "? <-> (? || ?)",
+              ^query,
+              u.nickname,
+              u.name
+            )
+        }
+      )
+
+    q =
+      from(
+        s in subquery(inner),
+        order_by: s.search_distance,
         limit: 20
       )