[412c4bc] | 1 | # Copyright (c) 2010-2011, Diaspora Inc. This file is |
---|
| 2 | # licensed under the Affero General Public License version 3 or later. See |
---|
| 3 | # the COPYRIGHT file. |
---|
| 4 | |
---|
| 5 | class StatusMessagesController < ApplicationController |
---|
| 6 | before_action :authenticate_user! |
---|
| 7 | |
---|
| 8 | before_action :remove_getting_started, :only => [:create] |
---|
| 9 | |
---|
| 10 | respond_to :html, |
---|
| 11 | :mobile, |
---|
| 12 | :json |
---|
| 13 | |
---|
| 14 | layout 'application', only: :bookmarklet |
---|
| 15 | |
---|
| 16 | # Called when a user clicks "Mention" on a profile page |
---|
| 17 | # @param person_id [Integer] The id of the person to be mentioned |
---|
| 18 | def new |
---|
| 19 | if params[:person_id] && @person = Person.where(:id => params[:person_id]).first |
---|
| 20 | @aspect = :profile |
---|
| 21 | @contact = current_user.contact_for(@person) |
---|
| 22 | @aspects_with_person = [] |
---|
| 23 | if @contact |
---|
| 24 | @aspects_with_person = @contact.aspects |
---|
| 25 | @aspect_ids = @aspects_with_person.map{|x| x.id} |
---|
| 26 | gon.aspect_ids = @aspect_ids |
---|
| 27 | @contacts_of_contact = @contact.contacts |
---|
| 28 | render :layout => nil |
---|
| 29 | end |
---|
| 30 | elsif(request.format == :mobile) |
---|
| 31 | @aspect = :all |
---|
| 32 | @aspects = current_user.aspects |
---|
| 33 | @aspect_ids = @aspects.map{ |a| a.id } |
---|
| 34 | gon.aspect_ids = @aspect_ids |
---|
| 35 | else |
---|
| 36 | redirect_to stream_path |
---|
| 37 | end |
---|
| 38 | end |
---|
| 39 | |
---|
| 40 | def bookmarklet |
---|
| 41 | @aspects = current_user.aspects |
---|
| 42 | @aspect_ids = @aspects.map{|x| x.id} |
---|
| 43 | |
---|
| 44 | gon.preloads[:bookmarklet] = { |
---|
| 45 | content: params[:content], |
---|
| 46 | title: params[:title], |
---|
| 47 | url: params[:url], |
---|
| 48 | notes: params[:notes] |
---|
| 49 | } |
---|
| 50 | end |
---|
| 51 | |
---|
| 52 | def create |
---|
| 53 | params[:status_message][:aspect_ids] = [*params[:aspect_ids]] |
---|
| 54 | normalize_public_flag! |
---|
| 55 | services = [*params[:services]].compact |
---|
| 56 | |
---|
| 57 | @status_message = current_user.build_post(:status_message, params[:status_message]) |
---|
| 58 | @status_message.build_location(:address => params[:location_address], :coordinates => params[:location_coords]) if params[:location_address].present? |
---|
[aed07f8] | 59 | puts "STATUS MESSAGE PARAMS XXXXXXXXXXXXXXXXXXXXXXXX" |
---|
[412c4bc] | 60 | if params[:poll_question].present? |
---|
| 61 | @status_message.build_poll(:question => params[:poll_question]) |
---|
| 62 | [*params[:poll_answers]].each do |poll_answer| |
---|
| 63 | @status_message.poll.poll_answers.build(:answer => poll_answer) |
---|
| 64 | end |
---|
| 65 | end |
---|
[50de8e9] | 66 | |
---|
[52c3597] | 67 | # Create text field Responsable |
---|
[8a68681] | 68 | if params[:poll_responsables].present? |
---|
| 69 | [*params[:poll_responsables]].each do |poll_responsable| |
---|
| 70 | @status_message.poll.poll_responsables.build(text: poll_responsable) |
---|
| 71 | end |
---|
| 72 | end |
---|
| 73 | |
---|
[220994c] | 74 | # Create date field Fecha |
---|
| 75 | if params[:poll_fechas].present? |
---|
| 76 | [*params[:poll_fechas]].each do |poll_fecha| |
---|
| 77 | @status_message.poll.poll_fechas.build(lafecha: poll_fecha) |
---|
| 78 | end |
---|
| 79 | end |
---|
| 80 | |
---|
| 81 | # Create text field Hora de Inicio |
---|
| 82 | if params[:poll_horainicios].present? |
---|
| 83 | [*params[:poll_horainicios]].each do |poll_horainicio| |
---|
| 84 | @status_message.poll.poll_horainicios.build(text: poll_horainicio) |
---|
| 85 | end |
---|
| 86 | end |
---|
| 87 | |
---|
[101698b] | 88 | # Create text field Hora de finalización |
---|
| 89 | if params[:poll_horafinals].present? |
---|
| 90 | [*params[:poll_horafinals]].each do |poll_horafinal| |
---|
| 91 | @status_message.poll.poll_horafinals.build(text: poll_horafinal) |
---|
| 92 | end |
---|
| 93 | end |
---|
| 94 | |
---|
[5603b39] | 95 | # Create text field Asistentes |
---|
| 96 | if params[:poll_asistentes].present? |
---|
| 97 | [*params[:poll_asistentes]].each do |poll_asistente| |
---|
| 98 | @status_message.poll.poll_asistentes.build(text: poll_asistente) |
---|
| 99 | end |
---|
| 100 | end |
---|
| 101 | |
---|
[ca51505] | 102 | # Create text field Asunto |
---|
| 103 | if params[:poll_asuntos].present? |
---|
| 104 | [*params[:poll_asuntos]].each do |poll_asunto| |
---|
| 105 | @status_message.poll.poll_asuntos.build(text: poll_asunto) |
---|
| 106 | end |
---|
| 107 | end |
---|
| 108 | |
---|
[4769715] | 109 | # Create text field Puntos Tratados |
---|
| 110 | if params[:poll_puntos].present? |
---|
| 111 | [*params[:poll_puntos]].each do |poll_punto| |
---|
| 112 | @status_message.poll.poll_puntos.build(text: poll_punto) |
---|
| 113 | end |
---|
| 114 | end |
---|
| 115 | |
---|
[11a277a] | 116 | # Create text field Acuerdos |
---|
| 117 | if params[:poll_acuerdos].present? |
---|
| 118 | [*params[:poll_acuerdos]].each do |poll_acuerdo| |
---|
| 119 | @status_message.poll.poll_acuerdos.build(text: poll_acuerdo) |
---|
| 120 | end |
---|
| 121 | end |
---|
| 122 | |
---|
[412c4bc] | 123 | @status_message.attach_photos_by_ids(params[:photos]) |
---|
| 124 | |
---|
| 125 | if @status_message.save |
---|
| 126 | aspects = current_user.aspects_from_ids(destination_aspect_ids) |
---|
[14d95c0] | 127 | |
---|
| 128 | # Jorge Redondo Flames |
---|
[074f151] | 129 | @status_message.aspect_mentions(current_user.aspects_from_ids(params[:aspect_ids])) |
---|
| 130 | |
---|
[412c4bc] | 131 | current_user.add_to_streams(@status_message, aspects) |
---|
| 132 | receiving_services = Service.titles(services) |
---|
| 133 | |
---|
| 134 | current_user.dispatch_post(@status_message, :url => short_post_url(@status_message.guid), :service_types => receiving_services) |
---|
| 135 | |
---|
| 136 | #this is done implicitly, somewhere else, but it doesnt work, says max. :'( |
---|
| 137 | @status_message.photos.each do |photo| |
---|
| 138 | current_user.dispatch_post(photo) |
---|
| 139 | end |
---|
| 140 | |
---|
| 141 | current_user.participate!(@status_message) |
---|
| 142 | |
---|
| 143 | if coming_from_profile_page? && !own_profile_page? # if this is a post coming from a profile page |
---|
| 144 | flash[:notice] = successful_mention_message |
---|
| 145 | end |
---|
| 146 | |
---|
| 147 | respond_to do |format| |
---|
| 148 | format.html { redirect_to :back } |
---|
| 149 | format.mobile { redirect_to stream_path } |
---|
| 150 | format.json { render :json => PostPresenter.new(@status_message, current_user), :status => 201 } |
---|
| 151 | end |
---|
| 152 | else |
---|
| 153 | respond_to do |format| |
---|
| 154 | format.html { redirect_to :back } |
---|
| 155 | format.mobile { redirect_to stream_path } |
---|
| 156 | format.json { render :text => @status_message.errors.messages[:text].to_sentence, :status => 403 } |
---|
| 157 | end |
---|
| 158 | end |
---|
| 159 | end |
---|
| 160 | |
---|
| 161 | private |
---|
| 162 | |
---|
| 163 | def destination_aspect_ids |
---|
[074f151] | 164 | if params[:status_message][:public] || params[:status_message][:aspect_ids].first == "all_aspects" |
---|
| 165 | current_user.aspect_ids |
---|
| 166 | else |
---|
| 167 | params[:aspect_ids] |
---|
| 168 | end |
---|
[412c4bc] | 169 | end |
---|
| 170 | |
---|
| 171 | def successful_mention_message |
---|
| 172 | t('status_messages.create.success', :names => @status_message.mentioned_people_names) |
---|
| 173 | end |
---|
| 174 | |
---|
| 175 | def coming_from_profile_page? |
---|
| 176 | request.env['HTTP_REFERER'].include?("people") |
---|
| 177 | end |
---|
| 178 | |
---|
| 179 | def own_profile_page? |
---|
| 180 | request.env['HTTP_REFERER'].include?("/people/" + params[:status_message][:author][:guid].to_s) |
---|
| 181 | end |
---|
| 182 | |
---|
| 183 | def normalize_public_flag! |
---|
| 184 | # mobile || desktop conditions |
---|
| 185 | sm = params[:status_message] |
---|
| 186 | public_flag = (sm[:aspect_ids] && sm[:aspect_ids].first == 'public') || sm[:public] |
---|
| 187 | public_flag.to_s.match(/(true)|(on)/) ? public_flag = true : public_flag = false |
---|
[cd582d2] | 188 | ###### OJO ############ |
---|
| 189 | #### EXPERIMENTAL ###### |
---|
| 190 | public_flag = true |
---|
[074f151] | 191 | ############################ |
---|
[412c4bc] | 192 | params[:status_message][:public] = public_flag |
---|
| 193 | public_flag |
---|
| 194 | end |
---|
| 195 | |
---|
| 196 | def remove_getting_started |
---|
| 197 | current_user.disable_getting_started |
---|
| 198 | end |
---|
[52c3597] | 199 | end |
---|