DG MS; added more stuff to importer.

This commit is contained in:
maxwell 2010-10-13 16:42:27 -07:00
parent 2c40fa5ebf
commit b395208a51
7 changed files with 149 additions and 38 deletions

View file

@ -47,7 +47,7 @@ class User
many :aspects, :class_name => 'Aspect' many :aspects, :class_name => 'Aspect'
after_create :seed_aspects #after_create :seed_aspects
before_validation :downcase_username, :on => :create before_validation :downcase_username, :on => :create
validates_with InvitedUserValidator validates_with InvitedUserValidator
@ -294,7 +294,11 @@ class User
opts[:serialized_private_key] = generate_key opts[:serialized_private_key] = generate_key
opts[:person][:serialized_public_key] = opts[:serialized_private_key].public_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 end
def seed_aspects def seed_aspects

View file

@ -33,8 +33,8 @@ module Diaspora
} }
xml.post_ids { xml.post_ids {
aspect.post_ids.each do |id| aspect.posts.each do |post|
xml.post_id id xml.post_id post.id
end end
} }
} }

View file

@ -9,11 +9,44 @@ module Diaspora
self.class.send(:include, strategy) self.class.send(:include, strategy)
end 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
end end
@ -22,8 +55,14 @@ module Diaspora
def verify_and_clean(user, person, people, aspects, posts) def verify_and_clean(user, person, people, aspects, posts)
verify_user(user) verify_user(user)
verify_person_for_user(user, person) verify_person_for_user(user, person)
post_filter = filter_posts(posts, person) filters = filter_posts(posts, person)
clean_aspects(aspects, post_filter[:whitelist])
clean_aspects(aspects, filters[:whitelist])
filters[:people] = filter_people(people)
filters
end end
def verify_user(user) def verify_user(user)
@ -43,28 +82,29 @@ module Diaspora
def filter_people(people) def filter_people(people)
person_ids = people.collect{|x| x.id} 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 people_from_db = Person.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 } 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 end
def filter_posts(posts, person) def filter_posts(posts, person)
post_ids = posts.collect{|x| x.id} 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_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} unknown_posts = post_ids - posts_from_db.collect{|x| x.id}
posts_from_db.delete_if{|x| x.person_id == person.id} posts_from_db.delete_if{|x| x.person_id == person.id}
unauthorized_post_ids = posts_from_db.collect{|x| x.id} unauthorized_post_ids = posts_from_db.collect{|x| x.id}
post_whitelist = post_ids - unauthorized_post_ids post_whitelist = post_ids - unauthorized_post_ids
unknown = {} unknown = {}
unknown_posts.each{|x| unknown[x] = true } unknown_posts.each{|x| unknown[x.to_s] = true }
whitelist = {} whitelist = {}
post_whitelist.each{|x| whitelist[x] = true } post_whitelist.each{|x| whitelist[x.to_s] = true }
return { return {
:unknown => unknown, :unknown => unknown,
@ -73,8 +113,8 @@ module Diaspora
def clean_aspects(aspects, whitelist) def clean_aspects(aspects, whitelist)
aspects.collect! do |aspect| aspects.each do |aspect|
aspect.post_ids.delete_if{ |x| !whitelist.include? x } aspect.post_ids.delete_if{ |x| !whitelist.include? x.to_s }
end end
end end
end end
@ -90,7 +130,7 @@ module Diaspora
posts = parse_posts(doc) posts = parse_posts(doc)
user user
commit(user, person, aspects, people, posts)
end end
def parse_user_and_person(doc) def parse_user_and_person(doc)

View file

@ -34,6 +34,8 @@ describe Diaspora::Exporter do
it 'should include post_ids' do it 'should include post_ids' do
doc = Nokogiri::XML::parse(exported) 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_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 doc.xpath('//posts').to_s.should include status_message1.id.to_s
end end

View file

@ -31,15 +31,23 @@ describe Diaspora::Importer do
@aspect8 = @user5.aspect(:name => "Hamsters") @aspect8 = @user5.aspect(:name => "Hamsters")
@aspect9 = @user5.aspect(:name => "Gophers") @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 # 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_message1 = @user1.post(:status_message, :message => "One", :public => false, :to => @aspect1.id)
@status_message2 = @user1.post(:status_message, :message => "Two", :public => true, :to => @aspect2.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_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_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_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_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) @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 with user1
friend_users( @user1, @aspect1, @user2, @aspect5 ) friend_users( @user1, @aspect1, @user2, @aspect5 )
friend_users( @user1, @aspect2, @user3, @aspect6 ) friend_users( @user1, @aspect2, @user3, @aspect6 )
@ -62,7 +70,9 @@ describe Diaspora::Importer do
end end
it 'should gut check this test' do it 'should gut check this test' do
@user1.friends.count.should be 4 @user1.friends.count.should be 4
@user1.friends.should include @user2.person @user1.friends.should include @user2.person
@user1.friends.should include @user3.person @user1.friends.should include @user3.person
@user1.friends.should include @user4.person @user1.friends.should include @user4.person
@ -83,6 +93,7 @@ describe Diaspora::Importer do
before(:each) do before(:each) do
# Generate exported XML for user1 # Generate exported XML for user1
exporter = Diaspora::Exporter.new(Diaspora::Exporters::XML) exporter = Diaspora::Exporter.new(Diaspora::Exporters::XML)
@user1.aspects.reload
@xml = exporter.execute(@user1) @xml = exporter.execute(@user1)
@old_user = @user1 @old_user = @user1
@ -97,6 +108,7 @@ describe Diaspora::Importer do
end end
it 'should import a user' do it 'should import a user' do
pending
user = @importer.execute(@xml) user = @importer.execute(@xml)
user.class.should == User user.class.should == User
end end
@ -161,5 +173,64 @@ describe Diaspora::Importer do
end end
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 end

View file

@ -58,8 +58,8 @@ describe Diaspora::Importer do
whitelist = importer.filter_posts(posts, user1.person)[:whitelist] whitelist = importer.filter_posts(posts, user1.person)[:whitelist]
whitelist.should have(2).posts whitelist.should have(2).posts
whitelist.should include status_message1.id whitelist.should include status_message1.id.to_s
whitelist.should include status_message2.id whitelist.should include status_message2.id.to_s
end end
it 'should remove posts not owned by the user' do it 'should remove posts not owned by the user' do
@ -87,29 +87,23 @@ describe Diaspora::Importer do
describe '#clean_aspects' do describe '#clean_aspects' do
it 'should purge posts not in whitelist that are present in 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.reload
aspect1.post_ids << status_message3.id aspect1.post_ids << status_message3.id.to_s
aspect1.post_ids.should have(3).ids
importer.clean_aspects([aspect1], whitelist)
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_not include status_message3.id
aspect1.post_ids.should have(2).ids
end end
end end
describe '#filter_people' do describe '#filter_people' do
it 'should filter people who already exist in the database' do it 'should filter people who already exist in the database' do
people = [user1.person, user2.person, Factory.build(:person)] new_peep = Factory.build(:person)
people = [user1.person, user2.person, new_peep]
importer.filter_people(people).should have(1).person
importer.filter_people(people).keys.should == [new_peep.id.to_s]
end end
end end
end end
end end

View file

@ -50,7 +50,7 @@ describe Aspect do
it 'belong to a user' do it 'belong to a user' do
@aspect.user.id.should == @user.id @aspect.user.id.should == @user.id
@user.aspects.size.should == 3 @user.aspects.size.should == 1
end end
it 'should have people' do it 'should have people' do