From a8c17ea25a79491328345f5834397eb6821a77f1 Mon Sep 17 00:00:00 2001 From: lain Date: Fri, 25 Sep 2020 08:46:14 +0200 Subject: [PATCH 1/3] User Search: Also find user by uri --- lib/pleroma/user/search.ex | 35 ++++++++++++++++++++++++++++------- test/user_search_test.exs | 15 +++++++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/lib/pleroma/user/search.ex b/lib/pleroma/user/search.ex index 408295e0c..d747bfa52 100644 --- a/lib/pleroma/user/search.ex +++ b/lib/pleroma/user/search.ex @@ -5,6 +5,7 @@ defmodule Pleroma.User.Search do alias Pleroma.Pagination alias Pleroma.User + alias Pleroma.EctoType.ActivityPub.ObjectValidators.Uri, as: UriType import Ecto.Query @limit 20 @@ -21,15 +22,12 @@ defmodule Pleroma.User.Search do # If this returns anything, it should bounce to the top maybe_resolved = maybe_resolve(resolve, for_user, query_string) - maybe_ap_id_match = User.get_cached_by_ap_id(query_string) top_user_ids = - case {maybe_resolved, maybe_ap_id_match} do - {{:ok, %User{} = user}, %User{} = other_user} -> [user.id, other_user.id] - {{:ok, %User{} = user}, _} -> [user.id] - {_, %User{} = user} -> [user.id] - _ -> [] - end + [] + |> maybe_add_resolved(maybe_resolved) + |> maybe_add_ap_id_match(query_string) + |> maybe_add_uri_match(query_string) results = query_string @@ -39,6 +37,29 @@ defmodule Pleroma.User.Search do results end + defp maybe_add_resolved(list, {:ok, %User{} = user}) do + [user.id | list] + end + + defp maybe_add_resolved(list, _), do: list + + defp maybe_add_ap_id_match(list, query) do + if user = User.get_cached_by_ap_id(query) do + [user.id | list] + else + list + end + end + + 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] + else + _ -> list + end + end + defp format_query(query_string) do # Strip the beginning @ off if there is a query query_string = String.trim_leading(query_string, "@") diff --git a/test/user_search_test.exs b/test/user_search_test.exs index 68fda1c53..cc14e9741 100644 --- a/test/user_search_test.exs +++ b/test/user_search_test.exs @@ -36,6 +36,21 @@ defmodule Pleroma.UserSearchTest do assert first_user.id == user.id end + test "returns a user with matching uri as the first result" do + user = + insert(:user, %{ + nickname: "no_relation", + ap_id: "https://lain.com/users/lain", + uri: "https://lain.com/@lain" + }) + + _user = insert(:user, %{nickname: "com_user"}) + + [first_user, _second_user] = User.search("https://lain.com/@lain") + + assert first_user.id == user.id + end + test "excludes invisible users from results" do user = insert(:user, %{nickname: "john t1000"}) insert(:user, %{invisible: true, nickname: "john t800"}) From 1e0f3f8514a42b088ed68ece5f9e339ad829e242 Mon Sep 17 00:00:00 2001 From: lain Date: Fri, 25 Sep 2020 08:56:58 +0200 Subject: [PATCH 2/3] User search: Make uri matches case insensitive. --- .../20200925065249_make_user_ids_ci.exs | 19 +++++++++++++++++++ test/user_search_test.exs | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 priv/repo/migrations/20200925065249_make_user_ids_ci.exs diff --git a/priv/repo/migrations/20200925065249_make_user_ids_ci.exs b/priv/repo/migrations/20200925065249_make_user_ids_ci.exs new file mode 100644 index 000000000..b7305f137 --- /dev/null +++ b/priv/repo/migrations/20200925065249_make_user_ids_ci.exs @@ -0,0 +1,19 @@ +defmodule Pleroma.Repo.Migrations.MakeUserIdsCI do + use Ecto.Migration + + def up do + alter table(:users) do + modify(:uri, :citext) + end + + create(unique_index(:users, :uri)) + end + + def don do + drop(unique_index(:users, :uri)) + + alter table(:users) do + modify(:uri, :text) + end + end +end diff --git a/test/user_search_test.exs b/test/user_search_test.exs index cc14e9741..b99a77b57 100644 --- a/test/user_search_test.exs +++ b/test/user_search_test.exs @@ -41,7 +41,7 @@ defmodule Pleroma.UserSearchTest do insert(:user, %{ nickname: "no_relation", ap_id: "https://lain.com/users/lain", - uri: "https://lain.com/@lain" + uri: "https://lain.com/@Lain" }) _user = insert(:user, %{nickname: "com_user"}) From 05b5241314182c5aab2907e27d4c5f46d7617f56 Mon Sep 17 00:00:00 2001 From: lain Date: Fri, 25 Sep 2020 09:12:48 +0200 Subject: [PATCH 3/3] Linter fixes --- lib/pleroma/user/search.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/user/search.ex b/lib/pleroma/user/search.ex index d747bfa52..03f2c552f 100644 --- a/lib/pleroma/user/search.ex +++ b/lib/pleroma/user/search.ex @@ -3,9 +3,10 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.User.Search do + alias Pleroma.EctoType.ActivityPub.ObjectValidators.Uri, as: UriType alias Pleroma.Pagination alias Pleroma.User - alias Pleroma.EctoType.ActivityPub.ObjectValidators.Uri, as: UriType + import Ecto.Query @limit 20