Backport new mention syntax to old syntax for backward compatibility

See #7276
This commit is contained in:
Benjamin Neff 2017-01-28 23:54:06 +01:00
parent a0d200d209
commit 322f92110a
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
4 changed files with 60 additions and 9 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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