diff --git a/Gemfile.lock b/Gemfile.lock index c92fae55db87afb82cdeb828b66b7436f8cd24b8..659a591259b1cc28cb841ab2bd66a05f2faceb14 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -250,8 +250,8 @@ GEM uuidtools (2.1.1) warden (0.10.7) rack (>= 1.0.0) - webmock (1.3.5) - addressable (>= 2.1.1) + webmock (1.4.0) + addressable (>= 2.2.2) crack (>= 0.1.7) webrat (0.7.1) nokogiri (>= 1.2.0) diff --git a/lib/diaspora/importer.rb b/lib/diaspora/importer.rb index 3ba57305f61b7b2d414573cfc1935870dc595257..633d7d34a554c06e06316bf61f3e4d964dc2bdcf 100644 --- a/lib/diaspora/importer.rb +++ b/lib/diaspora/importer.rb @@ -10,13 +10,20 @@ module Diaspora end + def commit(user, person, aspects, filters) + + filters[:unknown].values.each do |x| + + end + end + ### verification (to be module) ################ - def verify(user, person, people, aspects, posts) + def verify_and_clean(user, person, people, aspects, posts) verify_user(user) verify_person_for_user(user, person) - verified_posts = verify_posts(posts, person) - + post_filter = filter_posts(posts, person) + clean_aspects(aspects, post_filter[:whitelist]) end def verify_user(user) @@ -33,18 +40,43 @@ module Diaspora true end - def verify_posts(posts, person) + + def filter_people(people) + person_ids = people.collect{|x| x.id} + people_from_db = People.find_all_by_id(person_ids) #this query should be limited to only return person_id + person_ids - people_from_db.collect{ |x| x.id } + end + + def filter_posts(posts, person) post_ids = posts.collect{|x| x.id} + posts_from_db = Post.find_all_by_id(post_ids) #this query should be limited to only return post id and owner id + - posts_from_db = Post.find_all_by_id(post_id) #this query should be limited to only return post id and owner id + unknown_posts = post_ids - posts_from_db.collect{|x| x.id} - unauthorized_posts = posts_from_db.delete_if{|x| x.owner_id != person.id} - unauthorized_post_ids = unauthorized_posts.collect{|x| x.id} + posts_from_db.delete_if{|x| x.person_id == person.id} + unauthorized_post_ids = posts_from_db.collect{|x| x.id} post_whitelist = post_ids - unauthorized_post_ids + + unknown = {} + unknown_posts.each{|x| unknown[x] = true } + + whitelist = {} + post_whitelist.each{|x| whitelist[x] = true } + + return { + :unknown => unknown, + :whitelist => whitelist } + end + + + def clean_aspects(aspects, filter) + aspects.collect! do |aspect| + aspect.post_ids.delete_if{ |x| !filter.include? x.id } + end end - end module Parsers diff --git a/spec/lib/verify_spec.rb b/spec/lib/verify_spec.rb index 42153d2a74385c2c01285dc93cdb7dfdef2d1e79..bdbd0d364b31a6b6991f6196f4105ebbd3cc6a0a 100644 --- a/spec/lib/verify_spec.rb +++ b/spec/lib/verify_spec.rb @@ -52,9 +52,29 @@ describe Diaspora::Importer do end - describe '#verify_posts' do + describe '#filter_posts' do it 'should make sure all found posts are owned by the user' do - pending "next test to conquer" + posts = [status_message1, status_message2] + whitelist = importer.filter_posts(posts, user1.person)[:whitelist] + + whitelist.should have(2).posts + whitelist.should include status_message1.id + whitelist.should include status_message2.id + end + + it 'should remove posts not owned by the user' do + posts = [status_message1, status_message2, status_message3] + whitelist = importer.filter_posts(posts, user1.person)[:whitelist] + + whitelist.should have(2).posts + whitelist.should_not include status_message3.id + end + + it 'should return a list of unknown posts' do + posts = [status_message1, status_message2, Factory.build(:status_message)] + unknown = importer.filter_posts(posts, user1.person)[:unknown] + + unknown.should have(1).post end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 403042406702cc5fbdb69aa8f75125e2257470dc..552fd884da34e1119246a4cb5b354f01ea121a77 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -12,7 +12,7 @@ require 'database_cleaner' require 'webmock/rspec' include Devise::TestHelpers -include WebMock +include WebMock::API # Requires supporting files with custom matchers and macros, etc, # in ./support/ and its subdirectories.