From 65bb9b2fba7560df7331645db9839305c47dad11 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Fri, 18 Jan 2019 09:30:16 +0300 Subject: [PATCH] [#491] Made full nicknames be preserved in user links text only in Bio. --- lib/pleroma/formatter.ex | 11 ++++++++--- lib/pleroma/user.ex | 15 ++++++++++++--- lib/pleroma/web/common_api/utils.ex | 14 ++++++++------ test/formatter_test.exs | 6 +++--- test/user_test.exs | 15 +++++++++++++++ test/web/twitter_api/views/user_view_test.exs | 2 +- 6 files changed, 47 insertions(+), 16 deletions(-) diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index 024c6e117..37737853a 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -120,7 +120,7 @@ def add_links({subs, text}) 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 @@ def add_user_links({subs, text}, mentions) 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, "" <> - "@#{full_match}"} + "@#{nickname}"} end) {subs, uuid_text} diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index a52e536d3..2dc3c8d56 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -447,8 +447,7 @@ def get_cached_by_nickname(nickname) 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 @@ def parse_bio(bio, 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 @@ defp local_nickname_regex() 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, diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index 7e30d224c..a36ab5c15 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -116,16 +116,18 @@ def add_attachments(text, attachments) do Enum.join([text | attachment_text], "
") 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/, "
") |> (&{[], &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 @@ def format_input(text, mentions, tags, "text/plain") 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 diff --git a/test/formatter_test.exs b/test/formatter_test.exs index 7040f1c27..2e717194b 100644 --- a/test/formatter_test.exs +++ b/test/formatter_test.exs @@ -124,7 +124,7 @@ test "turning urls into links" 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 @@ test "gives a replacement for user links" do archaeme.id }' class='u-url mention' href='#{"https://archeme/@archa_eme_"}'>@archa_eme_, that is @daggsy. Also hello @archaeme@archae.me" + }' class='u-url mention' href='#{archaeme_remote.ap_id}'>@archaeme" assert expected_text == Formatter.finalize({subs, text}) end @@ -168,7 +168,7 @@ test "gives a replacement for user links when the user is using Osada" do Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end) expected_text = - "@mike@osada.macgirvin.com test" + "@mike test" assert expected_text == Formatter.finalize({subs, text}) end diff --git a/test/user_test.exs b/test/user_test.exs index cfccce8d1..21a62483f 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -874,4 +874,19 @@ test "returns true when the account is unauthenticated and being viewed by a pri 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. " <> "@nick@domain.com" + + assert expected_text == User.parse_bio(bio, user) + end + end end diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index b8f1afa76..5f7481eb6 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -12,7 +12,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do import Pleroma.Factory setup do - user = insert(:user, bio: "Here's some html, @mention@domain.com") + user = insert(:user, bio: "Here's some html") [user: user] end