Move mention parsing to Formatter module.

This commit is contained in:
Roger Braun 2017-06-14 13:58:56 +02:00
parent 9376443973
commit 1af9c77736
4 changed files with 32 additions and 29 deletions

View file

@ -1,4 +1,5 @@
defmodule Pleroma.Formatter do defmodule Pleroma.Formatter do
alias Pleroma.User
@link_regex ~r/https?:\/\/[\w\.\/?=\-#]+[\w]/ @link_regex ~r/https?:\/\/[\w\.\/?=\-#]+[\w]/
def linkify(text) do def linkify(text) do
@ -10,4 +11,15 @@ def parse_tags(text) do
Regex.scan(@tag_regex, text) Regex.scan(@tag_regex, text)
|> Enum.map(fn (["#" <> tag = full_tag]) -> {full_tag, tag} end) |> Enum.map(fn (["#" <> tag = full_tag]) -> {full_tag, tag} end)
end end
def parse_mentions(text) do
# Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
regex = ~r/@[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@?[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*/
Regex.scan(regex, text)
|> List.flatten
|> Enum.uniq
|> Enum.map(fn ("@" <> match = full_match) -> {full_match, User.get_cached_by_nickname(match)} end)
|> Enum.filter(fn ({_match, user}) -> user end)
end
end end

View file

@ -31,7 +31,7 @@ def get_replied_to_activity(_), do: nil
def create_status(%User{} = user, %{"status" => status} = data) do def create_status(%User{} = user, %{"status" => status} = data) do
with attachments <- attachments_from_ids(data["media_ids"]), with attachments <- attachments_from_ids(data["media_ids"]),
mentions <- parse_mentions(status), mentions <- Formatter.parse_mentions(status),
inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]), inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]),
to <- to_for_user_and_mentions(user, mentions, inReplyTo), to <- to_for_user_and_mentions(user, mentions, inReplyTo),
content_html <- make_content_html(status, mentions, attachments), content_html <- make_content_html(status, mentions, attachments),
@ -182,17 +182,6 @@ def upload(%Plug.Upload{} = file, format \\ "xml") do
end end
end end
def parse_mentions(text) do
# Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
regex = ~r/@[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@?[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*/
Regex.scan(regex, text)
|> List.flatten
|> Enum.uniq
|> Enum.map(fn ("@" <> match = full_match) -> {full_match, User.get_cached_by_nickname(match)} end)
|> Enum.filter(fn ({_match, user}) -> user end)
end
def register_user(params) do def register_user(params) do
params = %{ params = %{
nickname: params["nickname"], nickname: params["nickname"],

View file

@ -2,6 +2,8 @@ defmodule Pleroma.FormatterTest do
alias Pleroma.Formatter alias Pleroma.Formatter
use Pleroma.DataCase use Pleroma.DataCase
import Pleroma.Factory
describe ".linkify" do describe ".linkify" do
test "turning urls into links" do test "turning urls into links" do
text = "Hey, check out https://www.youtube.com/watch?v=8Zg1-TufFzY." text = "Hey, check out https://www.youtube.com/watch?v=8Zg1-TufFzY."
@ -25,4 +27,20 @@ test "parses tags in the text" do
assert Formatter.parse_tags(text) == expected assert Formatter.parse_tags(text) == expected
end end
end end
test "it can parse mentions and return the relevant users" do
text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
gsimg = insert(:user, %{nickname: "gsimg"})
archaeme = insert(:user, %{nickname: "archaeme"})
archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
expected_result = [
{"@gsimg", gsimg},
{"@archaeme", archaeme},
{"@archaeme@archae.me", archaeme_remote},
]
assert Formatter.parse_mentions(text) == expected_result
end
end end

View file

@ -240,22 +240,6 @@ test "upload a file" do
assert is_binary(response) assert is_binary(response)
end end
test "it can parse mentions and return the relevant users" do
text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
gsimg = insert(:user, %{nickname: "gsimg"})
archaeme = insert(:user, %{nickname: "archaeme"})
archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
expected_result = [
{"@gsimg", gsimg},
{"@archaeme", archaeme},
{"@archaeme@archae.me", archaeme_remote},
]
assert TwitterAPI.parse_mentions(text) == expected_result
end
test "it adds user links to an existing text" do test "it adds user links to an existing text" do
text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me" text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
@ -263,7 +247,7 @@ test "it adds user links to an existing text" do
archaeme = insert(:user, %{nickname: "archaeme"}) archaeme = insert(:user, %{nickname: "archaeme"})
archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"}) archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
mentions = TwitterAPI.parse_mentions(text) mentions = Pleroma.Formatter.parse_mentions(text)
expected_text = "<a href='#{gsimg.ap_id}'>@gsimg</a> According to <a href='#{archaeme.ap_id}'>@archaeme</a>, that is @daggsy. Also hello <a href='#{archaeme_remote.ap_id}'>@archaeme</a>" expected_text = "<a href='#{gsimg.ap_id}'>@gsimg</a> According to <a href='#{archaeme.ap_id}'>@archaeme</a>, that is @daggsy. Also hello <a href='#{archaeme_remote.ap_id}'>@archaeme</a>"
assert Utils.add_user_links(text, mentions) == expected_text assert Utils.add_user_links(text, mentions) == expected_text