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? |
---|
59 | puts "STATUS MESSAGE PARAMS XXXXXXXXXXXXXXXXXXXXXXXX" |
---|
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 |
---|
66 | |
---|
67 | # Create text area corta |
---|
68 | if params[:poll_textareas].present? |
---|
69 | puts "TEXT AREA CORTA... XXXXXXXXXXXXXXXXXXXXXXXX" |
---|
70 | puts params[:poll_textareas] |
---|
71 | [*params[:poll_textareas]].each do |poll_text_area| |
---|
72 | @status_message.poll.poll_text_areas.build(text: poll_text_area) |
---|
73 | end |
---|
74 | end |
---|
75 | |
---|
76 | # Create text area larga |
---|
77 | if params[:poll_textarealargas].present? |
---|
78 | puts "TEXT AREA LARGA... XXXXXXXXXXXXXXXXXXXXXXXX" |
---|
79 | puts params[:poll_textarealargas] |
---|
80 | [*params[:poll_textarealargas]].each do |poll_text_area_larga| |
---|
81 | @status_message.poll.poll_text_area_largas.build(text: poll_text_area_larga) |
---|
82 | end |
---|
83 | end |
---|
84 | |
---|
85 | # Create text field |
---|
86 | if params[:poll_textfields].present? |
---|
87 | puts "TEXT FIELD... XXXXXXXXXXXXXXXXXXXXXXXX" |
---|
88 | puts params[:poll_textfields] |
---|
89 | [*params[:poll_textfields]].each do |poll_text_field| |
---|
90 | @status_message.poll.poll_text_fields.build(text: poll_text_field) |
---|
91 | end |
---|
92 | end |
---|
93 | |
---|
94 | # Create Responsable |
---|
95 | if params[:poll_responsables].present? |
---|
96 | puts "RESPONSABLE... XXXXXXXXXXXXXXXXXXXXXXXX" |
---|
97 | puts params[:poll_responsables] |
---|
98 | [*params[:poll_responsables]].each do |poll_responsable| |
---|
99 | @status_message.poll.poll_responsables.build(text: poll_responsable) |
---|
100 | end |
---|
101 | end |
---|
102 | |
---|
103 | # Create seleccion field |
---|
104 | if params[:poll_seleccions].present? |
---|
105 | puts "SELECCION... XXXXXXXXXXXXXXXXXXXXXXXX" |
---|
106 | puts params[:poll_seleccions] |
---|
107 | [*params[:poll_seleccions]].each do |poll_seleccion| |
---|
108 | @status_message.poll.poll_seleccions.build(boolean: poll_seleccion) |
---|
109 | end |
---|
110 | end |
---|
111 | |
---|
112 | @status_message.attach_photos_by_ids(params[:photos]) |
---|
113 | |
---|
114 | if @status_message.save |
---|
115 | aspects = current_user.aspects_from_ids(destination_aspect_ids) |
---|
116 | |
---|
117 | # Jorge Redondo Flames |
---|
118 | @status_message.aspect_mentions(current_user.aspects_from_ids(params[:aspect_ids])) |
---|
119 | |
---|
120 | current_user.add_to_streams(@status_message, aspects) |
---|
121 | receiving_services = Service.titles(services) |
---|
122 | |
---|
123 | current_user.dispatch_post(@status_message, :url => short_post_url(@status_message.guid), :service_types => receiving_services) |
---|
124 | |
---|
125 | #this is done implicitly, somewhere else, but it doesnt work, says max. :'( |
---|
126 | @status_message.photos.each do |photo| |
---|
127 | current_user.dispatch_post(photo) |
---|
128 | end |
---|
129 | |
---|
130 | current_user.participate!(@status_message) |
---|
131 | |
---|
132 | if coming_from_profile_page? && !own_profile_page? # if this is a post coming from a profile page |
---|
133 | flash[:notice] = successful_mention_message |
---|
134 | end |
---|
135 | |
---|
136 | respond_to do |format| |
---|
137 | format.html { redirect_to :back } |
---|
138 | format.mobile { redirect_to stream_path } |
---|
139 | format.json { render :json => PostPresenter.new(@status_message, current_user), :status => 201 } |
---|
140 | end |
---|
141 | else |
---|
142 | respond_to do |format| |
---|
143 | format.html { redirect_to :back } |
---|
144 | format.mobile { redirect_to stream_path } |
---|
145 | format.json { render :text => @status_message.errors.messages[:text].to_sentence, :status => 403 } |
---|
146 | end |
---|
147 | end |
---|
148 | end |
---|
149 | |
---|
150 | private |
---|
151 | |
---|
152 | def destination_aspect_ids |
---|
153 | if params[:status_message][:public] || params[:status_message][:aspect_ids].first == "all_aspects" |
---|
154 | current_user.aspect_ids |
---|
155 | else |
---|
156 | params[:aspect_ids] |
---|
157 | end |
---|
158 | end |
---|
159 | |
---|
160 | def successful_mention_message |
---|
161 | t('status_messages.create.success', :names => @status_message.mentioned_people_names) |
---|
162 | end |
---|
163 | |
---|
164 | def coming_from_profile_page? |
---|
165 | request.env['HTTP_REFERER'].include?("people") |
---|
166 | end |
---|
167 | |
---|
168 | def own_profile_page? |
---|
169 | request.env['HTTP_REFERER'].include?("/people/" + params[:status_message][:author][:guid].to_s) |
---|
170 | end |
---|
171 | |
---|
172 | def normalize_public_flag! |
---|
173 | # mobile || desktop conditions |
---|
174 | sm = params[:status_message] |
---|
175 | public_flag = (sm[:aspect_ids] && sm[:aspect_ids].first == 'public') || sm[:public] |
---|
176 | public_flag.to_s.match(/(true)|(on)/) ? public_flag = true : public_flag = false |
---|
177 | ###### OJO ############ |
---|
178 | #### EXPERIMENTAL ###### |
---|
179 | public_flag = true |
---|
180 | ############################ |
---|
181 | params[:status_message][:public] = public_flag |
---|
182 | public_flag |
---|
183 | end |
---|
184 | |
---|
185 | def remove_getting_started |
---|
186 | current_user.disable_getting_started |
---|
187 | end |
---|
188 | end |
---|