CommonAPI: Linkify chat messages.
authorlain <lain@soykaf.club>
Sat, 30 May 2020 10:30:31 +0000 (12:30 +0200)
committerlain <lain@soykaf.club>
Sat, 30 May 2020 10:30:31 +0000 (12:30 +0200)
lib/pleroma/web/common_api/common_api.ex
test/web/common_api/common_api_test.exs

index 764fa4f4fbfd4c0d21260c4a8c4a8752ed2384cd..173353aa5ac363a646a5d56005cb485d4875cd36 100644 (file)
@@ -50,7 +50,12 @@ defmodule Pleroma.Web.CommonAPI do
   defp format_chat_content(nil), do: nil
 
   defp format_chat_content(content) do
-    content |> Formatter.html_escape("text/plain")
+    {text, _, _} =
+      content
+      |> Formatter.html_escape("text/plain")
+      |> Formatter.linkify()
+
+    text
   end
 
   defp validate_chat_content_length(_, true), do: :ok
index 9e129e5a723ab62187f053f958b27952f1876f46..41c6909de4b625eb90524c80a859aab0027a62c9 100644 (file)
@@ -50,6 +50,29 @@ defmodule Pleroma.Web.CommonAPITest do
       assert activity
     end
 
+    test "it linkifies" do
+      author = insert(:user)
+      recipient = insert(:user)
+
+      other_user = insert(:user)
+
+      {:ok, activity} =
+        CommonAPI.post_chat_message(
+          author,
+          recipient,
+          "https://example.org is the site of @#{other_user.nickname} #2hu"
+        )
+
+      assert other_user.ap_id not in activity.recipients
+
+      object = Object.normalize(activity, false)
+
+      assert object.data["content"] ==
+               "<a href=\"https://example.org\" rel=\"ugc\">https://example.org</a> is the site of <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{
+                 other_user.id
+               }\" href=\"#{other_user.ap_id}\" rel=\"ugc\">@<span>#{other_user.nickname}</span></a></span> <a class=\"hashtag\" data-tag=\"2hu\" href=\"http://localhost:4001/tag/2hu\">#2hu</a>"
+    end
+
     test "it posts a chat message" do
       author = insert(:user)
       recipient = insert(:user)