clean up posting spec, move socket_to_uid into add_to_streams

This commit is contained in:
Raphael 2010-12-01 13:43:36 -08:00
parent 836b5685f7
commit 199eb8945d
2 changed files with 30 additions and 47 deletions

View file

@ -166,8 +166,7 @@ class User
#socket post
Rails.logger.info("event=dispatch user=#{diaspora_handle} post=#{post.id.to_s}")
push_to_aspects(post, aspect_ids)
post.socket_to_uid(id, :aspect_ids => aspect_ids) if post.respond_to?(:socket_to_uid) && !post.pending
push_to_aspects(post, aspects_from_ids(aspect_ids))
if post.public
self.services.each do |service|
@ -200,6 +199,7 @@ class User
self.raw_visible_posts << post
self.save
post.socket_to_uid(id, :aspect_ids => aspect_ids) if post.respond_to? :socket_to_uid
target_aspects = aspects_from_ids(aspect_ids)
target_aspects.each do |aspect|
aspect.posts << post
@ -219,23 +219,13 @@ class User
end
end
def push_to_aspects(post, aspect_ids)
if aspect_ids == :all || aspect_ids == "all"
aspects = self.aspects
elsif aspect_ids.is_a?(Array) && aspect_ids.first.class == Aspect
aspects = aspect_ids
else
aspects = self.aspects.find_all_by_id(aspect_ids)
end
def push_to_aspects(post, aspects)
#send to the aspects
target_contacts = []
aspects.each { |aspect|
target_contacts = target_contacts | aspect.contacts
target_contacts = aspects.inject([]) { |contacts,aspect|
contacts = contacts | aspect.contacts
}
push_to_hub(post) if post.respond_to?(:public) && post.public
push_to_people(post, self.person_objects(target_contacts))
end
@ -250,7 +240,7 @@ class User
person.reload # Sadly, we need this for Ruby 1.9.
# person.owner will always return a ProxyObject.
# calling nil? performs a necessary evaluation.
unless person.owner.nil?
if person.owner_id
Rails.logger.info("event=push_to_person route=local sender=#{self.diaspora_handle} recipient=#{person.diaspora_handle} payload_type=#{post.class}")
person.owner.receive(post.to_diaspora_xml, self.person)
else

View file

@ -37,6 +37,10 @@ describe User do
aspect1.reload.post_ids.should include @post.id
end
it 'sockets the post to the poster' do
@post.should_receive(:socket_to_uid).with(user.id, anything)
user.add_to_streams(@post, @aspect_ids)
end
end
describe '#aspects_from_ids' do
@ -67,45 +71,34 @@ describe User do
end
describe '#dispatch_post' do
it 'should put the post in the aspect post array' do
post = user.post(:status_message, :message => "hey", :to => aspect.id)
aspect.reload
aspect.posts.should include post
let(:status) {user.build_post(:status_message, @status_opts)}
before do
@message = "hello, world!"
@status_opts = {:to => "all", :message => @message}
end
it "should add the post to that user's visible posts" do
status_message = user.post :status_message, :message => "hi", :to => aspect.id
user.reload
user.raw_visible_posts.include?(status_message).should be true
end
it "posts to services if post is public" do
message = "hello, world!"
user.should_receive(:post_to_twitter).with(service1, message).exactly(1).times
user.should_receive(:post_to_facebook).with(service2, message).exactly(1).times
user.post :status_message, :message => message, :to => "all", :public => true
@status_opts[:public] = true
status.save
user.should_receive(:post_to_twitter).with(service1, @message).once
user.should_receive(:post_to_facebook).with(service2, @message).once
user.dispatch_post(status, :to => "all")
end
it "does not post to services if post is not public" do
user.should_receive(:post_to_twitter).exactly(0).times
user.should_receive(:post_to_facebook).exactly(0).times
user.post :status_message, :message => "hi", :to => "all"
end
it 'should not socket a pending post' do
sm = user.build_post(:status_message, :message => "your mom", :to => aspect.id, :pending => true)
sm.should_not_receive(:socket_to_uid)
user.dispatch_post(sm, :to => aspect.id)
@status_opts[:public] = false
status.save
user.should_not_receive(:post_to_twitter)
user.should_not_receive(:post_to_facebook)
user.dispatch_post(status, :to => "all")
end
end
describe '#post' do
it 'should not create a post with invalid aspect' do
pending "this would just causes db polution"
post_count = Post.count
proc { user.post(:status_message, :message => "hey", :to => aspect2.id) }.should raise_error /Cannot post to an aspect you do not own./
Post.count.should == post_count
pending "this would just cause db polution"
proc {
user.post(:status_message, :message => "hey", :to => aspect2.id)
}.should_not change(Post, :count)
end
end
@ -139,12 +132,12 @@ describe User do
describe '#push_to_aspects' do
it 'should push a post to a aspect' do
user.should_receive(:push_to_person).twice
user.push_to_aspects(post, aspect.id)
user.push_to_aspects(post, [aspect])
end
it 'should push a post to contacts in all aspects' do
user.should_receive(:push_to_person).exactly(3).times
user.push_to_aspects(post, :all)
user.push_to_aspects(post, user.aspects)
end
end