Use ILIKE to search users
authorMaxim Filippov <colixer@gmail.com>
Tue, 19 Mar 2019 22:37:40 +0000 (01:37 +0300)
committerMaxim Filippov <colixer@gmail.com>
Tue, 19 Mar 2019 22:37:40 +0000 (01:37 +0300)
lib/pleroma/user.ex
test/user_test.exs

index 8df276ae0a5843edd84337d3017dc82caa9898b6..bf84eaf7f993a4614febce865dcdcde4eb19b231 100644 (file)
@@ -788,34 +788,26 @@ defmodule Pleroma.User do
 
   @spec search_for_admin(%{
           query: binary(),
-          admin: Pleroma.User.t(),
           local: boolean(),
           page: number(),
           page_size: number()
         }) :: {:ok, [Pleroma.User.t()], number()}
   def search_for_admin(%{
         query: term,
-        admin: admin,
         local: local,
         page: page,
         page_size: page_size
       }) do
-    term = String.trim_leading(term, "@")
+    maybe_local_query = User |> maybe_local_user_query(local)
 
-    local_paginated_query =
-      User
-      |> maybe_local_user_query(local)
+    search_query = from(u in maybe_local_query, where: ilike(u.nickname, ^"%#{term}%"))
+    count = search_query |> Repo.aggregate(:count, :id)
+    results =
+      search_query
       |> paginate(page, page_size)
+      |> Repo.all()
 
-    search_query = fts_search_subquery(term, local_paginated_query)
-
-    count =
-      term
-      |> fts_search_subquery()
-      |> maybe_local_user_query(local)
-      |> Repo.aggregate(:count, :id)
-
-    {:ok, do_search(search_query, admin), count}
+    {:ok, results, count}
   end
 
   def search(query, resolve \\ false, for_user \\ nil) do
index c57eb2c0691dcfe9938a99ec0fd189d74396bfd2..1f54f3e30dae5ccc9e4530e77df2150faaa79433 100644 (file)
@@ -1098,4 +1098,21 @@ defmodule Pleroma.UserTest do
     assert {:ok, user_state3} = User.bookmark(user, id2)
     assert user_state3.bookmarks == [id2]
   end
+
+  describe "search for admin" do
+    test "it ignores case" do
+      insert(:user, nickname: "papercoach")
+      insert(:user, nickname: "CanadaPaperCoach")
+
+      {:ok, _results, count} =
+        User.search_for_admin(%{
+          query: "paper",
+          local: false,
+          page: 1,
+          page_size: 50
+        })
+
+      assert count == 2
+    end
+  end
 end