diff --git a/lib/diaspora/exporter/non_contact_authors.rb b/lib/diaspora/exporter/non_contact_authors.rb new file mode 100644 index 000000000..f3cfc4801 --- /dev/null +++ b/lib/diaspora/exporter/non_contact_authors.rb @@ -0,0 +1,36 @@ +module Diaspora + class Exporter + # This class is capable of quering a list of people from authors of given posts that are non-contacts of a given + # user. + class NonContactAuthors + # @param posts [Post::ActiveRecord_Relation] posts that we fetch authors from to make authors list + # @param user [User] a user we fetch a contact list from + def initialize(posts, user) + @posts = posts + @user = user + end + + # Create a request of non-contact authors of the posts for the user + # @return [Post::ActiveRecord_Relation] + def query + Person.where(id: non_contact_authors_ids) + end + + private + + def non_contact_authors_ids + posts_authors_ids - contacts_ids + end + + def posts_authors_ids + posts.pluck(:author_id).uniq + end + + def contacts_ids + user.contacts.pluck(:person_id) + end + + attr_reader :posts, :user + end + end +end diff --git a/spec/lib/diaspora/exporter/non_contact_authors_spec.rb b/spec/lib/diaspora/exporter/non_contact_authors_spec.rb new file mode 100644 index 000000000..cac8c3ca0 --- /dev/null +++ b/spec/lib/diaspora/exporter/non_contact_authors_spec.rb @@ -0,0 +1,25 @@ +describe Diaspora::Exporter::NonContactAuthors do + describe "#query" do + let(:user) { FactoryGirl.create(:user_with_aspect) } + let(:post) { FactoryGirl.create(:status_message) } + let(:instance) { + Diaspora::Exporter::NonContactAuthors.new(Post.where(id: post.id), user) + } + + context "without contact relationship" do + it "includes post author to the result set" do + expect(instance.query).to eq([post.author]) + end + end + + context "with contact relationship" do + before do + user.share_with(post.author, user.aspects.first) + end + + it "doesn't include post author to the result set" do + expect(instance.query).to be_empty + end + end + end +end