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