[#1794] Fixes URI query handling for hashtags extraction in search.
authorIvan Tashkinov <ivantashkinov@gmail.com>
Thu, 11 Jun 2020 13:05:14 +0000 (16:05 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Thu, 11 Jun 2020 13:05:14 +0000 (16:05 +0300)
lib/pleroma/web/mastodon_api/controllers/search_controller.ex
test/web/mastodon_api/controllers/search_controller_test.exs

index 8840fc19ce790d7c11695035705f11c037687465..46bcf4228d41d74768e873dac53739afd6cba79a 100644 (file)
@@ -124,6 +124,7 @@ defmodule Pleroma.Web.MastodonAPI.SearchController do
   defp prepare_tags(query, add_joined_tag \\ true) do
     tags =
       query
+      |> preprocess_uri_query()
       |> String.split(~r/[^#\w]+/u, trim: true)
       |> Enum.uniq_by(&String.downcase/1)
 
@@ -147,6 +148,19 @@ defmodule Pleroma.Web.MastodonAPI.SearchController do
     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
     tags
     |> Enum.map(fn tag -> String.capitalize(tag) end)
index 84d46895edcb7b436cd57ed4022c0e597975211c..0e025adcafe26e7158d98634cf95f1227c0c520d 100644 (file)
@@ -111,6 +111,15 @@ defmodule Pleroma.Web.MastodonAPI.SearchControllerTest do
                %{"name" => "prone", "url" => "#{Web.base_url()}/tag/prone"},
                %{"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
 
     test "excludes a blocked users from search results", %{conn: conn} do