diff options
author | Maxwell Salzberg <maxwell@joindiaspora.com> | 2011-09-12 19:32:39 +0400 |
---|---|---|
committer | Maxwell Salzberg <maxwell@joindiaspora.com> | 2011-09-13 01:05:32 +0400 |
commit | 5177e5b2183bd536a8431a4e10ace2dd254081b4 (patch) | |
tree | f65c16f31fd5912b7bbbfaddab17a217cedbb047 | |
parent | 6e60905d0dba7a7b43562d4c7ee689ecc3dff47e (diff) |
DG MS test receive_relayable
-rw-r--r-- | app/models/comment.rb | 12 | ||||
-rw-r--r-- | lib/postzord/receiver/public.rb | 22 | ||||
-rw-r--r-- | spec/lib/postzord/receiver/public_spec.rb | 44 |
3 files changed, 69 insertions, 9 deletions
diff --git a/app/models/comment.rb b/app/models/comment.rb index e455f1457..7214f7b7b 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -56,15 +56,23 @@ class Comment < ActiveRecord::Base end def notification_type(user, person) - if self.post.author == user.person + if user.owns?(self.post) return Notifications::CommentOnPost - elsif self.post.comments.where(:author_id => user.person.id) != [] && self.author_id != user.person.id + elsif user_has_commented_on_others_post?(person, self.post, user) return Notifications::AlsoCommented else return false end end + def user_has_commented_on_others_post?(author, post, user) + Comment.comments_by_author_on_post_exist?(author, post.id) && self.author_id != user.person.id + end + + def self.comments_by_author_on_post_exist?(author, post_id) + Comment.exists?(:author_id => author.id, :post_id => post_id) + end + def parent_class Post end diff --git a/lib/postzord/receiver/public.rb b/lib/postzord/receiver/public.rb index bc007ecf0..55ef99092 100644 --- a/lib/postzord/receiver/public.rb +++ b/lib/postzord/receiver/public.rb @@ -22,7 +22,7 @@ module Postzord return false unless verified_signature? return unless save_object - if @object.respond_to?(:relayable) + if @object.respond_to?(:relayable?) receive_relayable else Resque.enqueue(Job::ReceiveLocalBatch, @object.id, self.recipient_user_ids) @@ -30,11 +30,14 @@ module Postzord end def receive_relayable - raise RelayableObjectWithoutParent.new("Receiving a relayable object without parent object present locally!") unless @object.parent.user.present? - - # receive relayable object only for the owner of the parent object - @object.receive(@object.parent.user, @author) + # unless @object.parent.present? + # raise RelayableObjectWithoutParent.new("Receiving a relayable object without parent object present locally!") + # end + if @object.parent.author.local? + # receive relayable object only for the owner of the parent object + @object.receive(@object.parent.author.user, @author) + end # notify everyone who can see the parent object receiver = Postzord::Receiver::LocalPostBatch.new(nil, self.recipient_user_ids) receiver.notify_users @@ -43,17 +46,22 @@ module Postzord # @return [Object] def save_object @object = Diaspora::Parser::from_xml(@salmon.parsed_data) - raise "Object is not public" unless @object.public? + raise "Object is not public" if object_can_be_public_and_it_is_not? @object.save! end + # @return [Array<Integer>] User ids def recipient_user_ids User.all_sharing_with_person(@author).select('users.id').map!{ |u| u.id } end class RelayableObjectWithoutParent < StandardError ; ; end + private + + def object_can_be_public_and_it_is_not? + @object.respond_to?(:public) && !@object.public? + end end end end - diff --git a/spec/lib/postzord/receiver/public_spec.rb b/spec/lib/postzord/receiver/public_spec.rb index 7fab3872e..3afb1241b 100644 --- a/spec/lib/postzord/receiver/public_spec.rb +++ b/spec/lib/postzord/receiver/public_spec.rb @@ -14,6 +14,19 @@ describe Postzord::Receiver::Public do @xml = @created_salmon.xml_for(nil) end + context 'round trips works with' do + it 'a comment' do + comment = bob.build_comment(:text => 'yo', :post => Factory(:status_message)) + comment.save + xml = Salmon::Slap.create_by_user_and_activity(bob, comment.to_diaspora_xml).xml_for(nil) + comment.destroy + expect{ + receiver = Postzord::Receiver::Public.new(xml) + receiver.perform! + }.to change(Comment, :count).by(1) + end + end + describe '#initialize' do it 'creates a Salmon instance variable' do receiver = Postzord::Receiver::Public.new(@xml) @@ -64,4 +77,35 @@ describe Postzord::Receiver::Public do receiver.perform! end end + + describe '#receive_relayable' do + before do + @comment = bob.build_comment(:text => 'yo', :post => Factory(:status_message)) + @comment.save + created_salmon = Salmon::Slap.create_by_user_and_activity(alice, @comment.to_diaspora_xml) + xml = created_salmon.xml_for(nil) + @comment.delete + @receiver = Postzord::Receiver::Public.new(xml) + end + + it 'raises if parent object does not exist' + + it 'receives only for the parent author if he is local to the pod' do + comment = stub.as_null_object + @receiver.instance_variable_set(:@object, comment) + + comment.should_receive(:receive) + @receiver.receive_relayable + end + + it 'calls notifiy_users' do + comment = stub.as_null_object + @receiver.instance_variable_set(:@object, comment) + + local_post_batch_receiver = stub.as_null_object + Postzord::Receiver::LocalPostBatch.stub(:new).and_return(local_post_batch_receiver) + local_post_batch_receiver.should_receive(:notify_users) + @receiver.receive_relayable + end + end end |