]> git.openstreetmap.org Git - rails.git/blob - app/controllers/map_bugs_controller.rb
f4105929ffe06a3ef4e3f80a266e739da08eeeb5
[rails.git] / app / controllers / map_bugs_controller.rb
1 class MapBugsController < ApplicationController
2
3   layout 'site', :only => [:my_bugs]
4
5   before_filter :check_api_readable
6   before_filter :authorize_web, :only => [:add_bug, :close_bug, :edit_bug, :delete, :my_bugs]
7   before_filter :check_api_writable, :only => [:add_bug, :close_bug, :edit_bug, :delete]
8   before_filter :require_moderator, :only => [:delete]
9   before_filter :set_locale, :only => [:my_bugs]
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 get_bugs
17
18     # Figure out the bbox
19     bbox = params['bbox']
20
21     if bbox and bbox.count(',') == 3
22       bbox = bbox.split(',')
23       @min_lon, @min_lat, @max_lon, @max_lat = sanitise_boundaries(bbox)
24     else
25       #Fallback to old style, this is deprecated and should not be used
26       raise OSM::APIBadUserInput.new("No l was given") unless params['l']
27       raise OSM::APIBadUserInput.new("No r was given") unless params['r']
28       raise OSM::APIBadUserInput.new("No b was given") unless params['b']
29       raise OSM::APIBadUserInput.new("No t was given") unless params['t']
30
31       @min_lon = params['l'].to_f
32       @max_lon = params['r'].to_f
33       @min_lat = params['b'].to_f
34       @max_lat = params['t'].to_f
35     end
36
37     limit = getLimit
38     conditions = closedCondition
39         
40     check_boundaries(@min_lon, @min_lat, @max_lon, @max_lat, MAX_BUG_REQUEST_AREA)
41
42     @bugs = MapBug.find_by_area(@min_lat, @min_lon, @max_lat, @max_lon, :include => :comments, :order => "last_changed DESC", :limit => limit, :conditions => conditions)
43
44     respond_to do |format|
45       format.html {render :template => 'map_bugs/get_bugs.js', :content_type => "text/javascript"}
46       format.rss {render :template => 'map_bugs/get_bugs.rss'}
47       format.js
48       format.xml {render :template => 'map_bugs/get_bugs.xml'}
49       format.json { render :json => @bugs.to_json(:methods => [:lat, :lon], :only => [:id, :status, :date_created], :include => { :comments => { :only => [:commenter_name, :date_created, :comment]}}) }         
50       format.gpx {render :template => 'map_bugs/get_bugs.gpx'}
51     end
52   end
53
54   def add_bug
55     raise OSM::APIBadUserInput.new("No lat was given") unless params['lat']
56     raise OSM::APIBadUserInput.new("No lon was given") unless params['lon']
57     raise OSM::APIBadUserInput.new("No text was given") unless params['text']
58
59     lon = params['lon'].to_f
60     lat = params['lat'].to_f
61     comment = params['text']
62
63     name = "NoName"
64     name = params['name'] if params['name']
65
66     #Include in a transaction to ensure that there is always a map_bug_comment for every map_bug
67     MapBug.transaction do
68       @bug = MapBug.create_bug(lat, lon)
69
70       #TODO: move this into a helper function
71       begin
72         url = "http://nominatim.openstreetmap.org/reverse?lat=" + lat.to_s + "&lon=" + lon.to_s + "&zoom=16" 
73         response = REXML::Document.new(Net::HTTP.get(URI.parse(url))) 
74                 
75         if result = response.get_text("reversegeocode/result") 
76           @bug.nearby_place = result.to_s 
77         else 
78           @bug.nearby_place = "unknown"
79         end
80       rescue Exception => err
81         @bug.nearby_place = "unknown"
82       end
83           
84       @bug.save
85       add_comment(@bug, comment, name,"opened")
86     end
87  
88     render_ok
89   end
90
91   def edit_bug
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     bug = MapBug.find_by_id(id)
101     raise OSM::APINotFoundError unless bug
102     raise OSM::APIAlreadyDeletedError unless bug.visible
103
104     MapBug.transaction do
105       bug_comment = add_comment(bug, params['text'], name,"commented")
106     end
107
108     render_ok
109   end
110
111   def close_bug
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     bug = MapBug.find_by_id(id)
119     raise OSM::APINotFoundError unless bug
120     raise OSM::APIAlreadyDeletedError unless bug.visible
121
122     MapBug.transaction do
123       bug.close_bug
124       add_comment(bug,: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_BUG_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 = MapBugComment.find(:all, :limit => limit, :order => "date_created DESC", :joins => :map_bug, :include => :map_bug, :conditions => conditions)
147     render :template => 'map_bugs/rss.rss'
148   end
149
150   def read
151     @bug = MapBug.find(params['id'])
152     raise OSM::APINotFoundError unless @bug
153     raise OSM::APIAlreadyDeletedError unless @bug.visible
154     
155     respond_to do |format|
156       format.rss
157       format.xml
158       format.json { render :json => @bug.to_json(:methods => [:lat, :lon], :only => [:id, :status, :date_created], :include => { :comments => { :only => [:commenter_name, :date_created, :comment]}}) }          
159       format.gpx
160     end
161   end
162
163   def delete
164     bug = MapBug.find(params['id'])
165     raise OSM::APINotFoundError unless @bug
166     raise OSM::APIAlreadyDeletedError unless @bug.visible
167
168     MapBug.transaction do
169       bug.status = "hidden"
170       bug.save
171       add_comment(bug,: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, ['map_bug_comment.comment ~ ?', params['q']]
182         
183     #TODO: There should be a better way to do this.   CloseConditions are ignored at the moment
184
185     bugs2 = MapBug.find(:all, :limit => limit, :order => "last_changed DESC", :joins => :comments, :include => :comments,
186                         :conditions => conditions)
187     @bugs = bugs2.uniq
188     respond_to do |format|
189       format.html {render :template => 'map_bugs/get_bugs.js', :content_type => "text/javascript"}
190       format.rss {render :template => 'map_bugs/get_bugs.rss'}
191       format.js
192       format.xml {render :template => 'map_bugs/get_bugs.xml'}
193       format.json { render :json => @bugs.to_json(:methods => [:lat, :lon], :only => [:id, :status, :date_created], :include => { :comments => { :only => [:commenter_name, :date_created, :comment]}}) }
194       format.gpx {render :template => 'map_bugs/get_bugs.gpx'}
195     end
196   end
197
198   def my_bugs
199     if params[:display_name] 
200       @user2 = User.find_by_display_name(params[:display_name], :conditions => { :status => ["active", "confirmed"] }) 
201  
202       if @user2  
203         if @user2.data_public? or @user2 == @user 
204           conditions = ['map_bug_comment.commenter_id = ?', @user2.id] 
205         else 
206           conditions = ['false'] 
207         end 
208       else #if request.format == :html 
209         @title = t 'user.no_such_user.title' 
210         @not_found_user = params[:display_name] 
211         render :template => 'user/no_such_user', :status => :not_found 
212         return
213       end 
214     end
215
216     if @user2 
217       user_link = render_to_string :partial => "user", :object => @user2 
218     end 
219     
220     @title =  t 'bugs.user.title_user', :user => @user2.display_name 
221     @heading =  t 'bugs.user.heading_user', :user => @user2.display_name 
222     @description = t 'bugs.user.description_user', :user => user_link
223     
224     @page = (params[:page] || 1).to_i 
225     @page_size = 10
226
227     @bugs = MapBug.find(:all, 
228                         :include => [:comments, {:comments => :user}],
229                         :joins => :comments,
230                         :order => "last_changed DESC",
231                         :conditions => conditions,
232                         :offset => (@page - 1) * @page_size, 
233                         :limit => @page_size).uniq
234   end
235
236 private 
237   #------------------------------------------------------------ 
238   # utility functions below. 
239   #------------------------------------------------------------   
240  
241   ## 
242   # merge two conditions 
243   # TODO: this is a copy from changeset_controler.rb and should be factored out to share
244   def cond_merge(a, b) 
245     if a and b 
246       a_str = a.shift 
247       b_str = b.shift 
248       return [ a_str + " AND " + b_str ] + a + b 
249     elsif a  
250       return a 
251     else b 
252       return b 
253     end 
254   end 
255
256   def render_ok
257     output_js = :false
258     output_js = :true if params['format'] == "js"
259
260     if output_js == :true
261       render :text => "osbResponse();", :content_type => "text/javascript" 
262     else
263       render :text => "ok " + @bug.id.to_s + "\n", :content_type => "text/html" if @bug
264       render :text => "ok\n", :content_type => "text/html" unless @bug
265     end
266   end
267
268   def getLimit
269     limit = 100
270     limit = params['limit'] if ((params['limit']) && (params['limit'].to_i < 10000) && (params['limit'].to_i > 0))
271     return limit
272   end
273
274   def closedCondition
275     closed_since = 7 unless params['closed']
276     closed_since = params['closed'].to_i if params['closed']
277         
278     if closed_since < 0
279       conditions = ["status != 'hidden'"]
280     elsif closed_since > 0
281       conditions = ["((status = 'open') OR ((status = 'closed' ) AND (date_closed > '" + (Time.now - closed_since.days).to_s + "')))"]
282     else
283       conditions = ["status = 'open'"]
284     end
285
286     return conditions
287   end
288
289   def add_comment(bug, comment, name,event) 
290     t = Time.now.getutc 
291     bug_comment = bug.comments.create(:date_created => t, :visible => true, :event => event)
292     bug_comment.comment = comment unless comment == :nil
293     if @user  
294       bug_comment.commenter_id = @user.id
295       bug_comment.commenter_name = @user.display_name
296     else  
297       bug_comment.commenter_ip = request.remote_ip
298       bug_comment.commenter_name = name + " (a)"
299     end
300     bug_comment.save 
301     bug.last_changed = t 
302     bug.save
303
304     sent_to = Set.new
305     bug.comments.each do | cmt |
306       if cmt.user
307         unless sent_to.include?(cmt.user)
308           Notifier.deliver_bug_comment_notification(bug_comment, cmt.user) unless cmt.user == @user
309           sent_to.add(cmt.user)
310         end
311       end
312     end
313   end
314 end