]> git.openstreetmap.org Git - rails.git/blob - app/controllers/diary_entry_controller.rb
Change abilities based on upstream renamings
[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
7   authorize_resource
8
9   before_action :lookup_user, :only => [:show, :comments]
10   before_action :check_database_readable
11   before_action :check_database_writable, :only => [:new, :edit, :comment, :hide, :hidecomment, :subscribe, :unsubscribe]
12   before_action :allow_thirdparty_images, :only => [:new, :edit, :index, :show, :comments]
13
14   def new
15     @title = t "diary_entry.new.title"
16
17     if request.post?
18       @diary_entry = DiaryEntry.new(entry_params)
19       @diary_entry.user = current_user
20
21       if @diary_entry.save
22         default_lang = current_user.preferences.where(:k => "diary.default_language").first
23         if default_lang
24           default_lang.v = @diary_entry.language_code
25           default_lang.save!
26         else
27           current_user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
28         end
29
30         # Subscribe user to diary comments
31         @diary_entry.subscriptions.create(:user => current_user)
32
33         redirect_to :action => "index", :display_name => current_user.display_name
34       else
35         render :action => "edit"
36       end
37     else
38       default_lang = current_user.preferences.where(:k => "diary.default_language").first
39       lang_code = default_lang ? default_lang.v : current_user.preferred_language
40       @diary_entry = DiaryEntry.new(entry_params.merge(:language_code => lang_code))
41       set_map_location
42       render :action => "edit"
43     end
44   end
45
46   def edit
47     @title = t "diary_entry.edit.title"
48     @diary_entry = DiaryEntry.find(params[:id])
49
50     if current_user != @diary_entry.user
51       redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
52     elsif params[:diary_entry] && @diary_entry.update(entry_params)
53       redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
54     end
55
56     set_map_location
57   rescue ActiveRecord::RecordNotFound
58     render :action => "no_such_entry", :status => :not_found
59   end
60
61   def comment
62     @entry = DiaryEntry.find(params[:id])
63     @diary_comment = @entry.comments.build(comment_params)
64     @diary_comment.user = current_user
65     if @diary_comment.save
66
67       # Notify current subscribers of the new comment
68       @entry.subscribers.visible.each do |user|
69         Notifier.diary_comment_notification(@diary_comment, user).deliver_now if current_user != user
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 diary_entry_path(@entry.user, @entry)
76     else
77       render :action => "show"
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 diary_entry_path(diary_entry.user, diary_entry)
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 diary_entry_path(diary_entry.user, diary_entry)
99   rescue ActiveRecord::RecordNotFound
100     render :action => "no_such_entry", :status => :not_found
101   end
102
103   def index
104     if params[:display_name]
105       @user = User.active.find_by(:display_name => params[:display_name])
106
107       if @user
108         @title = t "diary_entry.index.user_title", :user => @user.display_name
109         @entries = @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.index.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.index.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.index.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.index.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 = t("diary_entry.feed.user.title", :user => user.display_name)
160         @description = t("diary_entry.feed.user.description", :user => user.display_name)
161         @link = url_for :controller => "diary_entry", :action => "index", :display_name => user.display_name, :host => SERVER_URL, :protocol => SERVER_PROTOCOL
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 = t("diary_entry.feed.language.title", :language_name => Language.find(params[:language]).english_name)
172         @description = t("diary_entry.feed.language.description", :language_name => Language.find(params[:language]).english_name)
173         @link = url_for :controller => "diary_entry", :action => "index", :language => params[:language], :host => SERVER_URL, :protocol => SERVER_PROTOCOL
174       else
175         @title = t("diary_entry.feed.all.title")
176         @description = t("diary_entry.feed.all.description")
177         @link = url_for :controller => "diary_entry", :action => "index", :host => SERVER_URL, :protocol => SERVER_PROTOCOL
178       end
179     end
180
181     @entries = @entries.visible.includes(:user).order("created_at DESC").limit(20)
182   end
183
184   def show
185     @entry = @user.diary_entries.visible.where(:id => params[:id]).first
186     if @entry
187       @title = t "diary_entry.show.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(:visible => false)
197     redirect_to :action => "index", :display_name => entry.user.display_name
198   end
199
200   def hidecomment
201     comment = DiaryComment.find(params[:comment])
202     comment.update(:visible => false)
203     redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
204   end
205
206   def comments
207     @comment_pages, @comments = paginate(:diary_comments,
208                                          :conditions => {
209                                            :user_id => @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   # This is required because, being a default-deny system, cancancan
220   # _cannot_ tell you the reason you were denied access; and so
221   # the "nice" feedback presenting next steps can't be gleaned from
222   # the exception
223   ##
224   # for the hide actions, require that the user is a administrator, or fill out
225   # a helpful error message and return them to the user page.
226   def deny_access(exception)
227     if current_user && exception.action.in?([:hide, :hidecomment])
228       flash[:error] = t("users.filter.not_an_administrator")
229       redirect_to :action => "show"
230     else
231       super
232     end
233   end
234
235   ##
236   # return permitted diary entry parameters
237   def entry_params
238     params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
239   rescue ActionController::ParameterMissing
240     ActionController::Parameters.new.permit(:title, :body, :language_code, :latitude, :longitude)
241   end
242
243   ##
244   # return permitted diary comment parameters
245   def comment_params
246     params.require(:diary_comment).permit(:body)
247   end
248
249   ##
250   # decide on a location for the diary entry map
251   def set_map_location
252     if @diary_entry.latitude && @diary_entry.longitude
253       @lon = @diary_entry.longitude
254       @lat = @diary_entry.latitude
255       @zoom = 12
256     elsif current_user.home_lat.nil? || current_user.home_lon.nil?
257       @lon = params[:lon] || -0.1
258       @lat = params[:lat] || 51.5
259       @zoom = params[:zoom] || 4
260     else
261       @lon = current_user.home_lon
262       @lat = current_user.home_lat
263       @zoom = 12
264     end
265   end
266 end