diff --git a/Changelog.md b/Changelog.md index 6033f85560c2b8404f176ec6c1e6cf342bc28bb2..c3c7629df078d0768d91293166332d401089c15b 100644 --- a/Changelog.md +++ b/Changelog.md @@ -199,6 +199,7 @@ The command will report queues that still have jobs and launch sidekiq process f * Display message when there are no posts in a stream [#6974](https://github.com/diaspora/diaspora/pull/6974) * Add bootstrap-markdown editor to the publisher [#6551](https://github.com/diaspora/diaspora/pull/6551) * Don't create notifications for ignored users [#6984](https://github.com/diaspora/diaspora/pull/6984) +* Fetch missing persons when receiving a mention for them [#6992](https://github.com/diaspora/diaspora/pull/6992) # 0.5.10.2 diff --git a/lib/diaspora/mentionable.rb b/lib/diaspora/mentionable.rb index 147364cb3e9947098dd6183282da4a6a6ea9c770..8645d90200830a108c83ae4244ebe81772c7448a 100644 --- a/lib/diaspora/mentionable.rb +++ b/lib/diaspora/mentionable.rb @@ -46,12 +46,11 @@ module Diaspora::Mentionable # @return [Array<Person>] 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 280fcbac1220713fb323268ef01aa08a6fd912ad..552097a2ebf2d0e2223497efd7a33ba3ca2edf25 100644 --- a/spec/lib/diaspora/mentionable_spec.rb +++ b/spec/lib/diaspora/mentionable_spec.rb @@ -70,7 +70,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 @@ -80,7 +80,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 26bfe34a4af7551b5c150e2370e038e3a0eb1c3f..0094f8f496bc01a9f3fb75a70994d7245f6c75a4 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