IZ MS; fixed post update. Posts which implement the mutable? method now can be updated via receive

This commit is contained in:
maxwell 2010-11-02 12:38:30 -07:00
parent 9e1c6174bf
commit 19bbf8ce4d
9 changed files with 107 additions and 25 deletions

View file

@ -32,6 +32,10 @@ class Album < Post
p_photo ? p_photo : self.photos.sort(:created_at.desc).last p_photo ? p_photo : self.photos.sort(:created_at.desc).last
end end
def mutable?
true
end
protected protected
def destroy_photos def destroy_photos
self.photos.each{|p| p.destroy} self.photos.each{|p| p.destroy}

View file

@ -74,5 +74,9 @@ class Photo < Post
def thumb_hash def thumb_hash
{:thumb_url => url(:thumb_medium), :id => id, :album_id => album_id} {:thumb_url => url(:thumb_medium), :id => id, :album_id => album_id}
end end
def mutable?
true
end
end end

View file

@ -50,6 +50,10 @@ class Post
} }
end end
def mutable?
false
end
protected protected
def destroy_comments def destroy_comments
comments.each{|c| c.destroy} comments.each{|c| c.destroy}

View file

@ -10,14 +10,7 @@ module Diaspora
begin begin
new_object = body.name.camelize.constantize.from_xml body.to_s new_object = body.name.camelize.constantize.from_xml body.to_s
return new_object
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
rescue NameError => e rescue NameError => e
if e.message.include? 'wrong constant name' if e.message.include? 'wrong constant name'
Rails.logger.info "Not a real type: #{object.to_s}" Rails.logger.info "Not a real type: #{object.to_s}"

View file

@ -45,13 +45,13 @@ module Diaspora
raise "Not friends with that person" unless self.contact_for(salmon_author) raise "Not friends with that person" unless self.contact_for(salmon_author)
if object.is_a?(Comment) if object.is_a?(Comment)
receive_comment object, xml receive_comment object
elsif object.is_a?(Retraction) elsif object.is_a?(Retraction)
receive_retraction object, xml receive_retraction object
elsif object.is_a?(Profile) elsif object.is_a?(Profile)
receive_profile object, person receive_profile object, person
else else
receive_post object, xml receive_post object
end end
end end
} }
@ -60,7 +60,7 @@ module Diaspora
end end
end end
def receive_retraction retraction, xml def receive_retraction retraction
if retraction.type == 'Person' if retraction.type == 'Person'
unless retraction.person.id.to_s == retraction.post_id.to_s unless retraction.person.id.to_s == retraction.post_id.to_s
raise "#{retraction.diaspora_handle} trying to unfriend #{retraction.post_id} from #{self.id}" raise "#{retraction.diaspora_handle} trying to unfriend #{retraction.post_id} from #{self.id}"
@ -91,7 +91,7 @@ module Diaspora
person.save person.save
end 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 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.visible_people = self.visible_people | [comment.person]
self.save self.save
@ -105,7 +105,38 @@ module Diaspora
comment.socket_to_uid(id) if (comment.respond_to?(:socket_to_uid) && !self.owns?(comment)) comment.socket_to_uid(id) if (comment.respond_to?(:socket_to_uid) && !self.owns?(comment))
end end
def receive_post post, xml def exsists_on_pod?(post)
post.class.find_by_id(post.id)
end
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
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}") Rails.logger.debug("Saving post: #{post}")
post.user_refs += 1 post.user_refs += 1
post.save post.save
@ -114,11 +145,11 @@ module Diaspora
self.save self.save
aspects = self.aspects_with_person(post.person) aspects = self.aspects_with_person(post.person)
aspects.each{ |aspect| aspects.each do |aspect|
aspect.posts << post aspect.posts << post
aspect.save aspect.save
post.socket_to_uid(id, :aspect_ids => [aspect.id]) if (post.respond_to?(:socket_to_uid) && !self.owns?(post)) post.socket_to_uid(id, :aspect_ids => [aspect.id]) if (post.respond_to?(:socket_to_uid) && !self.owns?(post))
} end
end end
end end
end end

View file

@ -23,6 +23,11 @@ describe Album do
album.associations[:photos].type.should == :many album.associations[:photos].type.should == :many
end 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 context 'when an album has two attached images' do
before do before do
2.times do 2.times do

View file

@ -31,6 +31,10 @@ describe Photo do
end end
end end
it 'should be mutable' do
@photo.mutable?.should == true
end
it 'has a constructor' do it 'has a constructor' do
image = File.open(@fixture_name) image = File.open(@fixture_name)
photo = Photo.instantiate( photo = Photo.instantiate(

View file

@ -29,5 +29,12 @@ describe Post do
xml.include?(@user.person.diaspora_handle).should be true xml.include?(@user.person.diaspora_handle).should be true
end end
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 end

View file

@ -41,6 +41,36 @@ describe User do
user.aspects.size.should == num_aspects user.aspects.size.should == num_aspects
end 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 describe 'post refs' do
before do before do
@status_message = user2.post :status_message, :message => "hi", :to =>aspect2.id @status_message = user2.post :status_message, :message => "hi", :to =>aspect2.id