]> git.openstreetmap.org Git - rails.git/blob - app/controllers/note_controller.rb
Mass rename of "bugs" as "notes" for increased user friendliness
[rails.git] / app / controllers / note_controller.rb
1 class NoteController < ApplicationController
2
3   layout 'site', :only => [:mine]
4
5   before_filter :check_api_readable
6   before_filter :authorize_web, :only => [:create, :close, :update, :delete, :mine]
7   before_filter :check_api_writable, :only => [:create, :close, :update, :delete]
8   before_filter :require_moderator, :only => [:delete]
9   before_filter :set_locale, :only => [:mine]
10   after_filter :compress_output
11   around_filter :api_call_handle_error, :api_call_timeout
12
13   # Help methods for checking boundary sanity and area size
14   include MapBoundary
15
16   def list
17     # Figure out the bbox
18     bbox = params['bbox']
19
20     if bbox and bbox.count(',') == 3
21       bbox = bbox.split(',')
22       @min_lon, @min_lat, @max_lon, @max_lat = sanitise_boundaries(bbox)
23     else
24       #Fallback to old style, this is deprecated and should not be used
25       raise OSM::APIBadUserInput.new("No l was given") unless params['l']
26       raise OSM::APIBadUserInput.new("No r was given") unless params['r']
27       raise OSM::APIBadUserInput.new("No b was given") unless params['b']
28       raise OSM::APIBadUserInput.new("No t was given") unless params['t']
29
30       @min_lon = params['l'].to_f
31       @max_lon = params['r'].to_f
32       @min_lat = params['b'].to_f
33       @max_lat = params['t'].to_f
34     end
35
36     limit = getLimit
37     conditions = closedCondition
38         
39     check_boundaries(@min_lon, @min_lat, @max_lon, @max_lat, MAX_NOTE_REQUEST_AREA)
40
41     @notes = Note.find_by_area(@min_lat, @min_lon, @max_lat, @max_lon, :include => :comments, :order => "updated_at DESC", :limit => limit, :conditions => conditions)
42
43     respond_to do |format|
44       format.html {render :template => 'note/list.rjs', :content_type => "text/javascript"}
45       format.rss {render :template => 'note/list.rss'}
46       format.js
47       format.xml {render :template => 'note/list.xml'}
48       format.json { render :json => @notes.to_json(:methods => [:lat, :lon], :only => [:id, :status, :created_at], :include => { :comments => { :only => [:author_name, :created_at, :body]}}) }          
49       format.gpx {render :template => 'note/list.gpx'}
50     end
51   end
52
53   def create
54     raise OSM::APIBadUserInput.new("No lat was given") unless params['lat']
55     raise OSM::APIBadUserInput.new("No lon was given") unless params['lon']
56     raise OSM::APIBadUserInput.new("No text was given") unless params['text']
57
58     lon = params['lon'].to_f
59     lat = params['lat'].to_f
60     comment = params['text']
61
62     name = "NoName"
63     name = params['name'] if params['name']
64
65     #Include in a transaction to ensure that there is always a note_comment for every note
66     Note.transaction do
67       @note = Note.create_bug(lat, lon)
68
69       #TODO: move this into a helper function
70       begin
71         url = "http://nominatim.openstreetmap.org/reverse?lat=" + lat.to_s + "&lon=" + lon.to_s + "&zoom=16" 
72         response = REXML::Document.new(Net::HTTP.get(URI.parse(url))) 
73                 
74         if result = response.get_text("reversegeocode/result") 
75           @note.nearby_place = result.to_s 
76         else 
77           @note.nearby_place = "unknown"
78         end
79       rescue Exception => err
80         @note.nearby_place = "unknown"
81       end
82
83       @note.save
84
85       add_comment(@note, comment, name, "opened")
86     end
87  
88     render_ok
89   end
90
91   def update
92     raise OSM::APIBadUserInput.new("No id was given") unless params['id']
93     raise OSM::APIBadUserInput.new("No text was given") unless params['text']
94
95     name = "NoName"
96     name = params['name'] if params['name']
97         
98     id = params['id'].to_i
99
100     note = Note.find(id)
101     raise OSM::APINotFoundError unless note
102     raise OSM::APIAlreadyDeletedError unless note.visible
103
104     Note.transaction do
105       add_comment(note, params['text'], name, "commented")
106     end
107
108     render_ok
109   end
110
111   def close
112     raise OSM::APIBadUserInput.new("No id was given") unless params['id']
113         
114     id = params['id'].to_i
115     name = "NoName"
116     name = params['name'] if params['name']
117
118     note = Note.find_by_id(id)
119     raise OSM::APINotFoundError unless note
120     raise OSM::APIAlreadyDeletedError unless note.visible
121
122     Note.transaction do
123       note.close
124       add_comment(note, :nil, name, "closed")
125     end
126
127     render_ok
128   end 
129
130   def rss
131     limit = getLimit
132     conditions = closedCondition
133
134     # Figure out the bbox
135     bbox = params['bbox']
136
137     if bbox and bbox.count(',') == 3
138       bbox = bbox.split(',')
139       @min_lon, @min_lat, @max_lon, @max_lat = sanitise_boundaries(bbox)
140
141       check_boundaries(@min_lon, @min_lat, @max_lon, @max_lat, MAX_NOTE_REQUEST_AREA)
142
143       conditions = cond_merge conditions, [OSM.sql_for_area(@min_lat, @min_lon, @max_lat, @max_lon)]
144     end
145
146     @comments = NoteComment.find(:all, :limit => limit, :order => "created_at DESC", :joins => :note, :include => :note, :conditions => conditions)
147     render :template => 'note/rss.rss'
148   end
149
150   def read
151     @note = Note.find(params['id'])
152     raise OSM::APINotFoundError unless @note
153     raise OSM::APIAlreadyDeletedError unless @note.visible
154     
155     respond_to do |format|
156       format.rss
157       format.xml
158       format.json { render :json => @note.to_json(:methods => [:lat, :lon], :only => [:id, :status, :created_at], :include => { :comments => { :only => [:author_name, :created_at, :body]}}) }   
159       format.gpx
160     end
161   end
162
163   def delete
164     note = note.find(params['id'])
165     raise OSM::APINotFoundError unless note
166     raise OSM::APIAlreadyDeletedError unless note.visible
167
168     Note.transaction do
169       note.status = "hidden"
170       note.save
171       add_comment(note, :nil, name, "hidden")
172     end
173
174     render :text => "ok\n", :content_type => "text/html" 
175   end
176
177   def search
178     raise OSM::APIBadUserInput.new("No query string was given") unless params['q']
179     limit = getLimit
180     conditions = closedCondition
181     conditions = cond_merge conditions, ['note_comments.body ~ ?', params['q']]
182         
183     #TODO: There should be a better way to do this.   CloseConditions are ignored at the moment
184
185     @notes = Note.find(:all, :limit => limit, :order => "updated_at DESC", :joins => :comments, :include => :comments, :conditions => conditions).uniq
186     respond_to do |format|
187       format.html {render :template => 'note/list.rjs', :content_type => "text/javascript"}
188       format.rss {render :template => 'note/list.rss'}
189       format.js
190       format.xml {render :template => 'note/list.xml'}
191       format.json { render :json => @notes.to_json(:methods => [:lat, :lon], :only => [:id, :status, :created_at], :include => { :comments => { :only => [:author_name, :created_at, :body]}}) }
192       format.gpx {render :template => 'note/list.gpx'}
193     end
194   end
195
196   def mine
197     if params[:display_name] 
198       @user2 = User.find_by_display_name(params[:display_name], :conditions => { :status => ["active", "confirmed"] }) 
199  
200       if @user2  
201         if @user2.data_public? or @user2 == @user 
202           conditions = ['note_comments.author_id = ?', @user2.id] 
203         else 
204           conditions = ['false'] 
205         end 
206       else #if request.format == :html 
207         @title = t 'user.no_such_user.title' 
208         @not_found_user = params[:display_name] 
209         render :template => 'user/no_such_user', :status => :not_found 
210         return
211       end 
212     end
213
214     if @user2 
215       user_link = render_to_string :partial => "user", :object => @user2 
216     end 
217     
218     @title =  t 'note.mine.title', :user => @user2.display_name 
219     @heading =  t 'note.mine.heading', :user => @user2.display_name 
220     @description = t 'note.mine.description', :user => user_link
221     
222     @page = (params[:page] || 1).to_i 
223     @page_size = 10
224
225     @notes = Note.find(:all, 
226                        :include => [:comments, {:comments => :author}],
227                        :joins => :comments,
228                        :order => "updated_at DESC",
229                        :conditions => conditions,
230                        :offset => (@page - 1) * @page_size, 
231                        :limit => @page_size).uniq
232   end
233
234 private 
235   #------------------------------------------------------------ 
236   # utility functions below. 
237   #------------------------------------------------------------   
238  
239   ## 
240   # merge two conditions 
241   # TODO: this is a copy from changeset_controler.rb and should be factored out to share
242   def cond_merge(a, b) 
243     if a and b 
244       a_str = a.shift 
245       b_str = b.shift 
246       return [ a_str + " AND " + b_str ] + a + b 
247     elsif a  
248       return a 
249     else b 
250       return b 
251     end 
252   end 
253
254   def render_ok
255     output_js = :false
256     output_js = :true if params['format'] == "js"
257
258     if output_js == :true
259       render :text => "osbResponse();", :content_type => "text/javascript" 
260     else
261       render :text => "ok " + @note.id.to_s + "\n", :content_type => "text/html" if @note
262       render :text => "ok\n", :content_type => "text/html" unless @note
263     end
264   end
265
266   def getLimit
267     limit = 100
268     limit = params['limit'] if ((params['limit']) && (params['limit'].to_i < 10000) && (params['limit'].to_i > 0))
269     return limit
270   end
271
272   def closedCondition
273     closed_since = 7 unless params['closed']
274     closed_since = params['closed'].to_i if params['closed']
275         
276     if closed_since < 0
277       conditions = ["status != 'hidden'"]
278     elsif closed_since > 0
279       conditions = ["((status = 'open') OR ((status = 'closed' ) AND (closed_at > '" + (Time.now - closed_since.days).to_s + "')))"]
280     else
281       conditions = ["status = 'open'"]
282     end
283
284     return conditions
285   end
286
287   def add_comment(note, text, name, event) 
288     comment = note.comments.create(:visible => true, :event => event)
289     comment.body = text unless text == :nil
290     if @user  
291       comment.author_id = @user.id
292       comment.author_name = @user.display_name
293     else  
294       comment.author_ip = request.remote_ip
295       comment.author_name = name + " (a)"
296     end
297     comment.save 
298     note.save
299
300     sent_to = Set.new
301     note.comments.each do | cmt |
302       if cmt.author
303         unless sent_to.include?(cmt.author)
304           Notifier.deliver_note_comment_notification(note_comment, cmt.author) unless cmt.author == @user
305           sent_to.add(cmt.author)
306         end
307       end
308     end
309   end
310 end