[#491] Made full nicknames be preserved in user links text only in Bio.
authorIvan Tashkinov <ivantashkinov@gmail.com>
Fri, 18 Jan 2019 06:30:16 +0000 (09:30 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Fri, 18 Jan 2019 06:30:16 +0000 (09:30 +0300)
lib/pleroma/formatter.ex
lib/pleroma/user.ex
lib/pleroma/web/common_api/utils.ex
test/formatter_test.exs
test/user_test.exs
test/web/twitter_api/views/user_view_test.exs

index 024c6e11756bba6b4b46b07b1e4444469dfb1164..37737853ad3e0c6c3ed58fa68a225b270c1175bf 100644 (file)
@@ -120,7 +120,7 @@ defmodule Pleroma.Formatter do
   end
 
   @doc "Adds the links to mentioned users"
-  def add_user_links({subs, text}, mentions) do
+  def add_user_links({subs, text}, mentions, options \\ []) do
     mentions =
       mentions
       |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
@@ -142,11 +142,16 @@ defmodule Pleroma.Formatter do
               ap_id
             end
 
-          full_match = String.trim_leading(match, "@")
+          nickname =
+            if options[:format] == :full do
+              User.full_nickname(match)
+            else
+              User.local_nickname(match)
+            end
 
           {uuid,
            "<span class='h-card'><a data-user='#{id}' class='u-url mention' href='#{ap_id}'>" <>
-             "@<span>#{full_match}</span></a></span>"}
+             "@<span>#{nickname}</span></a></span>"}
         end)
 
     {subs, uuid_text}
index a52e536d3397f1e96e43ae46fb4b31128894e981..2dc3c8d56d06d31c8c3f85dcd70f8571a507cb0f 100644 (file)
@@ -447,8 +447,7 @@ defmodule Pleroma.User do
   def get_by_nickname(nickname) do
     Repo.get_by(User, nickname: nickname) ||
       if Regex.match?(~r(@#{Pleroma.Web.Endpoint.host()})i, nickname) do
-        [local_nickname, _] = String.split(nickname, "@")
-        Repo.get_by(User, nickname: local_nickname)
+        Repo.get_by(User, nickname: local_nickname(nickname))
       end
   end
 
@@ -990,7 +989,7 @@ defmodule Pleroma.User do
       end)
 
     bio
-    |> CommonUtils.format_input(mentions, tags, "text/plain")
+    |> CommonUtils.format_input(mentions, tags, "text/plain", user_links: [format: :full])
     |> Formatter.emojify(emoji)
   end
 
@@ -1041,6 +1040,16 @@ defmodule Pleroma.User do
     end
   end
 
+  def local_nickname(nickname_or_mention) do
+    nickname_or_mention
+    |> full_nickname()
+    |> String.split("@")
+    |> hd()
+  end
+
+  def full_nickname(nickname_or_mention),
+    do: String.trim_leading(nickname_or_mention, "@")
+
   def error_user(ap_id) do
     %User{
       name: ap_id,
index 7e30d224cef348d201ad67d3fb1da2dadaff6cf5..a36ab5c158ae6984680f2843da7aff935b173d3c 100644 (file)
@@ -116,16 +116,18 @@ defmodule Pleroma.Web.CommonAPI.Utils do
     Enum.join([text | attachment_text], "<br>")
   end
 
+  def format_input(text, mentions, tags, format, options \\ [])
+
   @doc """
   Formatting text to plain text.
   """
-  def format_input(text, mentions, tags, "text/plain") do
+  def format_input(text, mentions, tags, "text/plain", options) do
     text
     |> Formatter.html_escape("text/plain")
     |> String.replace(~r/\r?\n/, "<br>")
     |> (&{[], &1}).()
     |> Formatter.add_links()
-    |> Formatter.add_user_links(mentions)
+    |> Formatter.add_user_links(mentions, options[:user_links] || [])
     |> Formatter.add_hashtag_links(tags)
     |> Formatter.finalize()
   end
@@ -133,24 +135,24 @@ defmodule Pleroma.Web.CommonAPI.Utils do
   @doc """
   Formatting text to html.
   """
-  def format_input(text, mentions, _tags, "text/html") do
+  def format_input(text, mentions, _tags, "text/html", options) do
     text
     |> Formatter.html_escape("text/html")
     |> (&{[], &1}).()
-    |> Formatter.add_user_links(mentions)
+    |> Formatter.add_user_links(mentions, options[:user_links] || [])
     |> Formatter.finalize()
   end
 
   @doc """
   Formatting text to markdown.
   """
-  def format_input(text, mentions, tags, "text/markdown") do
+  def format_input(text, mentions, tags, "text/markdown", options) do
     text
     |> Formatter.mentions_escape(mentions)
     |> Earmark.as_html!()
     |> Formatter.html_escape("text/html")
     |> (&{[], &1}).()
-    |> Formatter.add_user_links(mentions)
+    |> Formatter.add_user_links(mentions, options[:user_links] || [])
     |> Formatter.add_hashtag_links(tags)
     |> Formatter.finalize()
   end
index 7040f1c276d312c29bcb5cd379537d2b5ee80b57..2e717194bb38f1302640f48e5761ba54b58b938f 100644 (file)
@@ -124,7 +124,7 @@ defmodule Pleroma.FormatterTest do
   end
 
   describe "add_user_links" do
-    test "gives a replacement for user links" do
+    test "gives a replacement for user links, using local nicknames in user links text" do
       text = "@gsimg According to @archa_eme_, that is @daggsy. Also hello @archaeme@archae.me"
       gsimg = insert(:user, %{nickname: "gsimg"})
 
@@ -150,7 +150,7 @@ defmodule Pleroma.FormatterTest do
           archaeme.id
         }' class='u-url mention' href='#{"https://archeme/@archa_eme_"}'>@<span>archa_eme_</span></a></span>, that is @daggsy. Also hello <span class='h-card'><a data-user='#{
           archaeme_remote.id
-        }' class='u-url mention' href='#{archaeme_remote.ap_id}'>@<span>archaeme@archae.me</span></a></span>"
+        }' class='u-url mention' href='#{archaeme_remote.ap_id}'>@<span>archaeme</span></a></span>"
 
       assert expected_text == Formatter.finalize({subs, text})
     end
