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