]> git.openstreetmap.org Git - rails.git/blob - app/controllers/diary_entry_controller.rb
Merge remote-tracking branch 'upstream/pull/1659'
[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     else
189       @title = t "diary_entry.no_such_entry.title", :id => params[:id]
190       render :action => "no_such_entry", :status => :not_found
191     end
192   end
193
194   def hide
195     entry = DiaryEntry.find(params[:id])
196     entry.update_attributes(:visible => false)
197     redirect_to :action => "list", :display_name => entry.user.display_name
198   end
199
200   def hidecomment
201     comment = DiaryComment.find(params[:comment])
202     comment.update_attributes(:visible => false)
203     redirect_to :action => "view", :display_name => comment.diary_entry.user.display_name, :id => comment.diary_entry.id
204   end
205
206   def comments
207     @comment_pages, @comments = paginate(:diary_comments,
208                                          :conditions => {
209                                            :user_id => @this_user,
210                                            :visible => true
211                                          },
212                                          :order => "created_at DESC",
213                                          :per_page => 20)
214     @page = (params[:page] || 1).to_i
215   end
216
217   private
218
219   ##
220   # return permitted diary entry parameters
221   def entry_params
222     params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
223   rescue ActionController::ParameterMissing
224     ActionController::Parameters.new.permit(:title, :body, :language_code, :latitude, :longitude)
225   end
226
227   ##
228   # return permitted diary comment parameters
229   def comment_params
230     params.require(:diary_comment).permit(:body)
231   end
232
233   ##
234   # require that the user is a administrator, or fill out a helpful error message
235   # and return them to the user page.
236   def require_administrator
237     unless current_user.administrator?
238       flash[:error] = t("user.filter.not_an_administrator")
239       redirect_to :action => "view"
240     end
241   end
242
243   ##
244   # decide on a location for the diary entry map
245   def set_map_location
246     if @diary_entry.latitude && @diary_entry.longitude
247       @lon = @diary_entry.longitude
248       @lat = @diary_entry.latitude
249       @zoom = 12
250     elsif current_user.home_lat.nil? || current_user.home_lon.nil?
251       @lon = params[:lon] || -0.1
252       @lat = params[:lat] || 51.5
253       @zoom = params[:zoom] || 4
254     else
255       @lon = current_user.home_lon
256       @lat = current_user.home_lat
257       @zoom = 12
258     end
259   end
260 end