From 19bbf8ce4d89b3bf5a4de28acde9d79fed292308 Mon Sep 17 00:00:00 2001 From: maxwell Date: Tue, 2 Nov 2010 12:38:30 -0700 Subject: [PATCH] IZ MS; fixed post update. Posts which implement the mutable? method now can be updated via receive --- app/models/album.rb | 4 ++ app/models/photo.rb | 4 ++ app/models/post.rb | 4 ++ lib/diaspora/parser.rb | 9 +---- lib/diaspora/user/receiving.rb | 65 +++++++++++++++++++++++--------- spec/models/album_spec.rb | 5 +++ spec/models/photo_spec.rb | 4 ++ spec/models/post_spec.rb | 7 ++++ spec/models/user/receive_spec.rb | 30 +++++++++++++++ 9 files changed, 107 insertions(+), 25 deletions(-) diff --git a/app/models/album.rb b/app/models/album.rb index fbc64b2cf..9be93701c 100644 --- a/app/models/album.rb +++ b/app/models/album.rb @@ -32,6 +32,10 @@ class Album < Post p_photo ? p_photo : self.photos.sort(:created_at.desc).last end + def mutable? + true + end + protected def destroy_photos self.photos.each{|p| p.destroy} diff --git a/app/models/photo.rb b/app/models/photo.rb index bce9d469d..e9790cd9f 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -74,5 +74,9 @@ class Photo < Post def thumb_hash {:thumb_url => url(:thumb_medium), :id => id, :album_id => album_id} end + + def mutable? + true + end end diff --git a/app/models/post.rb b/app/models/post.rb index 298d25b5f..842af2b2e 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -50,6 +50,10 @@ class Post } end + def mutable? + false + end + protected def destroy_comments comments.each{|c| c.destroy} diff --git a/lib/diaspora/parser.rb b/lib/diaspora/parser.rb index acdd27afd..da3344d84 100644 --- a/lib/diaspora/parser.rb +++ b/lib/diaspora/parser.rb @@ -10,14 +10,7 @@ module Diaspora begin new_object = body.name.camelize.constantize.from_xml body.to_s - - if new_object.is_a? Post - existing_object = new_object.class.find_by_id(new_object.id) - existing_object ? (return existing_object) : (return new_object) - end - - new_object - + return new_object rescue NameError => e if e.message.include? 'wrong constant name' Rails.logger.info "Not a real type: #{object.to_s}" diff --git a/lib/diaspora/user/receiving.rb b/lib/diaspora/user/receiving.rb index 9e9da020c..b3c6729a7 100644 --- a/lib/diaspora/user/receiving.rb +++ b/lib/diaspora/user/receiving.rb @@ -45,13 +45,13 @@ module Diaspora raise "Not friends with that person" unless self.contact_for(salmon_author) if object.is_a?(Comment) - receive_comment object, xml + receive_comment object elsif object.is_a?(Retraction) - receive_retraction object, xml + receive_retraction object elsif object.is_a?(Profile) receive_profile object, person else - receive_post object, xml + receive_post object end end } @@ -60,7 +60,7 @@ module Diaspora end end - def receive_retraction retraction, xml + def receive_retraction retraction if retraction.type == 'Person' unless retraction.person.id.to_s == retraction.post_id.to_s raise "#{retraction.diaspora_handle} trying to unfriend #{retraction.post_id} from #{self.id}" @@ -91,7 +91,7 @@ module Diaspora person.save end - def receive_comment comment, xml + def receive_comment comment raise "In receive for #{self.real_name}, signature was not valid on: #{comment.inspect}" unless comment.post.person == self.person || comment.verify_post_creator_signature self.visible_people = self.visible_people | [comment.person] self.save @@ -105,20 +105,51 @@ module Diaspora comment.socket_to_uid(id) if (comment.respond_to?(:socket_to_uid) && !self.owns?(comment)) end - def receive_post post, xml - Rails.logger.debug("Saving post: #{post}") - post.user_refs += 1 - post.save + def exsists_on_pod?(post) + post.class.find_by_id(post.id) + end - self.raw_visible_posts << post - self.save + def receive_post post + #exsists locally, but you dont know about it + #does not exsist locally, and you dont know about it + + #exsists_locally? + #you know about it, and it is mutable + #you know about it, and it is not mutable + # + on_pod = exsists_on_pod?(post) + if on_pod + known_post = find_visible_post_by_id(post.id) + if known_post + if known_post.mutable? + known_post.update_attributes(post.to_mongo) + else + Rails.logger.info("#{post.diaspora_handle} is trying to update an immutable object #{known_post.inspect}") + end + elsif on_pod == post + update_user_refs_and_add_to_aspects(on_pod) + end - aspects = self.aspects_with_person(post.person) - aspects.each{ |aspect| - aspect.posts << post - aspect.save - post.socket_to_uid(id, :aspect_ids => [aspect.id]) if (post.respond_to?(:socket_to_uid) && !self.owns?(post)) - } + else + update_user_refs_and_add_to_aspects(post) + end + end + + + def update_user_refs_and_add_to_aspects(post) + Rails.logger.debug("Saving post: #{post}") + post.user_refs += 1 + post.save + + self.raw_visible_posts << post + self.save + + aspects = self.aspects_with_person(post.person) + aspects.each do |aspect| + aspect.posts << post + aspect.save + post.socket_to_uid(id, :aspect_ids => [aspect.id]) if (post.respond_to?(:socket_to_uid) && !self.owns?(post)) + end end end end diff --git a/spec/models/album_spec.rb b/spec/models/album_spec.rb index d8226c2dc..6c3890611 100644 --- a/spec/models/album_spec.rb +++ b/spec/models/album_spec.rb @@ -23,6 +23,11 @@ describe Album do album.associations[:photos].type.should == :many end + it 'should be mutable' do + post = user.post :album, :name => "hello", :to => aspect.id + post.mutable?.should == true + end + context 'when an album has two attached images' do before do 2.times do diff --git a/spec/models/photo_spec.rb b/spec/models/photo_spec.rb index f5d07fc55..b048e9c16 100644 --- a/spec/models/photo_spec.rb +++ b/spec/models/photo_spec.rb @@ -31,6 +31,10 @@ describe Photo do end end + it 'should be mutable' do + @photo.mutable?.should == true + end + it 'has a constructor' do image = File.open(@fixture_name) photo = Photo.instantiate( diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 587751b0c..e4ca64938 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -29,5 +29,12 @@ describe Post do xml.include?(@user.person.diaspora_handle).should be true end end + + describe '#mutable?' do + it 'should be false by default' do + post = @user.post :status_message, :message => "hello", :to => @aspect.id + post.mutable?.should == false + end + end end diff --git a/spec/models/user/receive_spec.rb b/spec/models/user/receive_spec.rb index 7d504f1d3..6f309b6fd 100644 --- a/spec/models/user/receive_spec.rb +++ b/spec/models/user/receive_spec.rb @@ -41,6 +41,36 @@ describe User do user.aspects.size.should == num_aspects end + context 'update posts' do + let(:status) {user.post(:status_message, :message => "Original", :to => aspect.id)} + let(:album) {user.post(:album, :name => "Original", :to => aspect.id)} + + it 'does not update posts not marked as mutable' do + user2.receive_salmon(user.salmon(status).xml_for(user2.person)) + status.message = 'foo' + xml = user.salmon(status).xml_for(user2.person) + + status.reload.message.should == 'Original' + + user2.receive_salmon(xml) + + status.reload.message.should == 'Original' + end + + it 'updates posts marked as mutable' do + user2.receive_salmon(user.salmon(album).xml_for(user2.person)) + album.name = 'foo' + xml = user.salmon(album).xml_for(user2.person) + + album.reload.name.should == 'Original' + + user2.receive_salmon(xml) + + album.reload.name.should == 'foo' + end + + end + describe 'post refs' do before do @status_message = user2.post :status_message, :message => "hi", :to =>aspect2.id