DG MS test receive_relayable

This commit is contained in:
Maxwell Salzberg 2011-09-12 08:32:39 -07:00
parent 6e60905d0d
commit 5177e5b218
3 changed files with 69 additions and 9 deletions

View file

@ -56,15 +56,23 @@ class Comment < ActiveRecord::Base
end end
def notification_type(user, person) def notification_type(user, person)
if self.post.author == user.person if user.owns?(self.post)
return Notifications::CommentOnPost 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 return Notifications::AlsoCommented
else else
return false return false
end end
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 def parent_class
Post Post
end end

View file

@ -22,7 +22,7 @@ module Postzord
return false unless verified_signature? return false unless verified_signature?
return unless save_object return unless save_object
if @object.respond_to?(:relayable) if @object.respond_to?(:relayable?)
receive_relayable receive_relayable
else else
Resque.enqueue(Job::ReceiveLocalBatch, @object.id, self.recipient_user_ids) Resque.enqueue(Job::ReceiveLocalBatch, @object.id, self.recipient_user_ids)
@ -30,11 +30,14 @@ module Postzord
end end
def receive_relayable def receive_relayable
raise RelayableObjectWithoutParent.new("Receiving a relayable object without parent object present locally!") unless @object.parent.user.present? # unless @object.parent.present?
# raise RelayableObjectWithoutParent.new("Receiving a relayable object without parent object present locally!")
# receive relayable object only for the owner of the parent object # end
@object.receive(@object.parent.user, @author)
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 # notify everyone who can see the parent object
receiver = Postzord::Receiver::LocalPostBatch.new(nil, self.recipient_user_ids) receiver = Postzord::Receiver::LocalPostBatch.new(nil, self.recipient_user_ids)
receiver.notify_users receiver.notify_users
@ -43,17 +46,22 @@ module Postzord
# @return [Object] # @return [Object]
def save_object def save_object
@object = Diaspora::Parser::from_xml(@salmon.parsed_data) @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! @object.save!
end end
# @return [Array<Integer>] User ids # @return [Array<Integer>] User ids
def recipient_user_ids def recipient_user_ids
User.all_sharing_with_person(@author).select('users.id').map!{ |u| u.id } User.all_sharing_with_person(@author).select('users.id').map!{ |u| u.id }
end end
class RelayableObjectWithoutParent < StandardError ; ; 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 end
end end

View file

@ -14,6 +14,19 @@ describe Postzord::Receiver::Public do
@xml = @created_salmon.xml_for(nil) @xml = @created_salmon.xml_for(nil)
end 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 describe '#initialize' do
it 'creates a Salmon instance variable' do it 'creates a Salmon instance variable' do
receiver = Postzord::Receiver::Public.new(@xml) receiver = Postzord::Receiver::Public.new(@xml)
@ -64,4 +77,35 @@ describe Postzord::Receiver::Public do
receiver.perform! receiver.perform!
end end
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 end