Merge branch 'master' into moderation
[rails.git] / app / controllers / diary_entry_controller.rb
1 class DiaryEntryController < ApplicationController
2   layout "site", :except => :rss
3
4   before_action :authorize_web
5   before_action :set_locale
6   before_action :require_user, :only => [:new, :edit, :comment, :hide, :hidecomment, :subscribe, :unsubscribe]
7   before_action :lookup_this_user, :only => [:view, :comments]
8   before_action :check_database_readable
9   before_action :check_database_writable, :only => [:new, :edit, :comment, :hide, :hidecomment, :subscribe, :unsubscribe]
10   before_action :require_administrator, :only => [:hide, :hidecomment]
11
12   def new
13     @title = t "diary_entry.new.title"
14
15     if request.post?
16       @diary_entry = DiaryEntry.new(entry_params)
17       @diary_entry.user = current_user
18
19       if @diary_entry.save
20         default_lang = current_user.preferences.where(:k => "diary.default_language").first
21         if default_lang
22           default_lang.v = @diary_entry.language_code
23           default_lang.save!
24         else
25           current_user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
26         end
27
28         # Subscribe user to diary comments
29         @diary_entry.subscriptions.create(:user => current_user)
30
31         redirect_to :action => "list", :display_name => current_user.display_name
32       else
33         render :action => "edit"
34       end
35     else
36       default_lang = current_user.preferences.where(:k => "diary.default_language").first
37       lang_code = default_lang ? default_lang.v : current_user.preferred_language
38       @diary_entry = DiaryEntry.new(entry_params.merge(:language_code => lang_code))
39       set_map_location
40       render :action => "edit"
41     end
42   end
43
44   def edit
45     @title = t "diary_entry.edit.title"
46     @diary_entry = DiaryEntry.find(params[:id])
47
48     if current_user != @diary_entry.user
49       redirect_to :action => "view", :id => params[:id]
50     elsif params[:diary_entry] && @diary_entry.update_attributes(entry_params)
51       redirect_to :action => "view", :id => params[:id]
52     end
53
54     set_map_location
55   rescue ActiveRecord::RecordNotFound
56     render :action => "no_such_entry", :status => :not_found
57   end
58
59   def comment
60     @entry = DiaryEntry.find(params[:id])
61     @diary_comment = @entry.comments.build(comment_params)
62     @diary_comment.user = current_user
63     if @diary_comment.save
64
65       # Notify current subscribers of the new comment
66       @entry.subscribers.visible.each do |user|
67         if current_user != user
68           Notifier.diary_comment_notification(@diary_comment, user).deliver_now
69         end
70       end
71
72       # Add the commenter to the subscribers if necessary
73       @entry.subscriptions.create(:user => current_user) unless @entry.subscribers.exists?(current_user.id)
74
75       redirect_to :action => "view", :display_name => @entry.user.display_name, :id => @entry.id
76     else
77       render :action => "view"
78     end
79   rescue ActiveRecord::RecordNotFound
80     render :action => "no_such_entry", :status => :not_found
81   end
82
83   def subscribe
84     diary_entry = DiaryEntry.find(params[:id])
85
86     diary_entry.subscriptions.create(:user => current_user) unless diary_entry.subscribers.exists?(current_user.id)
87
88     redirect_to :action => "view", :display_name => diary_entry.user.display_name, :id => diary_entry.id
89   rescue ActiveRecord::RecordNotFound
90     render :action => "no_such_entry", :status => :not_found
91   end
92
93   def unsubscribe
94     diary_entry = DiaryEntry.find(params[:id])
95
96     diary_entry.subscriptions.where(:user => current_user).delete_all if diary_entry.subscribers.exists?(current_user.id)
97
98     redirect_to :action => "view", :display_name => diary_entry.user.display_name, :id => diary_entry.id
99   rescue ActiveRecord::RecordNotFound
100     render :action => "no_such_entry", :status => :not_found
101   end
102
103   def list
104     if params[:display_name]
105       @this_user = User.active.find_by(:display_name => params[:display_name])
106
107       if @this_user
108         @title = t "diary_entry.list.user_title", :user => @this_user.display_name
109         @entries = @this_user.diary_entries
110       else
111         render_unknown_user params[:display_name]
112         return
113       end
114     elsif params[:friends]
115       if current_user
116         @title = t "diary_entry.list.title_friends"
117         @entries = DiaryEntry.where(:user_id => current_user.friend_users)
118       else
119         require_user
120         return
121       end
122     elsif params[:nearby]
123       if current_user
124         @title = t "diary_entry.list.title_nearby"
125         @entries = DiaryEntry.where(:user_id => current_user.nearby)
126       else
127         require_user
128         return
129       end
130     else
131       @entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
132
133       if params[:language]
134         @title = t "diary_entry.list.in_language_title", :language => Language.find(params[:language]).english_name
135         @entries = @entries.where(:language_code => params[:language])
136       else
137         @title = t "diary_entry.list.title"
138       end
139     end
140
141     @params = params.permit(:display_name, :friends, :nearby, :language)
142
143     @page = (params[:page] || 1).to_i
144     @page_size = 20
145
146     @entries = @entries.visible
147     @entries = @entries.order("created_at DESC")
148     @entries = @entries.offset((@page - 1) * @page_size)
149     @entries = @entries.limit(@page_size)
150     @entries = @entries.includes(:user, :language)
151   end
152
153   def rss
154     if params[:display_name]
155       user = User.active.find_by(:display_name => params[:display_name])
156
157       if user
158         @entries = user.diary_entries
159         @title = I18n.t("diary_entry.feed.user.title", :user => user.display_name)
160         @description = I18n.t("diary_entry.feed.user.description", :user => user.display_name)
161         @link = "http://#{SERVER_URL}/user/#{user.display_name}/diary"
162       else
163         head :not_found
164         return
165       end
166     else
167       @entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
168
169       if params[:language]
170         @entries = @entries.where(:language_code => params[:language])
171         @title = I18n.t("diary_entry.feed.language.title", :language_name => Language.find(params[:language]).english_name)
172         @description = I18n.t("diary_entry.feed.language.description", :language_name => Language.find(params[:language]).english_name)
173         @link = "http://#{SERVER_URL}/diary/#{params[:language]}"
174       else
175         @title = I18n.t("diary_entry.feed.all.title")
176         @description = I18n.t("diary_entry.feed.all.description")
177         @link = "http://#{SERVER_URL}/diary"
178       end
179     end
180
181     @entries = @entries.visible.includes(:user).order("created_at DESC").limit(20)
182   end
183
184   def view
185     @entry = @this_user.diary_entries.visible.where(:id => params[:id]).first
186     if @entry
187       @title = t "diary_entry.view.title", :user => params[:display_name], :title => @entry.title
188       if params[:comment_id]
189         @reported_comment = DiaryComment.where(:id => params[:comment_id])
190       end
191     else
192       @title = t "diary_entry.no_such_entry.title", :id => params[:id]
193       render :action => "no_such_entry", :status => :not_found
194     end
195   end
196
197   def hide
198     entry = DiaryEntry.find(params[:id])
199     entry.update_attributes(:visible => false)
200     redirect_to :action => "list", :display_name => entry.user.display_name
201   end
202
203   def hidecomment
204     comment = DiaryComment.find(params[:comment])
205     comment.update_attributes(:visible => false)
206     redirect_to :action => "view", :display_name => comment.diary_entry.user.display_name, :id => comment.diary_entry.id
207   end
208
209   def comments
210     @comment_pages, @comments = paginate(:diary_comments,
211                                          :conditions => {
212                                            :user_id => @this_user,
213                                            :visible => true
214                                          },
215                                          :order => "created_at DESC",
216                                          :per_page => 20)
217     @page = (params[:page] || 1).to_i
218   end
219
220   private
221
222   ##
223   # return permitted diary entry parameters
224   def entry_params
225     params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
226   rescue ActionController::ParameterMissing
227     ActionController::Parameters.new.permit(:title, :body, :language_code, :latitude, :longitude)
228   end
229
230   ##
231   # return permitted diary comment parameters
232   def comment_params
233     params.require(:diary_comment).permit(:body)
234   end
235
236   ##
237   # require that the user is a administrator, or fill out a helpful error message
238   # and return them to the user page.
239   def require_administrator
240     unless current_user.administrator?
241       flash[:error] = t("user.filter.not_an_administrator")
242       redirect_to :action => "view"
243     end
244   end
245
246   ##
247   # decide on a location for the diary entry map
248   def set_map_location
249     if @diary_entry.latitude && @diary_entry.longitude
250       @lon = @diary_entry.longitude
251       @lat = @diary_entry.latitude
252       @zoom = 12
253     elsif current_user.home_lat.nil? || current_user.home_lon.nil?
254       @lon = params[:lon] || -0.1
255       @lat = params[:lat] || 51.5
256       @zoom = params[:zoom] || 4
257     else
258       @lon = current_user.home_lon
259       @lat = current_user.home_lat
260       @zoom = 12
261     end
262   end
263 end