]> git.openstreetmap.org Git - rails.git/blob - app/controllers/diary_entry_controller.rb
Don't send note comment notifications to deleted users
[rails.git] / app / controllers / diary_entry_controller.rb
1 class DiaryEntryController < ApplicationController
2   layout "site", :except => :rss
3
4   before_action :authorize_web
5   before_action :set_locale
6   before_action :require_user, :only => [:new, :edit, :comment, :hide, :hidecomment]
7   before_action :lookup_this_user, :only => [:view, :comments]
8   before_action :check_database_readable
9   before_action :check_database_writable, :only => [:new, :edit]
10   before_action :require_administrator, :only => [:hide, :hidecomment]
11
12   def new
13     @title = t "diary_entry.new.title"
14
15     if params[:diary_entry]
16       @diary_entry = DiaryEntry.new(entry_params)
17       @diary_entry.user = @user
18
19       if @diary_entry.save
20         default_lang = @user.preferences.where(:k => "diary.default_language").first
21         if default_lang
22           default_lang.v = @diary_entry.language_code
23           default_lang.save!
24         else
25           @user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
26         end
27         redirect_to :controller => "diary_entry", :action => "list", :display_name => @user.display_name
28       else
29         render :action => "edit"
30       end
31     else
32       default_lang = @user.preferences.where(:k => "diary.default_language").first
33       lang_code = default_lang ? default_lang.v : @user.preferred_language
34       @diary_entry = DiaryEntry.new(:language_code => lang_code)
35       set_map_location
36       render :action => "edit"
37     end
38   end
39
40   def edit
41     @title = t "diary_entry.edit.title"
42     @diary_entry = DiaryEntry.find(params[:id])
43
44     if @user != @diary_entry.user
45       redirect_to :controller => "diary_entry", :action => "view", :id => params[:id]
46     elsif params[:diary_entry] && @diary_entry.update_attributes(entry_params)
47       redirect_to :controller => "diary_entry", :action => "view", :id => params[:id]
48     end
49
50     set_map_location
51   rescue ActiveRecord::RecordNotFound
52     render :action => "no_such_entry", :status => :not_found
53   end
54
55   def comment
56     @entry = DiaryEntry.find(params[:id])
57     @diary_comment = @entry.comments.build(comment_params)
58     @diary_comment.user = @user
59     if @diary_comment.save
60       if @diary_comment.user != @entry.user
61         Notifier.diary_comment_notification(@diary_comment).deliver_now
62       end
63
64       redirect_to :controller => "diary_entry", :action => "view", :display_name => @entry.user.display_name, :id => @entry.id
65     else
66       render :action => "view"
67     end
68   rescue ActiveRecord::RecordNotFound
69     render :action => "no_such_entry", :status => :not_found
70   end
71
72   def list
73     if params[:display_name]
74       @this_user = User.active.find_by_display_name(params[:display_name])
75
76       if @this_user
77         @title = t "diary_entry.list.user_title", :user => @this_user.display_name
78         @entries = @this_user.diary_entries
79       else
80         render_unknown_user params[:display_name]
81         return
82       end
83     elsif params[:friends]
84       if @user
85         @title = t "diary_entry.list.title_friends"
86         @entries = DiaryEntry.where(:user_id => @user.friend_users)
87       else
88         require_user
89         return
90       end
91     elsif params[:nearby]
92       if @user
93         @title = t "diary_entry.list.title_nearby"
94         @entries = DiaryEntry.where(:user_id => @user.nearby)
95       else
96         require_user
97         return
98       end
99     else
100       @entries = DiaryEntry.joins(:user).where(:users => { :status => %w(active confirmed) })
101
102       if params[:language]
103         @title = t "diary_entry.list.in_language_title", :language => Language.find(params[:language]).english_name
104         @entries = @entries.where(:language_code => params[:language])
105       else
106         @title = t "diary_entry.list.title"
107       end
108     end
109
110     @page = (params[:page] || 1).to_i
111     @page_size = 20
112
113     @entries = @entries.visible
114     @entries = @entries.order("created_at DESC")
115     @entries = @entries.offset((@page - 1) * @page_size)
116     @entries = @entries.limit(@page_size)
117     @entries = @entries.includes(:user, :language)
118   end
119
120   def rss
121     if params[:display_name]
122       user = User.active.find_by_display_name(params[:display_name])
123
124       if user
125         @entries = user.diary_entries
126         @title = I18n.t("diary_entry.feed.user.title", :user => user.display_name)
127         @description = I18n.t("diary_entry.feed.user.description", :user => user.display_name)
128         @link = "http://#{SERVER_URL}/user/#{user.display_name}/diary"
129       else
130         render :text => "", :status => :not_found
131         return
132       end
133     else
134       @entries = DiaryEntry.joins(:user).where(:users => { :status => %w(active confirmed) })
135
136       if params[:language]
137         @entries = @entries.where(:language_code => params[:language])
138         @title = I18n.t("diary_entry.feed.language.title", :language_name => Language.find(params[:language]).english_name)
139         @description = I18n.t("diary_entry.feed.language.description", :language_name => Language.find(params[:language]).english_name)
140         @link = "http://#{SERVER_URL}/diary/#{params[:language]}"
141       else
142         @title = I18n.t("diary_entry.feed.all.title")
143         @description = I18n.t("diary_entry.feed.all.description")
144         @link = "http://#{SERVER_URL}/diary"
145       end
146     end
147
148     @entries = @entries.visible.includes(:user).order("created_at DESC").limit(20)
149   end
150
151   def view
152     @entry = @this_user.diary_entries.visible.where(:id => params[:id]).first
153     if @entry
154       @title = t "diary_entry.view.title", :user => params[:display_name], :title => @entry.title
155     else
156       @title = t "diary_entry.no_such_entry.title", :id => params[:id]
157       render :action => "no_such_entry", :status => :not_found
158     end
159   end
160
161   def hide
162     entry = DiaryEntry.find(params[:id])
163     entry.update_attributes(:visible => false)
164     redirect_to :action => "list", :display_name => entry.user.display_name
165   end
166
167   def hidecomment
168     comment = DiaryComment.find(params[:comment])
169     comment.update_attributes(:visible => false)
170     redirect_to :action => "view", :display_name => comment.diary_entry.user.display_name, :id => comment.diary_entry.id
171   end
172
173   def comments
174     @comment_pages, @comments = paginate(:diary_comments,
175                                          :conditions => {
176                                            :user_id => @this_user,
177                                            :visible => true
178                                          },
179                                          :order => "created_at DESC",
180                                          :per_page => 20)
181     @page = (params[:page] || 1).to_i
182   end
183
184   private
185
186   ##
187   # return permitted diary entry parameters
188   def entry_params
189     params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
190   end
191
192   ##
193   # return permitted diary comment parameters
194   def comment_params
195     params.require(:diary_comment).permit(:body)
196   end
197
198   ##
199   # require that the user is a administrator, or fill out a helpful error message
200   # and return them to the user page.
201   def require_administrator
202     unless @user.administrator?
203       flash[:error] = t("user.filter.not_an_administrator")
204       redirect_to :controller => "diary_entry", :action => "view"
205     end
206   end
207
208   ##
209   # decide on a location for the diary entry map
210   def set_map_location
211     if @diary_entry.latitude && @diary_entry.longitude
212       @lon = @diary_entry.longitude
213       @lat = @diary_entry.latitude
214       @zoom = 12
215     elsif @user.home_lat.nil? || @user.home_lon.nil?
216       @lon = params[:lon] || -0.1
217       @lat = params[:lat] || 51.5
218       @zoom = params[:zoom] || 4
219     else
220       @lon = @user.home_lon
221       @lat = @user.home_lat
222       @zoom = 12
223     end
224   end
225 end