diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index 4eb40990a..9f49c32dd 100644 --- a/app/controllers/status_messages_controller.rb +++ b/app/controllers/status_messages_controller.rb @@ -36,37 +36,25 @@ class StatusMessagesController < ApplicationController def create params[:status_message][:aspect_ids] = params[:aspect_ids] - photos = Photo.where(:id => [*params[:photos]], :diaspora_handle => current_user.person.diaspora_handle) - - public_flag = params[:status_message][:public] - public_flag.to_s.match(/(true)|(on)/) ? public_flag = true : public_flag = false - params[:status_message][:public] = public_flag + normalize_public_flag! @status_message = current_user.build_post(:status_message, params[:status_message]) - aspects = current_user.aspects_from_ids(params[:aspect_ids]) - if !photos.empty? - @status_message.photos << photos - end if @status_message.save Rails.logger.info("event=create type=status_message chars=#{params[:status_message][:text].length}") - current_user.add_to_streams(@status_message, aspects) - receiving_services = params[:services].map{|s| current_user.services.where( - :type => "Services::"+s.titleize).first} if params[:services] - current_user.dispatch_post(@status_message, :url => post_url(@status_message), :services => receiving_services) + photos = Photo.where(:id => [*params[:photos]], :diaspora_handle => current_user.person.diaspora_handle) if !photos.empty? - for photo in photos - was_pending = photo.pending - if was_pending - current_user.add_to_streams(photo, aspects) - current_user.dispatch_post(photo) - end - end - photos.update_all(:pending => false, :public => public_flag) + @status_message.photos << photos end - if request.env['HTTP_REFERER'].include?("people") + aspects = current_user.aspects_from_ids(params[:aspect_ids]) + current_user.add_to_streams(@status_message, aspects) + receiving_services = current_user.services.where( :type => params[:services].map{|s| "Services::"+s.titleize}) if params[:services] + current_user.dispatch_post(@status_message, :url => post_url(@status_message), :services => receiving_services) + + + if request.env['HTTP_REFERER'].include?("people") # if this is a post coming from a profile page flash[:notice] = t('status_messages.create.success', :names => @status_message.mentions.includes(:person => :profile).map{ |mention| mention.person.name }.join(', ')) end @@ -76,9 +64,7 @@ class StatusMessagesController < ApplicationController format.mobile{ redirect_to root_url} end else - if !photos.empty? - photos.update_all(:status_message_guid => nil) - end + respond_to do |format| format.js { errors = @status_message.errors.full_messages.collect { |msg| msg.gsub(/^Text/, "") } @@ -89,22 +75,11 @@ class StatusMessagesController < ApplicationController end end - def show - @status_message = current_user.find_visible_post_by_id params[:id] - if @status_message - - # mark corresponding notification as read - if notification = Notification.where(:recipient_id => current_user.id, :target_id => @status_message.id).first - notification.unread = false - notification.save - end - - respond_with @status_message - else - Rails.logger.info(:event => :link_to_nonexistent_post, :ref => request.env['HTTP_REFERER'], :user_id => current_user.id, :post_id => params[:id]) - flash[:error] = I18n.t('status_messages.show.not_found') - redirect_to :back - end + def normalize_public_flag! + public_flag = params[:status_message][:public] + public_flag.to_s.match(/(true)|(on)/) ? public_flag = true : public_flag = false + params[:status_message][:public] = public_flag + public_flag end helper_method :comments_expanded diff --git a/app/models/status_message.rb b/app/models/status_message.rb index f59f4fe06..e2a6959d7 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -120,6 +120,18 @@ class StatusMessage < Post super(user_or_id, opts) end + def after_dispatch sender + unless self.photos.empty? + self.photos.update_all(:pending => false, :public => self.public) + for photo in self.photos + if photo.pending + sender.add_to_streams(photo, self.aspects) + sender.dispatch_post(photo) + end + end + end + end + protected def message_or_photos_present? diff --git a/app/models/user.rb b/app/models/user.rb index 008bfac03..e6df4fe04 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -225,7 +225,7 @@ class User < ActiveRecord::Base def mail_confirm_email return false if unconfirmed_email.blank? Resque.enqueue(Job::MailConfirmEmail, id) - true + true end ######### Posts and Such ############### @@ -382,7 +382,7 @@ class User < ActiveRecord::Base def guard_unconfirmed_email self.unconfirmed_email = nil if unconfirmed_email.blank? || unconfirmed_email == email - + if unconfirmed_email_changed? self.confirm_email_token = unconfirmed_email ? ActiveSupport::SecureRandom.hex(15) : nil end diff --git a/lib/diaspora/webhooks.rb b/lib/diaspora/webhooks.rb index a8fbf18f6..d342d8ef8 100644 --- a/lib/diaspora/webhooks.rb +++ b/lib/diaspora/webhooks.rb @@ -11,17 +11,23 @@ module Diaspora xml += "#{to_xml.to_s}" xml += "" end - + def x(input) input.to_s.to_xs end + # @abstract def subscribers(user) raise 'you must override subscribers in order to enable federation on this model' end + # @abstract def receive(user, person) raise 'you must override receive in order to enable federation on this model' end + + # @param [User] sender + def after_dispatch sender + end end end diff --git a/lib/postzord/dispatch.rb b/lib/postzord/dispatch.rb index 1204e0e65..75ba6db1f 100644 --- a/lib/postzord/dispatch.rb +++ b/lib/postzord/dispatch.rb @@ -35,6 +35,7 @@ class Postzord::Dispatch self.deliver_to_remote(remote_people) end self.deliver_to_services(opts[:url], opts[:services] || []) + @object.after_dispatch(@sender) end protected diff --git a/spec/controllers/status_messages_controller_spec.rb b/spec/controllers/status_messages_controller_spec.rb index ce6fa6485..b648ab187 100644 --- a/spec/controllers/status_messages_controller_spec.rb +++ b/spec/controllers/status_messages_controller_spec.rb @@ -106,11 +106,8 @@ describe StatusMessagesController do context 'with photos' do before do - fixture_filename = 'button.png' - fixture_name = File.join(File.dirname(__FILE__), '..', 'fixtures', fixture_filename) - - @photo1 = alice.build_post(:photo, :pending => true, :user_file=> File.open(fixture_name), :to => @aspect1.id) - @photo2 = alice.build_post(:photo, :pending => true, :user_file=> File.open(fixture_name), :to => @aspect1.id) + @photo1 = alice.build_post(:photo, :pending => true, :user_file=> File.open(photo_fixture_name), :to => @aspect1.id) + @photo2 = alice.build_post(:photo, :pending => true, :user_file=> File.open(photo_fixture_name), :to => @aspect1.id) @photo1.save! @photo2.save! @@ -123,9 +120,9 @@ describe StatusMessagesController do post :create, @hash response.should be_redirect end - it "dispatches all referenced photos" do - alice.should_receive(:dispatch_post).exactly(3).times + it "attaches all referenced photos" do post :create, @hash + assigns[:status_message].photos.map(&:id).should =~ [@photo1, @photo2].map(&:id) end it "sets the pending bit of referenced photos" do post :create, @hash diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb index ce2f6e557..d76181543 100644 --- a/spec/models/status_message_spec.rb +++ b/spec/models/status_message_spec.rb @@ -244,4 +244,26 @@ STR Post.find(post.id).youtube_titles.should == {video_id => CGI::escape(expected_title)} end end + describe '#after_dispatch' do + before do + @photos = [alice.build_post(:photo, :pending => true, :user_file=> File.open(photo_fixture_name)), + alice.build_post(:photo, :pending => true, :user_file=> File.open(photo_fixture_name))] + + @photos.each(&:save!) + + @status_message = alice.build_post(:status_message, :text => "the best pebble.") + @status_message.photos << @photos + + @status_message.save! + alice.add_to_streams(@status_message, alice.aspects) + end + it 'sets pending to false on any attached photos' do + @status_message.after_dispatch(alice) + @photos.all?{|p| p.reload.pending}.should be_false + end + it 'dispatches any attached photos' do + alice.should_receive(:dispatch_post).twice + @status_message.after_dispatch(alice) + end + end end diff --git a/spec/support/user_methods.rb b/spec/support/user_methods.rb index e65e7f923..ff9925b41 100644 --- a/spec/support/user_methods.rb +++ b/spec/support/user_methods.rb @@ -13,6 +13,7 @@ class User p = build_post(class_name, opts) if p.save! self.aspects.reload + aspects = self.aspects_from_ids(opts[:to]) add_to_streams(p, aspects) dispatch_post(p, :to => opts[:to])