@@ -168,7 +168,7 @@ defmodule Pleroma.FormatterTest do
       Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
 
       expected_text =
-        "<span class='h-card'><a data-user='#{mike.id}' class='u-url mention' href='#{mike.ap_id}'>@<span>mike@osada.macgirvin.com</span></a></span> test"
+        "<span class='h-card'><a data-user='#{mike.id}' class='u-url mention' href='#{mike.ap_id}'>@<span>mike</span></a></span> test"
 
       assert expected_text == Formatter.finalize({subs, text})
     end
index cfccce8d118dadba29cd27299053d615957b89ad..21a62483f7dbcf97017f3f9b9a7abae63f641e7c 100644 (file)
@@ -874,4 +874,19 @@ defmodule Pleroma.UserTest do
       Pleroma.Config.put([:instance, :account_activation_required], false)
     end
   end
+
+  describe "parse_bio/2" do
+    test "preserves hosts in user links text" do
+      remote_user = insert(:user, local: false, nickname: "nick@domain.com")
+      user = insert(:user)
+      bio = "A.k.a. @nick@domain.com"
+
+      expected_text =
+        "A.k.a. <span class='h-card'><a data-user='#{remote_user.id}' class='u-url mention' href='#{
+          remote_user.ap_id
+        }'>" <> "@<span>nick@domain.com</span></a></span>"
+
+      assert expected_text == User.parse_bio(bio, user)
+    end
+  end
 end
index b8f1afa76fce4603398c38ead46342ff9c2f84d9..5f7481eb6bb2964d19a56ecdcef692050b47dbe5 100644 (file)
@@ -12,7 +12,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
   import Pleroma.Factory
 
   setup do
-    user = insert(:user, bio: "<span>Here's some html,</span> @mention@domain.com")
+    user = insert(:user, bio: "<span>Here's some html</span>")
     [user: user]
   end