Skip to content

Commit

Permalink
clean up posting spec, move socket_to_uid into add_to_streams
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael committed Dec 1, 2010
1 parent 836b568 commit 199eb89
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 47 deletions.
22 changes: 6 additions & 16 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ def dispatch_post(post, opts = {})

#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|
Expand Down Expand Up @@ -200,6 +199,7 @@ def add_to_streams(post, aspect_ids)
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
Expand All @@ -219,23 +219,13 @@ def aspects_from_ids(aspect_ids)
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

Expand All @@ -250,7 +240,7 @@ def push_to_person(salmon, post, person)
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
Expand Down
55 changes: 24 additions & 31 deletions spec/models/user/posting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
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
Expand Down Expand Up @@ -67,45 +71,34 @@
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
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
let(:status) {user.build_post(:status_message, @status_opts)}
before do
@message = "hello, world!"
@status_opts = {:to => "all", :message => @message}
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

Expand Down Expand Up @@ -139,12 +132,12 @@
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

Expand Down

0 comments on commit 199eb89

Please sign in to comment.