Fix hashtags being picked up by rich media parser
authorrinpatch <rinpatch@sdf.org>
Fri, 14 Jun 2019 11:34:42 +0000 (14:34 +0300)
committerrinpatch <rinpatch@sdf.org>
Fri, 14 Jun 2019 11:34:42 +0000 (14:34 +0300)
Closes #989

lib/pleroma/html.ex
test/html_test.exs

index e5e78ee4f50124a1d1c2d0568b5d7c14f6c0fa67..8c226c9446b1f369e03c47bc5fe86a322b264d3c 100644 (file)
@@ -89,7 +89,7 @@ defmodule Pleroma.HTML do
     Cachex.fetch!(:scrubber_cache, key, fn _key ->
       result =
         content
-        |> Floki.filter_out("a.mention")
+        |> Floki.filter_out("a.mention,a.hashtag")
         |> Floki.attribute("a", "href")
         |> Enum.at(0)
 
index 08738276eb02e28d70cd99b527da89ddbf5fc30f..64513980b0bb48fb57ec375646d968cef70a86b0 100644 (file)
@@ -4,8 +4,12 @@
 
 defmodule Pleroma.HTMLTest do
   alias Pleroma.HTML
+  alias Pleroma.Object
+  alias Pleroma.Web.CommonAPI
   use Pleroma.DataCase
 
+  import Pleroma.Factory
+
   @html_sample """
     <b>this is in bold</b>
     <p>this is a paragraph</p>
@@ -160,4 +164,53 @@ defmodule Pleroma.HTMLTest do
                )
     end
   end
+
+  describe "extract_first_external_url" do
+    test "extracts the url" do
+      user = insert(:user)
+
+      {:ok, activity} =
+        CommonAPI.post(user, %{
+          "status" =>
+            "I think I just found the best github repo https://github.com/komeiji-satori/Dress"
+        })
+
+      object = Object.normalize(activity)
+      {:ok, url} = HTML.extract_first_external_url(object, object.data["content"])
+      assert url == "https://github.com/komeiji-satori/Dress"
+    end
+
+    test "skips mentions" do
+      user = insert(:user)
+      other_user = insert(:user)
+
+      {:ok, activity} =
+        CommonAPI.post(user, %{
+          "status" =>
+            "@#{other_user.nickname} install misskey! https://github.com/syuilo/misskey/blob/develop/docs/setup.en.md"
+        })
+
+      object = Object.normalize(activity)
+      {:ok, url} = HTML.extract_first_external_url(object, object.data["content"])
+
+      assert url == "https://github.com/syuilo/misskey/blob/develop/docs/setup.en.md"
+
+      refute url == other_user.ap_id
+    end
+
+    test "skips hashtags" do
+      user = insert(:user)
+
+      {:ok, activity} =
+        CommonAPI.post(user, %{
+          "status" =>
+            "#cofe https://www.pixiv.net/member_illust.php?mode=medium&illust_id=72255140"
+        })
+
+      object = Object.normalize(activity)
+      {:ok, url} = HTML.extract_first_external_url(object, object.data["content"])
+
+      assert url == "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=72255140"
+    end
+  end
 end