New Exporter::NonContactAuthors class

This class is capable of quering a list of people from authors of given posts that are non-contacts of a given
user.
This commit is contained in:
cmrd Senya 2017-04-02 20:49:14 +03:00
parent c985af1f85
commit c63493b0d1
No known key found for this signature in database
GPG key ID: 5FCC5BA680E67BFE
2 changed files with 61 additions and 0 deletions

View file

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

View file

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