fetch mentioned people if they don't exist locally yet

fixes #4491
This commit is contained in:
Benjamin Neff 2016-06-27 16:43:00 +02:00
parent fddec62828
commit 824201fedc
3 changed files with 31 additions and 7 deletions

View file

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

View file

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

View file

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