User search: Once again, change uri handling.
authorlain <lain@soykaf.club>
Wed, 30 Sep 2020 12:14:41 +0000 (14:14 +0200)
committerlain <lain@soykaf.club>
Wed, 30 Sep 2020 12:14:41 +0000 (14:14 +0200)
They can indeed be non-unique.

lib/pleroma/user/search.ex
priv/repo/migrations/20200930082320_user_ur_is_index_part_three.exs [new file with mode: 0644]
test/user_search_test.exs

index 03f2c552f2f29b82cf20909d2d830ea284f1dc85..35a828008ceb34e6b6653b34d47df68b65c54d53 100644 (file)
@@ -54,8 +54,9 @@ defmodule Pleroma.User.Search do
 
   defp maybe_add_uri_match(list, query) do
     with {:ok, query} <- UriType.cast(query),
-         %User{} = user <- Pleroma.Repo.get_by(User, uri: query) do
-      [user.id | list]
+         q = from(u in User, where: u.uri == ^query, select: u.id),
+         users = Pleroma.Repo.all(q) do
+      users ++ list
     else
       _ -> list
     end
diff --git a/priv/repo/migrations/20200930082320_user_ur_is_index_part_three.exs b/priv/repo/migrations/20200930082320_user_ur_is_index_part_three.exs
new file mode 100644 (file)
index 0000000..816c652
--- /dev/null
@@ -0,0 +1,8 @@
+defmodule Pleroma.Repo.Migrations.UserURIsIndexPartThree do
+  use Ecto.Migration
+
+  def change do
+    drop_if_exists(unique_index(:users, :uri))
+    create_if_not_exists(index(:users, :uri))
+  end
+end
index cc14e97415547bb1bbffbb58c263473e712dbf89..c4b8050055b04c167ed4e0afeacd65c1f40feb67 100644 (file)
@@ -36,6 +36,12 @@ defmodule Pleroma.UserSearchTest do
       assert first_user.id == user.id
     end
 
+    test "doesn't die if two users have the same uri" do
+      insert(:user, %{uri: "https://gensokyo.2hu/@raymoo"})
+      insert(:user, %{uri: "https://gensokyo.2hu/@raymoo"})
+      assert [_first_user, _second_user] = User.search("https://gensokyo.2hu/@raymoo")
+    end
+
     test "returns a user with matching uri as the first result" do
       user =
         insert(:user, %{