+ expected =
+ "<a href=\"https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul\">https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul</a>"
+
+ assert {^expected, [], []} = Formatter.linkify(text)
+
+ text = "https://www.google.co.jp/search?q=Nasim+Aghdam"
+
+ expected =
+ "<a href=\"https://www.google.co.jp/search?q=Nasim+Aghdam\">https://www.google.co.jp/search?q=Nasim+Aghdam</a>"
+
+ assert {^expected, [], []} = Formatter.linkify(text)
+
+ text = "https://en.wikipedia.org/wiki/Duff's_device"
+
+ expected =
+ "<a href=\"https://en.wikipedia.org/wiki/Duff's_device\">https://en.wikipedia.org/wiki/Duff's_device</a>"
+
+ assert {^expected, [], []} = Formatter.linkify(text)
+
+ text = "https://pleroma.com https://pleroma.com/sucks"
+
+ expected =
+ "<a href=\"https://pleroma.com\">https://pleroma.com</a> <a href=\"https://pleroma.com/sucks\">https://pleroma.com/sucks</a>"
+
+ assert {^expected, [], []} = Formatter.linkify(text)
+
+ text = "xmpp:contact@hacktivis.me"
+
+ expected = "<a href=\"xmpp:contact@hacktivis.me\">xmpp:contact@hacktivis.me</a>"
+
+ assert {^expected, [], []} = Formatter.linkify(text)
+
+ text =
+ "magnet:?xt=urn:btih:7ec9d298e91d6e4394d1379caf073c77ff3e3136&tr=udp%3A%2F%2Fopentor.org%3A2710&tr=udp%3A%2F%2Ftracker.blackunicorn.xyz%3A6969&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com"
+
+ expected = "<a href=\"#{text}\">#{text}</a>"
+
+ assert {^expected, [], []} = Formatter.linkify(text)
+ end
+ end
+
+ describe "add_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"})
+
+ archaeme =
+ insert(:user, %{
+ nickname: "archa_eme_",
+ info: %Pleroma.User.Info{source_data: %{"url" => "https://archeme/@archa_eme_"}}
+ })
+
+ archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
+
+ {text, mentions, []} = Formatter.linkify(text)
+
+ assert length(mentions) == 3
+
+ expected_text =
+ "<span class='h-card'><a data-user='#{gsimg.id}' class='u-url mention' href='#{
+ gsimg.ap_id
+ }'>@<span>gsimg</span></a></span> According to <span class='h-card'><a data-user='#{
+ 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</span></a></span>"
+
+ assert expected_text == text
+ end
+
+ test "gives a replacement for user links when the user is using Osada" do
+ {:ok, mike} = User.get_or_fetch("mike@osada.macgirvin.com")
+
+ text = "@mike@osada.macgirvin.com test"
+
+ {text, mentions, []} = Formatter.linkify(text)
+
+ assert length(mentions) == 1
+
+ expected_text =
+ "<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 == text
+ end
+
+ test "gives a replacement for single-character local nicknames" do
+ text = "@o hi"
+ o = insert(:user, %{nickname: "o"})
+
+ {text, mentions, []} = Formatter.linkify(text)
+
+ assert length(mentions) == 1
+
+ expected_text =
+ "<span class='h-card'><a data-user='#{o.id}' class='u-url mention' href='#{o.ap_id}'>@<span>o</span></a></span> hi"
+
+ assert expected_text == text
+ end
+
+ test "does not give a replacement for single-character local nicknames who don't exist" do
+ text = "@a hi"
+
+ expected_text = "@a hi"
+ assert {^expected_text, [] = _mentions, [] = _tags} = Formatter.linkify(text)
+ end
+
+ test "given the 'safe_mention' option, it will only mention people in the beginning" do
+ user = insert(:user)
+ _other_user = insert(:user)
+ third_user = insert(:user)
+ text = " @#{user.nickname} hey dude i hate @#{third_user.nickname}"
+ {expected_text, mentions, [] = _tags} = Formatter.linkify(text, safe_mention: true)
+
+ assert mentions == [{"@#{user.nickname}", user}]
+
+ assert expected_text ==
+ "<span class='h-card'><a data-user='#{user.id}' class='u-url mention' href='#{
+ user.ap_id
+ }'>@<span>#{user.nickname}</span></a></span> hey dude i hate <span class='h-card'><a data-user='#{
+ third_user.id
+ }' class='u-url mention' href='#{third_user.ap_id}'>@<span>#{third_user.nickname}</span></a></span>"
+ end
+
+ test "given the 'safe_mention' option, it will still work without any mention" do
+ text = "A post without any mention"
+ {expected_text, mentions, [] = _tags} = Formatter.linkify(text, safe_mention: true)
+
+ assert mentions == []
+ assert expected_text == text