Update regex for new mention syntax

See #7276
This commit is contained in:
Benjamin Neff 2017-01-27 23:44:35 +01:00
parent 2aa91e0b62
commit a0d200d209
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
2 changed files with 42 additions and 16 deletions

View file

@ -1,23 +1,20 @@
module Diaspora::Mentionable module Diaspora::Mentionable
# regex for finding mention markup in plain text # regex for finding mention markup in plain text:
# ex. # "message @{user@pod.net} text"
# it can also contain a name, which gets used as the link text:
# "message @{User Name; user@pod.net} text" # "message @{User Name; user@pod.net} text"
# will yield "User Name" and "user@pod.net" # will yield "User Name" and "user@pod.net"
REGEX = /(@\{(.+?; [^\}]+)\})/ REGEX = /@\{(?:([^\}]+?); )?([^\} ]+)\}/
# class attribute that will be added to all mention html links # class attribute that will be added to all mention html links
PERSON_HREF_CLASS = "mention hovercardable" PERSON_HREF_CLASS = "mention hovercardable"
def self.mention_attrs(mention_str) def self.mention_attrs(mention_str)
mention = mention_str.match(REGEX)[2] name, diaspora_id = mention_str.match(REGEX).captures
del_pos = mention.rindex(/;/)
name = mention[0..(del_pos - 1)].strip [name.try(:strip), diaspora_id.strip]
handle = mention[(del_pos + 1)..-1].strip
[name, handle]
end end
# takes a message text and returns the text with mentions in (html escaped) # takes a message text and returns the text with mentions in (html escaped)
@ -46,7 +43,7 @@ module Diaspora::Mentionable
# @return [Array<Person>] array of people # @return [Array<Person>] array of people
def self.people_from_string(msg_text) def self.people_from_string(msg_text)
identifiers = msg_text.to_s.scan(REGEX).map do |match_str| identifiers = msg_text.to_s.scan(REGEX).map do |match_str|
_, identifier = mention_attrs(match_str.first) identifier = match_str.second.strip
identifier if Validation::Rule::DiasporaId.new.valid_value?(identifier) identifier if Validation::Rule::DiasporaId.new.valid_value?(identifier)
end end
@ -72,8 +69,6 @@ module Diaspora::Mentionable
} }
end end
private
private_class_method def self.find_or_fetch_person_by_identifier(identifier) private_class_method def self.find_or_fetch_person_by_identifier(identifier)
Person.find_or_fetch_by_identifier(identifier) Person.find_or_fetch_by_identifier(identifier)
rescue DiasporaFederation::Discovery::DiscoveryError rescue DiasporaFederation::Discovery::DiscoveryError
@ -113,5 +108,4 @@ module Diaspora::Mentionable
"[#{display_name.presence || person.name}](#{local_or_remote_person_path(person)})" "[#{display_name.presence || person.name}](#{local_or_remote_person_path(person)})"
end end
end end
end end

View file

@ -18,7 +18,27 @@ three &quot;Eve&gt; E.
STR STR
end end
describe "#format" do describe ".mention_attrs" do
it "returns name and diaspora ID" do
name, diaspora_id = Diaspora::Mentionable.mention_attrs("@{#{@names[0]}; #{@people[0].diaspora_handle}}")
expect(name).to eq(@names[0])
expect(diaspora_id).to eq(@people[0].diaspora_handle)
end
it "returns only diaspora-ID when no name is included" do
name, diaspora_id = Diaspora::Mentionable.mention_attrs("@{#{@people[0].diaspora_handle}}")
expect(diaspora_id).to eq(@people[0].diaspora_handle)
expect(name).to be_nil
end
it "trims the name if available" do
name, diaspora_id = Diaspora::Mentionable.mention_attrs("@{#{@names[0]} ; #{@people[0].diaspora_handle}}")
expect(name).to eq(@names[0])
expect(diaspora_id).to eq(@people[0].diaspora_handle)
end
end
describe ".format" do
context "html output" do context "html output" do
it "adds the links to the formatted message" do it "adds the links to the formatted message" do
fmt_msg = Diaspora::Mentionable.format(@test_txt, @people) fmt_msg = Diaspora::Mentionable.format(@test_txt, @people)
@ -64,12 +84,24 @@ STR
end end
end end
describe "#people_from_string" do describe ".people_from_string" do
it "extracts the mentioned people from the text" do it "extracts the mentioned people from the text" do
ppl = Diaspora::Mentionable.people_from_string(@test_txt) ppl = Diaspora::Mentionable.people_from_string(@test_txt)
expect(ppl).to match_array(@people) expect(ppl).to match_array(@people)
end end
it "extracts the mentioned people from the text without name" do
text = "test @{#{@people[0].diaspora_handle}} test"
ppl = Diaspora::Mentionable.people_from_string(text)
expect(ppl).to match_array([@people[0]])
end
it "extracts the mentioned people from the text mixed mentions (with and without name)" do
text = "@{#{@people[0].diaspora_handle}} and @{#{@names[1]}; #{@people[1].diaspora_handle}}"
ppl = Diaspora::Mentionable.people_from_string(text)
expect(ppl).to match_array([@people[0], @people[1]])
end
describe "returns an empty array if nobody was found" do describe "returns an empty array if nobody was found" do
it "gets a post without mentions" do it "gets a post without mentions" do
ppl = Diaspora::Mentionable.people_from_string("post w/o mentions") ppl = Diaspora::Mentionable.people_from_string("post w/o mentions")
@ -102,7 +134,7 @@ STR
end end
end end
describe "#filter_people" do describe ".filter_people" do
before do before do
@user_a = FactoryGirl.create(:user_with_aspect, username: "user_a") @user_a = FactoryGirl.create(:user_with_aspect, username: "user_a")
@user_b = FactoryGirl.create(:user, username: "user_b") @user_b = FactoryGirl.create(:user, username: "user_b")