diff --git a/app/models/user.rb b/app/models/user.rb index 12322dca6a818dacf513d0ad4f081ff27e27636a..f4937788dc553b7333884f97cefa31f146b38c99 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -47,7 +47,7 @@ class User many :aspects, :class_name => 'Aspect' - after_create :seed_aspects + #after_create :seed_aspects before_validation :downcase_username, :on => :create validates_with InvitedUserValidator @@ -294,7 +294,11 @@ class User opts[:serialized_private_key] = generate_key opts[:person][:serialized_public_key] = opts[:serialized_private_key].public_key - User.create(opts) + + u = User.new(opts) + u.seed_aspects + u.save! + u end def seed_aspects diff --git a/lib/diaspora/exporter.rb b/lib/diaspora/exporter.rb index 74c1f9726320e8ba6022783c586390f91e0d5034..589cacfa9aaccb13d93d8cf649c1c476f9ff9efb 100644 --- a/lib/diaspora/exporter.rb +++ b/lib/diaspora/exporter.rb @@ -33,8 +33,8 @@ module Diaspora } xml.post_ids { - aspect.post_ids.each do |id| - xml.post_id id + aspect.posts.each do |post| + xml.post_id post.id end } } diff --git a/lib/diaspora/importer.rb b/lib/diaspora/importer.rb index c333f53d1b7b9fdfaa7d17225b7222f133bd8856..c0a7aa2557fe9ac4d7626c823de79407cdb66acb 100644 --- a/lib/diaspora/importer.rb +++ b/lib/diaspora/importer.rb @@ -9,11 +9,44 @@ module Diaspora self.class.send(:include, strategy) end + def commit(user, person, aspects, people, posts) + filter = verify_and_clean(user, person, people, aspects, posts) + #assume data is good + + # to go + user.email = "tits@tits.tits" + user.password= "megatits@tits.tits" + user.password_confirmation = "megatits@tits.tits" - def commit(user, person, aspects, filters) - - filters[:unknown].values.each do |x| + + + user.person = person + + user.person.diaspora_handle = "obby@foo.com" + + user.visible_post_ids = filter[:whitelist].keys + + user.friend_ids = people.collect{ |x| x.id } + user.visible_person_ids = user.friend_ids + + user.save! + user.person.save! + + posts.each do |post| + post.save! if filter[:unknown].include? post.id + end + + + + aspects.each do |aspect| + user.aspects << aspect + end + + + + people.each do |p| + p.save! #if filter[:people].include? person.id end end @@ -22,8 +55,14 @@ module Diaspora def verify_and_clean(user, person, people, aspects, posts) verify_user(user) verify_person_for_user(user, person) - post_filter = filter_posts(posts, person) - clean_aspects(aspects, post_filter[:whitelist]) + filters = filter_posts(posts, person) + + + clean_aspects(aspects, filters[:whitelist]) + + + filters[:people] = filter_people(people) + filters end def verify_user(user) @@ -43,28 +82,29 @@ module Diaspora 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 } + people_from_db = Person.find_all_by_id(person_ids) #this query should be limited to only return person_id + person_ids = person_ids - people_from_db.collect{ |x| x.id } + + person_hash = {} + person_ids.each{|x| person_hash[x.to_s] = true } + person_hash 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 - - + unknown_posts = post_ids - posts_from_db.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 } + unknown_posts.each{|x| unknown[x.to_s] = true } whitelist = {} - post_whitelist.each{|x| whitelist[x] = true } + post_whitelist.each{|x| whitelist[x.to_s] = true } return { :unknown => unknown, @@ -73,8 +113,8 @@ module Diaspora def clean_aspects(aspects, whitelist) - aspects.collect! do |aspect| - aspect.post_ids.delete_if{ |x| !whitelist.include? x } + aspects.each do |aspect| + aspect.post_ids.delete_if{ |x| !whitelist.include? x.to_s } end end end @@ -90,7 +130,7 @@ module Diaspora posts = parse_posts(doc) user - + commit(user, person, aspects, people, posts) end def parse_user_and_person(doc) diff --git a/spec/lib/exporter_spec.rb b/spec/lib/exporter_spec.rb index 619208bafa777124bac5044c9b2bbcdd3ab91dc2..b902f12d735ab1e65f5d144ee94edf458edac5b0 100644 --- a/spec/lib/exporter_spec.rb +++ b/spec/lib/exporter_spec.rb @@ -34,6 +34,8 @@ describe Diaspora::Exporter do it 'should include post_ids' do doc = Nokogiri::XML::parse(exported) doc.xpath('//aspects').to_s.should include status_message1.id.to_s + + doc.xpath('//aspects').to_s.should include status_message2.id.to_s doc.xpath('//posts').to_s.should include status_message1.id.to_s end diff --git a/spec/lib/importer_spec.rb b/spec/lib/importer_spec.rb index 59b3d1b342730b63669fa6c4156f6d16c8bc1a9c..7385c35b25f01c7be92df1d445ce052ad9afaf03 100644 --- a/spec/lib/importer_spec.rb +++ b/spec/lib/importer_spec.rb @@ -31,15 +31,23 @@ describe Diaspora::Importer do @aspect8 = @user5.aspect(:name => "Hamsters") @aspect9 = @user5.aspect(:name => "Gophers") + @aspect10 = @user1.aspect(:name => "Work") + @aspect11 = @user1.aspect(:name => "Family") + # User1 posts one status messages to aspects (1-4), two other users post message to one aspect - @status_message1 = @user1.post(:status_message, :message => "One", :public => true, :to => @aspect1.id) - @status_message2 = @user1.post(:status_message, :message => "Two", :public => true, :to => @aspect2.id) + @status_message1 = @user1.post(:status_message, :message => "One", :public => false, :to => @aspect1.id) + @status_message2 = @user1.post(:status_message, :message => "Two", :public => false, :to => @aspect2.id) @status_message3 = @user1.post(:status_message, :message => "Three", :public => false, :to => @aspect3.id) @status_message4 = @user1.post(:status_message, :message => "Four", :public => false, :to => @aspect4.id) @status_message5 = @user2.post(:status_message, :message => "Five", :public => false, :to => @aspect5.id) @status_message6 = @user3.post(:status_message, :message => "Six", :public => false, :to => @aspect6.id) @status_message7 = @user5.post(:status_message, :message => "Seven", :public => false, :to => @aspect9.id) + @aspect1.posts << @status_message1 + @aspect2.posts << @status_message2 + @aspect3.posts << @status_message3 + @aspect4.posts << @status_message4 + # Friend users with user1 friend_users( @user1, @aspect1, @user2, @aspect5 ) friend_users( @user1, @aspect2, @user3, @aspect6 ) @@ -62,7 +70,9 @@ describe Diaspora::Importer do end it 'should gut check this test' do + @user1.friends.count.should be 4 + @user1.friends.should include @user2.person @user1.friends.should include @user3.person @user1.friends.should include @user4.person @@ -83,6 +93,7 @@ describe Diaspora::Importer do before(:each) do # Generate exported XML for user1 exporter = Diaspora::Exporter.new(Diaspora::Exporters::XML) + @user1.aspects.reload @xml = exporter.execute(@user1) @old_user = @user1 @@ -97,6 +108,7 @@ describe Diaspora::Importer do end it 'should import a user' do + pending user = @importer.execute(@xml) user.class.should == User end @@ -161,5 +173,64 @@ describe Diaspora::Importer do end end + + describe 'importing a user' do + + context '#execute' do + before(:each) do + # Generate exported XML for user1 + exporter = Diaspora::Exporter.new(Diaspora::Exporters::XML) + @xml = exporter.execute(@user1) + + # Remove user1 from the server + @user1.aspects.each( &:delete ) + @user1.raw_visible_posts.find_all_by_person_id(@user1.person.id).each( &:delete ) + @user1.delete + + @importer = Diaspora::Importer.new(Diaspora::Parsers::XML) + end + + it 'should import' do + User.delete_all + Person.delete_all + Post.delete_all + StatusMessage.delete_all + Aspect.delete_all + + User.count.should == 0 + Person.count.should == 0 + + @importer.execute(@xml) + + User.count.should == 1 + n = User.first + Post.count.should == 4 + n.aspects.count.should == 6 + Person.count.should be == 5 + + + Person.find_by_id( @user1.person.id ).nil?.should == false + Person.find_by_id( @user2.person.id ).nil?.should == false + + n.aspects.count.should == 6 + + people_count = 0 + n.aspects.each{|x| people_count += x.people.count } + people_count.should == 4 + + post_count = 0 + n.aspects.reload + n.aspects.each{ |x| post_count += x.post_ids.count } + post_count.should == 4 + + n.friends.count.should be 4 + end + + + + end + + end + end diff --git a/spec/lib/verify_spec.rb b/spec/lib/verify_spec.rb index 6590c5d2b6997a08f14cbc9e295b584fc268fee8..b0b8a0de65ed3debcd03b647499236a3c9c6b1a3 100644 --- a/spec/lib/verify_spec.rb +++ b/spec/lib/verify_spec.rb @@ -58,8 +58,8 @@ describe Diaspora::Importer do 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 + whitelist.should include status_message1.id.to_s + whitelist.should include status_message2.id.to_s end it 'should remove posts not owned by the user' do @@ -87,29 +87,23 @@ describe Diaspora::Importer do describe '#clean_aspects' do it 'should purge posts not in whitelist that are present in aspects' do - whitelist = {status_message1.id => true, status_message2.id => true} + whitelist = {status_message1.id.to_s => true, status_message2.id.to_s => true} aspect1.reload - aspect1.post_ids << status_message3.id - - aspect1.post_ids.should have(3).ids - - importer.clean_aspects([aspect1], whitelist) + aspect1.post_ids << status_message3.id.to_s + proc{ importer.clean_aspects([aspect1], whitelist) }.should change(aspect1.post_ids, :count).by(-1) aspect1.post_ids.should_not include status_message3.id - aspect1.post_ids.should have(2).ids end end describe '#filter_people' do - it 'should filter people who already exist in the database' do - people = [user1.person, user2.person, Factory.build(:person)] - - importer.filter_people(people).should have(1).person + new_peep = Factory.build(:person) + people = [user1.person, user2.person, new_peep] + + importer.filter_people(people).keys.should == [new_peep.id.to_s] end - - end end end diff --git a/spec/models/aspect_spec.rb b/spec/models/aspect_spec.rb index b3acbd028031d8ffb0254c3ed6053f1666830dce..4fbbff5cb8d8069e36f9f2b9724c6bf472fa9f53 100644 --- a/spec/models/aspect_spec.rb +++ b/spec/models/aspect_spec.rb @@ -50,7 +50,7 @@ describe Aspect do it 'belong to a user' do @aspect.user.id.should == @user.id - @user.aspects.size.should == 3 + @user.aspects.size.should == 1 end it 'should have people' do