Skip to content
Extraits de code Groupes Projets
Valider 718c4fd3 rédigé par Maxwell Salzberg's avatar Maxwell Salzberg
Parcourir les fichiers

refactor status_message_controller#create to suck less. it still sucks

parent 0da5f18a
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -43,32 +43,21 @@ class StatusMessagesController < ApplicationController
def create
params[:status_message][:aspect_ids] = [*params[:aspect_ids]]
normalize_public_flag!
# ensure services is an array since .map doesn't work on a string for ruby 1.9
params[:services] = [params[:services]] if params[:services].is_a?(String)
services = [*params[:services]].compact
@status_message = current_user.build_post(:status_message, params[:status_message])
@status_message.attach_photos_by_ids(params[:photos])
photos = Photo.where(:id => [*params[:photos]], :diaspora_handle => current_user.person.diaspora_handle)
unless photos.empty?
@status_message.photos << photos
end
if @status_message.save
# always send to all aspects if public
if params[:status_message][:public] || params[:status_message][:aspect_ids].first == "all_aspects"
aspect_ids = current_user.aspects.map{|a| a.id}
else
aspect_ids = params[:aspect_ids]
end
aspects = current_user.aspects_from_ids(aspect_ids)
aspects = current_user.aspects_from_ids(destination_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]
receiving_services = current_user.services.where(:type => Service.titles(services))
current_user.dispatch_post(@status_message, :url => short_post_url(@status_message.guid), :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(', '))
if coming_from_profile_page? # if this is a post coming from a profile page
flash[:notice] = successful_mention_message
end
respond_to do |format|
......@@ -77,10 +66,6 @@ class StatusMessagesController < ApplicationController
format.json{ render :json => @status_message.as_api_response(:backbone), :status => 201 }
end
else
unless photos.empty?
photos.update_all(:status_message_guid => nil)
end
respond_to do |format|
format.json { render :nothing, :status => 403 }
format.html { redirect_to :back }
......@@ -88,23 +73,32 @@ class StatusMessagesController < ApplicationController
end
end
def destination_aspect_ids
if params[:status_message][:public] || params[:status_message][:aspect_ids].first == "all_aspects"
current_user.aspect_ids
else
params[:aspect_ids]
end
end
def successful_mention_message
t('status_messages.create.success', :names => @status_message.mentioned_people_names)
end
def coming_from_profile_page?
request.env['HTTP_REFERER'].include?("people")
end
def normalize_public_flag!
# mobile || desktop conditions
public_flag = (params[:status_message][:aspect_ids] && params[:status_message][:aspect_ids].first == 'public') || params[:status_message][:public]
sm = params[:status_message]
public_flag = (sm[:aspect_ids] && sm[:aspect_ids].first == 'public') || sm[: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
def comments_expanded
true
end
def remove_getting_started
if current_user.getting_started == true
current_user.update_attributes(:getting_started => false)
end
true
current_user.disable_getting_started
end
end
......@@ -8,6 +8,7 @@ class Photo < ActiveRecord::Base
include Diaspora::Commentable
include Diaspora::Shareable
# NOTE API V1 to be extracted
acts_as_api
api_accessible :backbone do |t|
......@@ -32,6 +33,7 @@ class Photo < ActiveRecord::Base
xml_attr :status_message_guid
belongs_to :status_message, :foreign_key => :status_message_guid, :primary_key => :guid
validates_associated :status_message
attr_accessible :text, :pending
validate :ownership_of_status_message
......
......@@ -9,6 +9,10 @@ class Service < ActiveRecord::Base
validates_uniqueness_of :uid, :scope => :type
has_many :service_users, :dependent => :destroy
def self.titles(service_strings)
service_strings.map{|s| "Services::#{s.titleize}"}
end
def public_message(post, length, url = "")
url = "" if post.respond_to?(:photos) && post.photos.count == 0
space_for_url = url.blank? ? 0 : (url.length + 1)
......@@ -17,6 +21,7 @@ class Service < ActiveRecord::Base
return truncated
end
def profile_photo_url
nil
end
......
......@@ -66,6 +66,12 @@ class StatusMessage < Post
write_attribute(:text, text)
end
def attach_photos_by_ids(photo_ids)
return [] unless photo_ids.present?
self.photos << Photo.where(:id => photo_ids, :author_id => self.author_id).all
end
def nsfw?
self.raw_message.match(/#nsfw/i)
end
......@@ -104,6 +110,10 @@ class StatusMessage < Post
end
end
def mentioned_people_names
self.mentioned_people.map(&:name).join(', ')
end
def create_mentions
mentioned_people_from_string.each do |person|
self.mentions.create(:person => person)
......
......@@ -191,6 +191,10 @@ class User < ActiveRecord::Base
end
end
def disable_getting_started
self.update_attribute(:getting_started, false) if self.getting_started?
end
def set_current_language
self.language = I18n.locale.to_s if self.language.blank?
end
......
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