]> git.openstreetmap.org Git - rails.git/blob - app/controllers/map_bugs_controller.rb
Add methods to get a bug's author details from the first comment
[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 => "updated_at 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, :created_at], :include => { :comments => { :only => [:author_name, :created_at, :body]}}) }   
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
86       add_comment(@bug, comment, name, "opened")
87     end
88  
89     render_ok
90   end
91
92   def edit_bug
93     raise OSM::APIBadUserInput.new("No id was given") unless params['id']
94     raise OSM::APIBadUserInput.new("No text was given") unless params['text']
95
96     name = "NoName"
97     name = params['name'] if params['name']
98         
99     id = params['id'].to_i
100
101     bug = MapBug.find_by_id(id)
102     raise OSM::APINotFoundError unless bug
103     raise OSM::APIAlreadyDeletedError unless bug.visible
104
105     MapBug.transaction do
106       bug_comment = add_comment(bug, params['text'], name, "commented")
107     end
108
109     render_ok
110   end
111
112   def close_bug
113     raise OSM::APIBadUserInput.new("No id was given") unless params['id']
114         
115     id = params['id'].to_i
116     name = "NoName"
117     name = params['name'] if params['name']
118
119     bug = MapBug.find_by_id(id)
120     raise OSM::APINotFoundError unless bug
121     raise OSM::APIAlreadyDeletedError unless bug.visible
122
123     MapBug.transaction do
124       bug.close_bug
125       add_comment(bug, :nil, name, "closed")
126     end
127
128     render_ok
129   end 
130
131   def rss
132     limit = getLimit
133     conditions = closedCondition
134
135     # Figure out the bbox
136     bbox = params['bbox']
137
138     if bbox and bbox.count(',') == 3
139       bbox = bbox.split(',')
140       @min_lon, @min_lat, @max_lon, @max_lat = sanitise_boundaries(bbox)
141
142       check_boundaries(@min_lon, @min_lat, @max_lon, @max_lat, MAX_BUG_REQUEST_AREA)
143
144       conditions = cond_merge conditions, [OSM.sql_for_area(@min_lat, @min_lon, @max_lat, @max_lon)]
145     end
146
147     @comments = MapBugComment.find(:all, :limit => limit, :order => "created_at DESC", :joins => :map_bug, :include => :map_bug, :conditions => conditions)
148     render :template => 'map_bugs/rss.rss'
149   end
150
151   def read
152     @bug = MapBug.find(params['id'])
153     raise OSM::APINotFoundError unless @bug
154     raise OSM::APIAlreadyDeletedError unless @bug.visible
155     
156     respond_to do |format|
157       format.rss
158       format.xml
159       format.json { render :json => @bug.to_json(:methods => [:lat, :lon], :only => [:id, :status, :created_at], :include => { :comments => { :only => [:author_name, :created_at, :body]}}) }    
160       format.gpx
161     end
162   end
163
164   def delete
165     bug = MapBug.find(params['id'])
166     raise OSM::APINotFoundError unless @bug
167     raise OSM::APIAlreadyDeletedError unless @bug.visible
168
169     MapBug.transaction do
170       bug.status = "hidden"
171       bug.save
172       add_comment(bug,:nil,name,"hidden")
173     end
174
175     render :text => "ok\n", :content_type => "text/html" 
176   end
177
178   def search
179     raise OSM::APIBadUserInput.new("No query string was given") unless params['q']
180     limit = getLimit
181     conditions = closedCondition
182     conditions = cond_merge conditions, ['map_bug_comment.body ~ ?', params['q']]
183         
184     #TODO: There should be a better way to do this.   CloseConditions are ignored at the moment
185
186     bugs2 = MapBug.find(:all, :limit => limit, :order => "updated_at DESC", :joins => :comments, :include => :comments,
187                         :conditions => conditions)
188     @bugs = bugs2.uniq
189     respond_to do |format|
190       format.html {render :template => 'map_bugs/get_bugs.js', :content_type => "text/javascript"}
191       format.rss {render :template => 'map_bugs/get_bugs.rss'}
192       format.js
193       format.xml {render :template => 'map_bugs/get_bugs.xml'}
194       format.json { render :json => @bugs.to_json(:methods => [:lat, :lon], :only => [:id, :status, :created_at], :include => { :comments => { :only => [:author_name, :created_at, :body]}}) }
195       format.gpx {render :template => 'map_bugs/get_bugs.gpx'}
196     end
197   end
198
199   def my_bugs
200     if params[:display_name] 
201       @user2 = User.find_by_display_name(params[:display_name], :conditions => { :status => ["active", "confirmed"] }) 
202  
203       if @user2  
204         if @user2.data_public? or @user2 == @user 
205           conditions = ['map_bug_comment.author_id = ?', @user2.id] 
206         else 
207           conditions = ['false'] 
208         end 
209       else #if request.format == :html 
210         @title = t 'user.no_such_user.title' 
211         @not_found_user = params[:display_name] 
212         render :template => 'user/no_such_user', :status => :not_found 
213         return
214       end 
215     end
216
217     if @user2 
218       user_link = render_to_string :partial => "user", :object => @user2 
219     end 
220     
221     @title =  t 'bugs.user.title_user', :user => @user2.display_name 
222     @heading =  t 'bugs.user.heading_user', :user => @user2.display_name 
223     @description = t 'bugs.user.description_user', :user => user_link
224     
225     @page = (params[:page] || 1).to_i 
226     @page_size = 10
227
228     @bugs = MapBug.find(:all, 
229                         :include => [:comments, {:comments => :author}],
230                         :joins => :comments,
231                         :order => "updated_at DESC",
232                         :conditions => conditions,
233                         :offset => (@page - 1) * @page_size, 
234                         :limit => @page_size).uniq
235   end
236
237 private 
238   #------------------------------------------------------------ 
239   # utility functions below. 
240   #------------------------------------------------------------   
241  
242   ## 
243   # merge two conditions 
244   # TODO: this is a copy from changeset_controler.rb and should be factored out to share
245   def cond_merge(a, b) 
246     if a and b 
247       a_str = a.shift 
248       b_str = b.shift 
249       return [ a_str + " AND " + b_str ] + a + b 
250     elsif a  
251       return a 
252     else b 
253       return b 
254     end 
255   end 
256
257   def render_ok
258     output_js = :false
259     output_js = :true if params['format'] == "js"
260
261     if output_js == :true
262       render :text => "osbResponse();", :content_type => "text/javascript" 
263     else
264       render :text => "ok " + @bug.id.to_s + "\n", :content_type => "text/html" if @bug
265       render :text => "ok\n", :content_type => "text/html" unless @bug
266     end
267   end
268
269   def getLimit
270     limit = 100
271     limit = params['limit'] if ((params['limit']) && (params['limit'].to_i < 10000) && (params['limit'].to_i > 0))
272     return limit
273   end
274
275   def closedCondition
276     closed_since = 7 unless params['closed']
277     closed_since = params['closed'].to_i if params['closed']
278         
279     if closed_since < 0
280       conditions = ["status != 'hidden'"]
281     elsif closed_since > 0
282       conditions = ["((status = 'open') OR ((status = 'closed' ) AND (closed_at > '" + (Time.now - closed_since.days).to_s + "')))"]
283     else
284       conditions = ["status = 'open'"]
285     end
286
287     return conditions
288   end
289
290   def add_comment(bug, comment, name,event) 
291     bug_comment = bug.comments.create(:visible => true, :event => event)
292     bug_comment.body = comment unless comment == :nil
293     if @user  
294       bug_comment.author_id = @user.id
295       bug_comment.author_name = @user.display_name
296     else  
297       bug_comment.author_ip = request.remote_ip
298       bug_comment.author_name = name + " (a)"
299     end
300     bug_comment.save 
301     bug.save
302
303     sent_to = Set.new
304     bug.comments.each do | cmt |
305       if cmt.author
306         unless sent_to.include?(cmt.author)
307           Notifier.deliver_bug_comment_notification(bug_comment, cmt.author) unless cmt.author == @user
308           sent_to.add(cmt.author)
309         end
310       end
311     end
312   end
313 end