]> git.openstreetmap.org Git - rails.git/blob - app/controllers/diary_entries_controller.rb
Exchange focus between home location delete/undelete buttons
[rails.git] / app / controllers / diary_entries_controller.rb
1 class DiaryEntriesController < ApplicationController
2   layout "site", :except => :rss
3
4   before_action :authorize_web
5   before_action :set_locale
6   before_action :check_database_readable
7
8   authorize_resource
9
10   before_action :lookup_user, :only => [:show, :comments]
11   before_action :check_database_writable, :only => [:new, :create, :edit, :update, :comment, :hide, :hidecomment, :subscribe, :unsubscribe]
12   before_action :allow_thirdparty_images, :only => [:new, :create, :edit, :update, :index, :show, :comments]
13
14   def index
15     if params[:display_name]
16       @user = User.active.find_by(:display_name => params[:display_name])
17
18       if @user
19         @title = t ".user_title", :user => @user.display_name
20         entries = @user.diary_entries
21       else
22         render_unknown_user params[:display_name]
23         return
24       end
25     elsif params[:friends]
26       if current_user
27         @title = t ".title_friends"
28         entries = DiaryEntry.where(:user_id => current_user.friends)
29       else
30         require_user
31         return
32       end
33     elsif params[:nearby]
34       if current_user
35         @title = t ".title_nearby"
36         entries = DiaryEntry.where(:user_id => current_user.nearby)
37       else
38         require_user
39         return
40       end
41     else
42       entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
43
44       if params[:language]
45         @title = t ".in_language_title", :language => Language.find(params[:language]).english_name
46         entries = entries.where(:language_code => params[:language])
47       else
48         @title = t ".title"
49       end
50     end
51
52     entries = entries.visible unless can? :unhide, DiaryEntry
53
54     @params = params.permit(:display_name, :friends, :nearby, :language)
55
56     @entries = if params[:before]
57                  entries.where("diary_entries.id < ?", params[:before]).order(:id => :desc)
58                elsif params[:after]
59                  entries.where("diary_entries.id > ?", params[:after]).order(:id => :asc)
60                else
61                  entries.order(:id => :desc)
62                end
63
64     @entries = @entries.limit(20)
65     @entries = @entries.includes(:user, :language)
66     @entries = @entries.sort.reverse
67
68     @newer_entries = @entries.count.positive? && entries.exists?(["diary_entries.id > ?", @entries.first.id])
69     @older_entries = @entries.count.positive? && entries.exists?(["diary_entries.id < ?", @entries.last.id])
70   end
71
72   def show
73     entries = @user.diary_entries
74     entries = entries.visible unless can? :unhide, DiaryEntry
75     @entry = entries.where(:id => params[:id]).first
76     if @entry
77       @title = t ".title", :user => params[:display_name], :title => @entry.title
78       @comments = can?(:unhidecomment, DiaryEntry) ? @entry.comments : @entry.visible_comments
79     else
80       @title = t "diary_entries.no_such_entry.title", :id => params[:id]
81       render :action => "no_such_entry", :status => :not_found
82     end
83   end
84
85   def new
86     @title = t ".title"
87
88     default_lang = current_user.preferences.where(:k => "diary.default_language").first
89     lang_code = default_lang ? default_lang.v : current_user.preferred_language
90     @diary_entry = DiaryEntry.new(entry_params.merge(:language_code => lang_code))
91     set_map_location
92     render :action => "new"
93   end
94
95   def edit
96     @title = t ".title"
97     @diary_entry = DiaryEntry.find(params[:id])
98
99     redirect_to diary_entry_path(@diary_entry.user, @diary_entry) if current_user != @diary_entry.user
100
101     set_map_location
102   rescue ActiveRecord::RecordNotFound
103     render :action => "no_such_entry", :status => :not_found
104   end
105
106   def create
107     @title = t "diary_entries.new.title"
108
109     @diary_entry = DiaryEntry.new(entry_params)
110     @diary_entry.user = current_user
111
112     if @diary_entry.save
113       default_lang = current_user.preferences.where(:k => "diary.default_language").first
114       if default_lang
115         default_lang.v = @diary_entry.language_code
116         default_lang.save!
117       else
118         current_user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
119       end
120
121       # Subscribe user to diary comments
122       @diary_entry.subscriptions.create(:user => current_user)
123
124       redirect_to :action => "index", :display_name => current_user.display_name
125     else
126       render :action => "new"
127     end
128   end
129
130   def update
131     @title = t "diary_entries.edit.title"
132     @diary_entry = DiaryEntry.find(params[:id])
133
134     if current_user != @diary_entry.user ||
135        (params[:diary_entry] && @diary_entry.update(entry_params))
136       redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
137     else
138       set_map_location
139       render :action => "edit"
140     end
141   rescue ActiveRecord::RecordNotFound
142     render :action => "no_such_entry", :status => :not_found
143   end
144
145   def comment
146     @entry = DiaryEntry.find(params[:id])
147     @comments = @entry.visible_comments
148     @diary_comment = @entry.comments.build(comment_params)
149     @diary_comment.user = current_user
150     if @diary_comment.save
151
152       # Notify current subscribers of the new comment
153       @entry.subscribers.visible.each do |user|
154         UserMailer.diary_comment_notification(@diary_comment, user).deliver_later if current_user != user
155       end
156
157       # Add the commenter to the subscribers if necessary
158       @entry.subscriptions.create(:user => current_user) unless @entry.subscribers.exists?(current_user.id)
159
160       redirect_to diary_entry_path(@entry.user, @entry)
161     else
162       render :action => "show"
163     end
164   rescue ActiveRecord::RecordNotFound
165     render :action => "no_such_entry", :status => :not_found
166   end
167
168   def subscribe
169     diary_entry = DiaryEntry.find(params[:id])
170
171     diary_entry.subscriptions.create(:user => current_user) unless diary_entry.subscribers.exists?(current_user.id)
172
173     redirect_to diary_entry_path(diary_entry.user, diary_entry)
174   rescue ActiveRecord::RecordNotFound
175     render :action => "no_such_entry", :status => :not_found
176   end
177
178   def unsubscribe
179     diary_entry = DiaryEntry.find(params[:id])
180
181     diary_entry.subscriptions.where(:user => current_user).delete_all if diary_entry.subscribers.exists?(current_user.id)
182
183     redirect_to diary_entry_path(diary_entry.user, diary_entry)
184   rescue ActiveRecord::RecordNotFound
185     render :action => "no_such_entry", :status => :not_found
186   end
187
188   def rss
189     if params[:display_name]
190       user = User.active.find_by(:display_name => params[:display_name])
191
192       if user
193         @entries = user.diary_entries
194         @title = t("diary_entries.feed.user.title", :user => user.display_name)
195         @description = t("diary_entries.feed.user.description", :user => user.display_name)
196         @link = url_for :action => "index", :display_name => user.display_name, :host => Settings.server_url, :protocol => Settings.server_protocol
197       else
198         head :not_found
199         return
200       end
201     else
202       @entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
203
204       # Items can't be flagged as deleted in the RSS format.
205       # For the general feeds, allow a delay before publishing, to help spam fighting
206       @entries = @entries.where("created_at < :time", :time => Settings.diary_feed_delay.hours.ago)
207
208       if params[:language]
209         @entries = @entries.where(:language_code => params[:language])
210         @title = t("diary_entries.feed.language.title", :language_name => Language.find(params[:language]).english_name)
211         @description = t("diary_entries.feed.language.description", :language_name => Language.find(params[:language]).english_name)
212         @link = url_for :action => "index", :language => params[:language], :host => Settings.server_url, :protocol => Settings.server_protocol
213       else
214         @title = t("diary_entries.feed.all.title")
215         @description = t("diary_entries.feed.all.description")
216         @link = url_for :action => "index", :host => Settings.server_url, :protocol => Settings.server_protocol
217       end
218     end
219     @entries = @entries.visible.includes(:user).order("created_at DESC").limit(20)
220   end
221
222   def hide
223     entry = DiaryEntry.find(params[:id])
224     entry.update(:visible => false)
225     redirect_to :action => "index", :display_name => entry.user.display_name
226   end
227
228   def unhide
229     entry = DiaryEntry.find(params[:id])
230     entry.update(:visible => true)
231     redirect_to :action => "index", :display_name => entry.user.display_name
232   end
233
234   def hidecomment
235     comment = DiaryComment.find(params[:comment])
236     comment.update(:visible => false)
237     redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
238   end
239
240   def unhidecomment
241     comment = DiaryComment.find(params[:comment])
242     comment.update(:visible => true)
243     redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
244   end
245
246   def comments
247     @title = t ".title", :user => @user.display_name
248
249     conditions = { :user_id => @user }
250
251     conditions[:visible] = true unless can? :unhidecomment, DiaryEntry
252
253     @comment_pages, @comments = paginate(:diary_comments,
254                                          :conditions => conditions,
255                                          :order => "created_at DESC",
256                                          :per_page => 20)
257     @page = (params[:page] || 1).to_i
258   end
259
260   private
261
262   ##
263   # return permitted diary entry parameters
264   def entry_params
265     params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
266   rescue ActionController::ParameterMissing
267     ActionController::Parameters.new.permit(:title, :body, :language_code, :latitude, :longitude)
268   end
269
270   ##
271   # return permitted diary comment parameters
272   def comment_params
273     params.require(:diary_comment).permit(:body)
274   end
275
276   ##
277   # decide on a location for the diary entry map
278   def set_map_location
279     if @diary_entry.latitude && @diary_entry.longitude
280       @lon = @diary_entry.longitude
281       @lat = @diary_entry.latitude
282       @zoom = 12
283     elsif !current_user.has_home?
284       @lon = params[:lon] || -0.1
285       @lat = params[:lat] || 51.5
286       @zoom = params[:zoom] || 4
287     else
288       @lon = current_user.home_lon
289       @lat = current_user.home_lat
290       @zoom = 12
291     end
292   end
293 end