User Search: Also find user by uri

This commit is contained in:
lain 2020-09-25 08:46:14 +02:00
parent c788593f7f
commit a8c17ea25a
2 changed files with 43 additions and 7 deletions

View File

@ -5,6 +5,7 @@
defmodule Pleroma.User.Search do defmodule Pleroma.User.Search do
alias Pleroma.Pagination alias Pleroma.Pagination
alias Pleroma.User alias Pleroma.User
alias Pleroma.EctoType.ActivityPub.ObjectValidators.Uri, as: UriType
import Ecto.Query import Ecto.Query
@limit 20 @limit 20
@ -21,15 +22,12 @@ def search(query_string, opts \\ []) do
# If this returns anything, it should bounce to the top # If this returns anything, it should bounce to the top
maybe_resolved = maybe_resolve(resolve, for_user, query_string) maybe_resolved = maybe_resolve(resolve, for_user, query_string)
maybe_ap_id_match = User.get_cached_by_ap_id(query_string)
top_user_ids = top_user_ids =
case {maybe_resolved, maybe_ap_id_match} do []
{{:ok, %User{} = user}, %User{} = other_user} -> [user.id, other_user.id] |> maybe_add_resolved(maybe_resolved)
{{:ok, %User{} = user}, _} -> [user.id] |> maybe_add_ap_id_match(query_string)
{_, %User{} = user} -> [user.id] |> maybe_add_uri_match(query_string)
_ -> []
end
results = results =
query_string query_string
@ -39,6 +37,29 @@ def search(query_string, opts \\ []) do
results results
end 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 defp format_query(query_string) do
# Strip the beginning @ off if there is a query # Strip the beginning @ off if there is a query
query_string = String.trim_leading(query_string, "@") query_string = String.trim_leading(query_string, "@")

View File

@ -36,6 +36,21 @@ test "returns a user with matching ap_id as the first result" do
assert first_user.id == user.id assert first_user.id == user.id
end 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 test "excludes invisible users from results" do
user = insert(:user, %{nickname: "john t1000"}) user = insert(:user, %{nickname: "john t1000"})
insert(:user, %{invisible: true, nickname: "john t800"}) insert(:user, %{invisible: true, nickname: "john t800"})