From a0d200d209ab8e94d55d847d1e196c9519edbf94 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Fri, 27 Jan 2017 23:44:35 +0100 Subject: [PATCH] Update regex for new mention syntax See #7276 --- lib/diaspora/mentionable.rb | 20 +++++--------- spec/lib/diaspora/mentionable_spec.rb | 38 ++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/lib/diaspora/mentionable.rb b/lib/diaspora/mentionable.rb index c922096af..4029054d3 100644 --- a/lib/diaspora/mentionable.rb +++ b/lib/diaspora/mentionable.rb @@ -1,23 +1,20 @@ module Diaspora::Mentionable - # regex for finding mention markup in plain text - # ex. + # regex for finding mention markup in plain text: + # "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" # will yield "User Name" and "user@pod.net" - REGEX = /(@\{(.+?; [^\}]+)\})/ + REGEX = /@\{(?:([^\}]+?); )?([^\} ]+)\}/ # class attribute that will be added to all mention html links PERSON_HREF_CLASS = "mention hovercardable" def self.mention_attrs(mention_str) - mention = mention_str.match(REGEX)[2] - del_pos = mention.rindex(/;/) + name, diaspora_id = mention_str.match(REGEX).captures - name = mention[0..(del_pos - 1)].strip - handle = mention[(del_pos + 1)..-1].strip - - [name, handle] + [name.try(:strip), diaspora_id.strip] end # takes a message text and returns the text with mentions in (html escaped) @@ -46,7 +43,7 @@ module Diaspora::Mentionable # @return [Array] array of people def self.people_from_string(msg_text) 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) end @@ -72,8 +69,6 @@ module Diaspora::Mentionable } end - private - private_class_method def self.find_or_fetch_person_by_identifier(identifier) Person.find_or_fetch_by_identifier(identifier) rescue DiasporaFederation::Discovery::DiscoveryError @@ -113,5 +108,4 @@ module Diaspora::Mentionable "[#{display_name.presence || person.name}](#{local_or_remote_person_path(person)})" end end - end diff --git a/spec/lib/diaspora/mentionable_spec.rb b/spec/lib/diaspora/mentionable_spec.rb index de8e2fcac..6a6274ad6 100644 --- a/spec/lib/diaspora/mentionable_spec.rb +++ b/spec/lib/diaspora/mentionable_spec.rb @@ -18,7 +18,27 @@ three "Eve> E. STR 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 it "adds the links to the formatted message" do fmt_msg = Diaspora::Mentionable.format(@test_txt, @people) @@ -64,12 +84,24 @@ STR end end - describe "#people_from_string" do + describe ".people_from_string" do it "extracts the mentioned people from the text" do ppl = Diaspora::Mentionable.people_from_string(@test_txt) expect(ppl).to match_array(@people) 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 it "gets a post without mentions" do ppl = Diaspora::Mentionable.people_from_string("post w/o mentions") @@ -102,7 +134,7 @@ STR end end - describe "#filter_people" do + describe ".filter_people" do before do @user_a = FactoryGirl.create(:user_with_aspect, username: "user_a") @user_b = FactoryGirl.create(:user, username: "user_b")