Skip to content
Extraits de code Groupes Projets
Valider 20de3a56 rédigé par Raphael Sofaer's avatar Raphael Sofaer
Parcourir les fichiers

Refactor StatusMessagesController#create, move the photo dispatching into an after_dispatch hook

parent a58a06a0
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -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
......
......@@ -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?
......
......@@ -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
......
......@@ -11,17 +11,23 @@ module Diaspora
xml += "<post>#{to_xml.to_s}</post>"
xml += "</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
......@@ -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
......
......@@ -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
......
......@@ -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
......@@ -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])
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter