diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index 21c72711b2f1cca708004a75cdb91789b99cdc81..aecb585aa30ca6a679f60b8add62e5c7b5a2c77b 100644 --- a/app/controllers/status_messages_controller.rb +++ b/app/controllers/status_messages_controller.rb @@ -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 diff --git a/app/models/photo.rb b/app/models/photo.rb index 373bef0c6a9327963a05a3459fd0cf19646a1e11..c30e4d26edb7351a09b576d1cfb1fc82a856bc07 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -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 diff --git a/app/models/service.rb b/app/models/service.rb index c6b2f35f98b1dab67a8e0bcc7cc0512a4e8f4b66..9eab8442a7f90da5a432a12797079a80c71bc72d 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -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 diff --git a/app/models/status_message.rb b/app/models/status_message.rb index 0e47afe1aa6e64333e4b189de415a99a1ba2807d..96014b4528b414d145e668496def29f86ccf6ddf 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -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) diff --git a/app/models/user.rb b/app/models/user.rb index d8f51e279a503daf6fed847c03b1685a9a11581e..72aca80a852b58b6844b6acfeb28007a00453fd2 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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