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