From 824201fedc52cd2ee344c6aa8d72a7810835497f Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Mon, 27 Jun 2016 16:43:00 +0200 Subject: [PATCH] fetch mentioned people if they don't exist locally yet fixes #4491 --- lib/diaspora/mentionable.rb | 13 +++++++++---- spec/lib/diaspora/mentionable_spec.rb | 23 +++++++++++++++++++++-- spec/lib/publisher_spec.rb | 2 +- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/diaspora/mentionable.rb b/lib/diaspora/mentionable.rb index f64857f77..6d43f686d 100644 --- a/lib/diaspora/mentionable.rb +++ b/lib/diaspora/mentionable.rb @@ -46,12 +46,11 @@ 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| - _, handle = mention_attrs(match_str.first) - handle + _, identifier = mention_attrs(match_str.first) + identifier if Validation::Rule::DiasporaId.new.valid_value?(identifier) end - return [] if identifiers.empty? - Person.where(diaspora_handle: identifiers) + identifiers.compact.uniq.map {|identifier| find_or_fetch_person_by_identifier(identifier) }.compact end # takes a message text and converts mentions for people that are not in the @@ -81,6 +80,12 @@ module Diaspora::Mentionable private + private_class_method def self.find_or_fetch_person_by_identifier(identifier) + Person.find_or_fetch_by_identifier(identifier) + rescue DiasporaFederation::Discovery::DiscoveryError + nil + end + # inline module for namespacing module MentionsInternal extend ::PeopleHelper diff --git a/spec/lib/diaspora/mentionable_spec.rb b/spec/lib/diaspora/mentionable_spec.rb index a328fe7cc..a0b4138de 100644 --- a/spec/lib/diaspora/mentionable_spec.rb +++ b/spec/lib/diaspora/mentionable_spec.rb @@ -72,7 +72,7 @@ STR 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 include(*@people) + expect(ppl).to match_array(@people) end describe "returns an empty array if nobody was found" do @@ -82,7 +82,26 @@ STR end it "gets a post with invalid handles" do - ppl = Diaspora::Mentionable.people_from_string("@{a; xxx@xxx.xx} @{b; yyy@yyyy.yyy} @{...} @{bla; blubb}") + ppl = Diaspora::Mentionable.people_from_string("@{...} @{bla; blubb}") + expect(ppl).to be_empty + end + + it "filters duplicate handles" do + ppl = Diaspora::Mentionable.people_from_string("@{a; #{alice.diaspora_handle}} @{a; #{alice.diaspora_handle}}") + expect(ppl).to eq([alice.person]) + end + + it "fetches unknown handles" do + person = FactoryGirl.build(:person) + expect(Person).to receive(:find_or_fetch_by_identifier).with("xxx@xxx.xx").and_return(person) + ppl = Diaspora::Mentionable.people_from_string("@{a; xxx@xxx.xx}") + expect(ppl).to eq([person]) + end + + it "handles DiscoveryError" do + expect(Person).to receive(:find_or_fetch_by_identifier).with("yyy@yyy.yy") + .and_raise(DiasporaFederation::Discovery::DiscoveryError) + ppl = Diaspora::Mentionable.people_from_string("@{b; yyy@yyy.yy}") expect(ppl).to be_empty end end diff --git a/spec/lib/publisher_spec.rb b/spec/lib/publisher_spec.rb index 26bfe34a4..0094f8f49 100644 --- a/spec/lib/publisher_spec.rb +++ b/spec/lib/publisher_spec.rb @@ -18,7 +18,7 @@ describe Publisher do describe '#text' do it 'is a formatted version of the prefill' do - p = Publisher.new(alice, :prefill => "@{alice; alice@pod.com}") + p = Publisher.new(alice, prefill: "@{alice; #{alice.diaspora_handle}}") expect(p.text).to eq("alice") end end