]> git.openstreetmap.org Git - rails.git/blob - app/controllers/diary_entries_controller.rb
Replace page numbers with ID based selection for diary indexes
[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     @entry = @user.diary_entries.visible.where(:id => params[:id]).first
74     if @entry
75       @title = t ".title", :user => params[:display_name], :title => @entry.title
76       @comments = can?(:unhidecomment, DiaryEntry) ? @entry.comments : @entry.visible_comments
77     else
78       @title = t "diary_entries.no_such_entry.title", :id => params[:id]
79       render :action => "no_such_entry", :status => :not_found
80     end
81   end
82
83   def new
84     @title = t ".title"
85
86     default_lang = current_user.preferences.where(:k => "diary.default_language").first
87     lang_code = default_lang ? default_lang.v : current_user.preferred_language
88     @diary_entry = DiaryEntry.new(entry_params.merge(:language_code => lang_code))
89     set_map_location
90     render :action => "new"
91   end
92
93   def edit
94     @title = t ".title"
95     @diary_entry = DiaryEntry.find(params[:id])
96
97     redirect_to diary_entry_path(@diary_entry.user, @diary_entry) if current_user != @diary_entry.user
98
99     set_map_location
100   rescue ActiveRecord::RecordNotFound
101     render :action => "no_such_entry", :status => :not_found
102   end
103
104   def create
105     @title = t "diary_entries.new.title"
106
107     @diary_entry = DiaryEntry.new(entry_params)
108     @diary_entry.user = current_user
109
110     if @diary_entry.save
111       default_lang = current_user.preferences.where(:k => "diary.default_language").first
112       if default_lang
113         default_lang.v = @diary_entry.language_code
114         default_lang.save!
115       else
116         current_user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
117       end
118
119       # Subscribe user to diary comments
120       @diary_entry.subscriptions.create(:user => current_user)
121
122       redirect_to :action => "index", :display_name => current_user.display_name
123     else
124       render :action => "new"
125     end
126   end
127
128   def update
129     @title = t "diary_entries.edit.title"
130     @diary_entry = DiaryEntry.find(params[:id])
131
132     if current_user != @diary_entry.user ||
133        (params[:diary_entry] && @diary_entry.update(entry_params))
134       redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
135     else
136       set_map_location
137       render :action => "edit"
138     end
139   rescue ActiveRecord::RecordNotFound
140     render :action => "no_such_entry", :status => :not_found
141   end
142
143   def comment
144     @entry = DiaryEntry.find(params[:id])
145     @comments = @entry.visible_comments
146     @diary_comment = @entry.comments.build(comment_params)
147     @diary_comment.user = current_user
148     if @diary_comment.save
149
150       # Notify current subscribers of the new comment
151       @entry.subscribers.visible.each do |user|
152         UserMailer.diary_comment_notification(@diary_comment, user).deliver_later if current_user != user
153       end
154
155       # Add the commenter to the subscribers if necessary
156       @entry.subscriptions.create(:user => current_user) unless @entry.subscribers.exists?(current_user.id)
157
158       redirect_to diary_entry_path(@entry.user, @entry)
159     else
160       render :action => "show"
161     end
162   rescue ActiveRecord::RecordNotFound
163     render :action => "no_such_entry", :status => :not_found
164   end
165
166   def subscribe
167     diary_entry = DiaryEntry.find(params[:id])
168
169     diary_entry.subscriptions.create(:user => current_user) unless diary_entry.subscribers.exists?(current_user.id)
170
171     redirect_to diary_entry_path(diary_entry.user, diary_entry)
172   rescue ActiveRecord::RecordNotFound
173     render :action => "no_such_entry", :status => :not_found
174   end
175
176   def unsubscribe
177     diary_entry = DiaryEntry.find(params[:id])
178
179     diary_entry.subscriptions.where(:user => current_user).delete_all if diary_entry.subscribers.exists?(current_user.id)
180
181     redirect_to diary_entry_path(diary_entry.user, diary_entry)
182   rescue ActiveRecord::RecordNotFound
183     render :action => "no_such_entry", :status => :not_found
184   end
185
186   def rss
187     if params[:display_name]
188       user = User.active.find_by(:display_name => params[:display_name])
189
190       if user
191         @entries = user.diary_entries
192         @title = t("diary_entries.feed.user.title", :user => user.display_name)
193         @description = t("diary_entries.feed.user.description", :user => user.display_name)
194         @link = url_for :action => "index", :display_name => user.display_name, :host => Settings.server_url, :protocol => Settings.server_protocol
195       else
196         head :not_found
197         return
198       end
199     else
200       @entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
201
202       # Items can't be flagged as deleted in the RSS format.
203       # For the general feeds, allow a delay before publishing, to help spam fighting
204       @entries = @entries.where("created_at < :time", :time => Settings.diary_feed_delay.hours.ago)
205
206       if params[:language]
207         @entries = @entries.where(:language_code => params[:language])
208         @title = t("diary_entries.feed.language.title", :language_name => Language.find(params[:language]).english_name)
209         @description = t("diary_entries.feed.language.description", :language_name => Language.find(params[:language]).english_name)
210         @link = url_for :action => "index", :language => params[:language], :host => Settings.server_url, :protocol => Settings.server_protocol
211       else
212         @title = t("diary_entries.feed.all.title")
213         @description = t("diary_entries.feed.all.description")
214         @link = url_for :action => "index", :host => Settings.server_url, :protocol => Settings.server_protocol
215       end
216     end
217     @entries = @entries.visible.includes(:user).order("created_at DESC").limit(20)
218   end
219
220   def hide
221     entry = DiaryEntry.find(params[:id])
222     entry.update(:visible => false)
223     redirect_to :action => "index", :display_name => entry.user.display_name
224   end
225
226   def unhide
227     entry = DiaryEntry.find(params[:id])
228     entry.update(:visible => true)
229     redirect_to :action => "index", :display_name => entry.user.display_name
230   end
231
232   def hidecomment
233     comment = DiaryComment.find(params[:comment])
234     comment.update(:visible => false)
235     redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
236   end
237
238   def unhidecomment
239     comment = DiaryComment.find(params[:comment])
240     comment.update(:visible => true)
241     redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
242   end
243
244   def comments
245     @title = t ".title", :user => @user.display_name
246
247     conditions = { :user_id => @user }
248
249     conditions[:visible] = true unless can? :unhidecomment, DiaryEntry
250
251     @comment_pages, @comments = paginate(:diary_comments,
252                                          :conditions => conditions,
253                                          :order => "created_at DESC",
254                                          :per_page => 20)
255     @page = (params[:page] || 1).to_i
256   end
257
258   private
259
260   ##
261   # return permitted diary entry parameters
262   def entry_params
263     params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
264   rescue ActionController::ParameterMissing
265     ActionController::Parameters.new.permit(:title, :body, :language_code, :latitude, :longitude)
266   end
267
268   ##
269   # return permitted diary comment parameters
270   def comment_params
271     params.require(:diary_comment).permit(:body)
272   end
273
274   ##
275   # decide on a location for the diary entry map
276   def set_map_location
277     if @diary_entry.latitude && @diary_entry.longitude
278       @lon = @diary_entry.longitude
279       @lat = @diary_entry.latitude
280       @zoom = 12
281     elsif current_user.home_lat.nil? || current_user.home_lon.nil?
282       @lon = params[:lon] || -0.1
283       @lat = params[:lat] || 51.5
284       @zoom = params[:zoom] || 4
285     else
286       @lon = current_user.home_lon
287       @lat = current_user.home_lat
288       @zoom = 12
289     end
290   end
291 end