]> git.openstreetmap.org Git - rails.git/blob - app/controllers/diary_entries_controller.rb
Move all settings to settings.yml
[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
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_entries.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_entries.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_later 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_entries.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_entries.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_entries.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_entries.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_entries.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_entries.feed.user.title", :user => user.display_name)
160         @description = t("diary_entries.feed.user.description", :user => user.display_name)
161         @link = url_for :action => "index", :display_name => user.display_name, :host => Settings.server_url, :protocol => Settings.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_entries.feed.language.title", :language_name => Language.find(params[:language]).english_name)
172         @description = t("diary_entries.feed.language.description", :language_name => Language.find(params[:language]).english_name)
173         @link = url_for :action => "index", :language => params[:language], :host => Settings.server_url, :protocol => Settings.server_protocol
174       else
175         @title = t("diary_entries.feed.all.title")
176         @description = t("diary_entries.feed.all.description")
177         @link = url_for :action => "index", :host => Settings.server_url, :protocol => Settings.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_entries.show.title", :user => params[:display_name], :title => @entry.title
188     else
189       @title = t "diary_entries.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   ##
220   # return permitted diary entry parameters
221   def entry_params
222     params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
223   rescue ActionController::ParameterMissing
224     ActionController::Parameters.new.permit(:title, :body, :language_code, :latitude, :longitude)
225   end
226
227   ##
228   # return permitted diary comment parameters
229   def comment_params
230     params.require(:diary_comment).permit(:body)
231   end
232
233   ##
234   # decide on a location for the diary entry map
235   def set_map_location
236     if @diary_entry.latitude && @diary_entry.longitude
237       @lon = @diary_entry.longitude
238       @lat = @diary_entry.latitude
239       @zoom = 12
240     elsif current_user.home_lat.nil? || current_user.home_lon.nil?
241       @lon = params[:lon] || -0.1
242       @lat = params[:lat] || 51.5
243       @zoom = params[:zoom] || 4
244     else
245       @lon = current_user.home_lon
246       @lat = current_user.home_lat
247       @zoom = 12
248     end
249   end
250 end