Merge branch '1794-hashtags-construction-from-uri' into 'develop'

[#1794] Hashtags search fix for URI as query string

Closes #1794

See merge request pleroma/pleroma!2641
This commit is contained in:
feld 2020-06-11 14:04:51 +00:00
commit 5d6ec6e5fc
2 changed files with 23 additions and 0 deletions

View File

@ -124,6 +124,7 @@ defp resource_search(:v1, "hashtags", query, _options) do
defp prepare_tags(query, add_joined_tag \\ true) do defp prepare_tags(query, add_joined_tag \\ true) do
tags = tags =
query query
|> preprocess_uri_query()
|> String.split(~r/[^#\w]+/u, trim: true) |> String.split(~r/[^#\w]+/u, trim: true)
|> Enum.uniq_by(&String.downcase/1) |> Enum.uniq_by(&String.downcase/1)
@ -147,6 +148,19 @@ defp prepare_tags(query, add_joined_tag \\ true) do
end end
end end
# If `query` is a URI, returns last component of its path, otherwise returns `query`
defp preprocess_uri_query(query) do
if query =~ ~r/https?:\/\// do
query
|> URI.parse()
|> Map.get(:path)
|> String.split("/")
|> Enum.at(-1)
else
query
end
end
defp joined_tag(tags) do defp joined_tag(tags) do
tags tags
|> Enum.map(fn tag -> String.capitalize(tag) end) |> Enum.map(fn tag -> String.capitalize(tag) end)

View File

@ -111,6 +111,15 @@ test "constructs hashtags from search query", %{conn: conn} do
%{"name" => "prone", "url" => "#{Web.base_url()}/tag/prone"}, %{"name" => "prone", "url" => "#{Web.base_url()}/tag/prone"},
%{"name" => "AccidentProne", "url" => "#{Web.base_url()}/tag/AccidentProne"} %{"name" => "AccidentProne", "url" => "#{Web.base_url()}/tag/AccidentProne"}
] ]
results =
conn
|> get("/api/v2/search?#{URI.encode_query(%{q: "https://shpposter.club/users/shpuld"})}")
|> json_response_and_validate_schema(200)
assert results["hashtags"] == [
%{"name" => "shpuld", "url" => "#{Web.base_url()}/tag/shpuld"}
]
end end
test "excludes a blocked users from search results", %{conn: conn} do test "excludes a blocked users from search results", %{conn: conn} do