[#491] Made full nicknames be preserved in user links text only in Bio.

This commit is contained in:
Ivan Tashkinov 2019-01-18 09:30:16 +03:00
parent 2bfae25a1f
commit 65bb9b2fba
6 changed files with 47 additions and 16 deletions

View file

@ -120,7 +120,7 @@ def add_links({subs, text}) do
end end
@doc "Adds the links to mentioned users" @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 =
mentions mentions
|> Enum.sort_by(fn {name, _} -> -String.length(name) end) |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
@ -142,11 +142,16 @@ def add_user_links({subs, text}, mentions) do
ap_id ap_id
end end
full_match = String.trim_leading(match, "@") nickname =
if options[:format] == :full do
User.full_nickname(match)
else
User.local_nickname(match)
end
{uuid, {uuid,
"<span class='h-card'><a data-user='#{id}' class='u-url mention' href='#{ap_id}'>" <> "<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) end)
{subs, uuid_text} {subs, uuid_text}

View file

@ -447,8 +447,7 @@ def get_cached_by_nickname(nickname) do
def get_by_nickname(nickname) do def get_by_nickname(nickname) do
Repo.get_by(User, nickname: nickname) || Repo.get_by(User, nickname: nickname) ||
if Regex.match?(~r(@#{Pleroma.Web.Endpoint.host()})i, nickname) do if Regex.match?(~r(@#{Pleroma.Web.Endpoint.host()})i, nickname) do
[local_nickname, _] = String.split(nickname, "@") Repo.get_by(User, nickname: local_nickname(nickname))
Repo.get_by(User, nickname: local_nickname)
end end
end end
@ -990,7 +989,7 @@ def parse_bio(bio, user) do
end) end)
bio bio
|> CommonUtils.format_input(mentions, tags, "text/plain") |> CommonUtils.format_input(mentions, tags, "text/plain", user_links: [format: :full])
|> Formatter.emojify(emoji) |> Formatter.emojify(emoji)
end end
@ -1041,6 +1040,16 @@ defp local_nickname_regex() do
end end
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 def error_user(ap_id) do
%User{ %User{
name: ap_id, name: ap_id,

View file

@ -116,16 +116,18 @@ def add_attachments(text, attachments) do
Enum.join([text | attachment_text], "<br>") Enum.join([text | attachment_text], "<br>")
end end
def format_input(text, mentions, tags, format, options \\ [])
@doc """ @doc """
Formatting text to plain text. 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 text
|> Formatter.html_escape("text/plain") |> Formatter.html_escape("text/plain")
|> String.replace(~r/\r?\n/, "<br>") |> String.replace(~r/\r?\n/, "<br>")
|> (&{[], &1}).() |> (&{[], &1}).()
|> Formatter.add_links() |> Formatter.add_links()
|> Formatter.add_user_links(mentions) |> Formatter.add_user_links(mentions, options[:user_links] || [])
|> Formatter.add_hashtag_links(tags) |> Formatter.add_hashtag_links(tags)
|> Formatter.finalize() |> Formatter.finalize()
end end
@ -133,24 +135,24 @@ def format_input(text, mentions, tags, "text/plain") do
@doc """ @doc """
Formatting text to html. Formatting text to html.
""" """
def format_input(text, mentions, _tags, "text/html") do def format_input(text, mentions, _tags, "text/html", options) do
text text
|> Formatter.html_escape("text/html") |> Formatter.html_escape("text/html")
|> (&{[], &1}).() |> (&{[], &1}).()
|> Formatter.add_user_links(mentions) |> Formatter.add_user_links(mentions, options[:user_links] || [])
|> Formatter.finalize() |> Formatter.finalize()
end end
@doc """ @doc """
Formatting text to markdown. Formatting text to markdown.
""" """
def format_input(text, mentions, tags, "text/markdown") do def format_input(text, mentions, tags, "text/markdown", options) do
text text
|> Formatter.mentions_escape(mentions) |> Formatter.mentions_escape(mentions)
|> Earmark.as_html!() |> Earmark.as_html!()
|> Formatter.html_escape("text/html") |> Formatter.html_escape("text/html")
|> (&{[], &1}).() |> (&{[], &1}).()
|> Formatter.add_user_links(mentions) |> Formatter.add_user_links(mentions, options[:user_links] || [])
|> Formatter.add_hashtag_links(tags) |> Formatter.add_hashtag_links(tags)
|> Formatter.finalize() |> Formatter.finalize()
end end

View file

@ -124,7 +124,7 @@ test "turning urls into links" do
end end
describe "add_user_links" do 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" text = "@gsimg According to @archa_eme_, that is @daggsy. Also hello @archaeme@archae.me"
gsimg = insert(:user, %{nickname: "gsimg"}) gsimg = insert(:user, %{nickname: "gsimg"})
@ -150,7 +150,7 @@ test "gives a replacement for user links" do
archaeme.id 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='#{ }' 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 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}) assert expected_text == Formatter.finalize({subs, text})
end 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) Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
expected_text = 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}) assert expected_text == Formatter.finalize({subs, text})
end end

View file

@ -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) Pleroma.Config.put([:instance, :account_activation_required], false)
end end
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 end

View file

@ -12,7 +12,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
import Pleroma.Factory import Pleroma.Factory
setup do 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] [user: user]
end end