diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index 9388690d1c101d1e65f3a72808a55da90a99581b..d2a52e4c6edb3c9ca217d30d54e764821c64f0b0 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -46,6 +46,7 @@ class PhotosController < ApplicationController if @photo.save raise 'MongoMapper failed to catch a failed save' unless @photo.id + current_user.add_to_streams(@photo, params[:photo][:aspect_ids]) current_user.dispatch_post(@photo, :to => params[:photo][:aspect_ids]) unless @photo.pending respond_to do |format| format.json{ render(:layout => false , :json => {"success" => true, "data" => @photo}.to_json )} diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index fe4db72550b362356c657ae7a8b71f589d22d019..75d00a28d330c1b01c864a81d3edb6e41a2eaabd 100644 --- a/app/controllers/status_messages_controller.rb +++ b/app/controllers/status_messages_controller.rb @@ -26,9 +26,11 @@ class StatusMessagesController < ApplicationController raise 'MongoMapper failed to catch a failed save' unless @status_message.id @status_message.photos += photos unless photos.nil? + current_user.add_to_streams(post, params[:status_message][:aspect_ids]) current_user.dispatch_post(@status_message, :to => params[:status_message][:aspect_ids]) for photo in photos + current_user.add_to_streams(photo, params[:status_message][:aspect_ids]) current_user.dispatch_post(photo, :to => params[:status_message][:aspect_ids]) end diff --git a/app/models/user.rb b/app/models/user.rb index 09cb0162f528e18f9d06e5e81483387fab60675f..75fbc60c797bc1d326a266b8815837777c434a87 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -147,6 +147,7 @@ class User if post.save raise 'MongoMapper failed to catch a failed save' unless post.id + add_to_streams(post, opts[:to]) dispatch_post(post, :to => opts[:to]) end post @@ -163,10 +164,6 @@ class User def dispatch_post(post, opts = {}) aspect_ids = opts.delete(:to) - aspect_ids = prune_aspect_ids(aspect_ids) - self.raw_visible_posts << post - self.save - #socket post Rails.logger.info("event=dispatch user=#{diaspora_handle} post=#{post.id.to_s}") push_to_aspects(post, aspect_ids) @@ -199,14 +196,27 @@ class User end end - def prune_aspect_ids(aspect_ids) - return aspect_ids if aspect_ids == "all" - if aspect_ids.respond_to? :to_id - aspect_ids = [aspect_ids] + def add_to_streams(post, aspect_ids) + self.raw_visible_posts << post + self.save + + target_aspects = aspects_from_ids(aspect_ids) + target_aspects.each do |aspect| + aspect.posts << post + aspect.save end + end - aspect_ids.map!{ |x| x.to_id } - aspects.map{ |x| x.id } & aspect_ids + def aspects_from_ids(aspect_ids) + if aspect_ids == "all" || aspect_ids == :all + self.aspects + else + if aspect_ids.respond_to? :to_id + aspect_ids = [aspect_ids] + end + aspect_ids.map!{ |x| x.to_id } + aspects.all(:id.in => aspect_ids) + end end def push_to_aspects(post, aspect_ids) @@ -221,8 +231,6 @@ class User target_contacts = [] aspects.each { |aspect| - aspect.posts << post - aspect.save target_contacts = target_contacts | aspect.contacts } diff --git a/spec/models/user/posting_spec.rb b/spec/models/user/posting_spec.rb index 0626300516fc17b24b41f8e048083271f30c21cf..8c00057654c748c8a246b59d6b3084645e3d8c4b 100644 --- a/spec/models/user/posting_spec.rb +++ b/spec/models/user/posting_spec.rb @@ -16,9 +16,8 @@ describe User do let!(:service1) { s = Factory(:service, :provider => 'twitter'); user.services << s; s } let!(:service2) { s = Factory(:service, :provider => 'facebook'); user.services << s; s } - describe '#add_to_stream' do + describe '#add_to_streams' do before do - pending "not implemented" @params = {:message => "hey", :to => [aspect.id, aspect1.id]} @post = user.build_post(:status_message, @params) @post.save @@ -27,27 +26,30 @@ describe User do it 'saves post into visible post ids' do proc { - user.add_to_stream(@post, @aspect_ids) + user.add_to_streams(@post, @aspect_ids) }.should change(user.raw_visible_posts, :count).by(1) user.reload.raw_visible_posts.should include @post end it 'saves post into each aspect in aspect_ids' do - user.add_to_stream(@post, @aspect_ids) + user.add_to_streams(@post, @aspect_ids) aspect.reload.post_ids.should include @post.id aspect1.reload.post_ids.should include @post.id end end - describe '#prune_aspect_ids' do + describe '#aspects_from_ids' do it 'returns a list of all valid aspects a user can post to' do aspect_ids = Aspect.all.map(&:id) - user.prune_aspect_ids(aspect_ids).should =~ [aspect.id, aspect1.id] + user.aspects_from_ids(aspect_ids).should =~ [aspect, aspect1] end it "lets you post to your own aspects" do - user.prune_aspect_ids(aspect.id).should be_true - user.prune_aspect_ids(aspect1.id).should be_true + user.aspects_from_ids([aspect.id]).should == [aspect] + user.aspects_from_ids([aspect1.id]).should == [aspect1] + end + it 'removes aspects that are not yours' do + user.aspects_from_ids(aspect2.id).should == [] end end