diff --git a/lib/diaspora/mentionable.rb b/lib/diaspora/mentionable.rb index 4029054d37a9d60dd586a373e1bb64d1ea4a2587..f1b8cc9180eb69d3cb16f3a26269dd764c8f268c 100644 --- a/lib/diaspora/mentionable.rb +++ b/lib/diaspora/mentionable.rb @@ -29,8 +29,8 @@ module Diaspora::Mentionable people = [*people] msg_text.to_s.gsub(REGEX) {|match_str| - name, handle = mention_attrs(match_str) - person = people.find {|p| p.diaspora_handle == handle } + name, diaspora_id = mention_attrs(match_str) + person = people.find {|p| p.diaspora_handle == diaspora_id } ERB::Util.h(MentionsInternal.mention_link(person, name, opts)) } @@ -42,10 +42,7 @@ module Diaspora::Mentionable # @param [String] text containing mentions # @return [Array<Person>] array of people def self.people_from_string(msg_text) - identifiers = msg_text.to_s.scan(REGEX).map do |match_str| - identifier = match_str.second.strip - identifier if Validation::Rule::DiasporaId.new.valid_value?(identifier) - end + identifiers = msg_text.to_s.scan(REGEX).map {|match_str| match_str.second.strip } identifiers.compact.uniq.map {|identifier| find_or_fetch_person_by_identifier(identifier) }.compact end @@ -61,16 +58,30 @@ module Diaspora::Mentionable mentioned_ppl = people_from_string(msg_text) msg_text.to_s.gsub(REGEX) {|match_str| - name, handle = mention_attrs(match_str) - person = mentioned_ppl.find {|p| p.diaspora_handle == handle } + name, diaspora_id = mention_attrs(match_str) + person = mentioned_ppl.find {|p| p.diaspora_handle == diaspora_id } mention = MentionsInternal.profile_link(person, name) unless allowed_people.include?(person.id) mention || match_str } end + # Regex to find mentions with new syntax, only used for backporting to old syntax + NEW_SYNTAX_REGEX = /@\{[^ ]+\}/ + + # replaces new syntax with old syntax, to be compatible with old pods + # @deprecated remove when most of the posts can handle the new syntax + def self.backport_mention_syntax(text) + text.to_s.gsub(NEW_SYNTAX_REGEX) do |match_str| + _, diaspora_id = mention_attrs(match_str) + person = find_or_fetch_person_by_identifier(diaspora_id) + old_syntax = "@{#{person.name}; #{diaspora_id}}" if person + old_syntax || match_str + end + end + 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) if Validation::Rule::DiasporaId.new.valid_value?(identifier) rescue DiasporaFederation::Discovery::DiscoveryError nil end diff --git a/lib/diaspora/mentions_container.rb b/lib/diaspora/mentions_container.rb index 5364aa5ae1a9e1933b13c70b7d749d0d44cddaa8..6aea4acf52e820b9fd7521c8e73420d38f2c76e3 100644 --- a/lib/diaspora/mentions_container.rb +++ b/lib/diaspora/mentions_container.rb @@ -3,6 +3,11 @@ module Diaspora extend ActiveSupport::Concern included do + before_create do + # TODO: remove when most of the posts can handle the new syntax + self.text = Diaspora::Mentionable.backport_mention_syntax(text) if text + end + after_create :create_mentions has_many :mentions, as: :mentions_container, dependent: :destroy end diff --git a/spec/lib/diaspora/mentionable_spec.rb b/spec/lib/diaspora/mentionable_spec.rb index 6a6274ad6473540eb00057ab4bbfccca4831cb0d..b01161a82aa8f7f75d46f8828020b43ad140fa63 100644 --- a/spec/lib/diaspora/mentionable_spec.rb +++ b/spec/lib/diaspora/mentionable_spec.rb @@ -175,4 +175,29 @@ STR expect(txt).to include(@mention_b) end end + + describe ".backport_mention_syntax" do + it "replaces the new syntax with the old syntax" do + text = "mention @{#{@people[0].diaspora_handle}} text" + expected_text = "mention @{#{@people[0].name}; #{@people[0].diaspora_handle}} text" + expect(Diaspora::Mentionable.backport_mention_syntax(text)).to eq(expected_text) + end + + it "does not change the text, when the mention includes a name" do + text = "mention @{#{@names[0]}; #{@people[0].diaspora_handle}} text" + expect(Diaspora::Mentionable.backport_mention_syntax(text)).to eq(text) + end + + it "does not change the text, when the person is not found" do + text = "mention @{non_existing_user@example.org} text" + expect(Person).to receive(:find_or_fetch_by_identifier).with("non_existing_user@example.org").and_return(nil) + expect(Diaspora::Mentionable.backport_mention_syntax(text)).to eq(text) + end + + it "does not change the text, when the diaspora ID is invalid" do + text = "mention @{invalid_diaspora_id} text" + expect(Person).not_to receive(:find_or_fetch_by_identifier) + expect(Diaspora::Mentionable.backport_mention_syntax(text)).to eq(text) + end + end end diff --git a/spec/shared_behaviors/mentions_container.rb b/spec/shared_behaviors/mentions_container.rb index 87dedfef5fafa7f9d361ea1c4ace9635cadac58f..42633e6e94267ebd91c6e39e0563df8dba740515 100644 --- a/spec/shared_behaviors/mentions_container.rb +++ b/spec/shared_behaviors/mentions_container.rb @@ -10,6 +10,16 @@ shared_examples_for "it is mentions container" do target } + describe ".before_create" do + it "backports mention syntax to old syntax" do + text = "mention @{#{people[0].diaspora_handle}} text" + expected_text = "mention @{#{people[0].name}; #{people[0].diaspora_handle}} text" + obj = FactoryGirl.build(described_class.to_s.underscore.to_sym, text: text, author: alice.person) + obj.save + expect(obj.text).to eq(expected_text) + end + end + describe ".after_create" do it "calls create_mentions" do expect(target).to receive(:create_mentions).and_call_original