]> git.openstreetmap.org Git - rails.git/blob - app/controllers/changesets_controller.rb
Merge remote-tracking branch 'upstream/pull/2281'
[rails.git] / app / controllers / changesets_controller.rb
1 # The ChangesetController is the RESTful interface to Changeset objects
2
3 class ChangesetsController < ApplicationController
4   layout "site"
5   require "xml/libxml"
6
7   before_action :authorize_web
8   before_action :set_locale
9   before_action -> { check_database_readable(true) }, :only => [:index, :feed]
10
11   authorize_resource
12
13   around_action :web_timeout
14
15   # Helper methods for checking consistency
16   include ConsistencyValidations
17
18   ##
19   # list non-empty changesets in reverse chronological order
20   def index
21     @params = params.permit(:display_name, :bbox, :friends, :nearby, :max_id, :list)
22
23     if request.format == :atom && @params[:max_id]
24       redirect_to url_for(@params.merge(:max_id => nil)), :status => :moved_permanently
25       return
26     end
27
28     if @params[:display_name]
29       user = User.find_by(:display_name => @params[:display_name])
30       if !user || !user.active?
31         render_unknown_user @params[:display_name]
32         return
33       end
34     end
35
36     if (@params[:friends] || @params[:nearby]) && !current_user
37       require_user
38       return
39     end
40
41     if request.format == :html && !@params[:list]
42       require_oauth
43       render :action => :history, :layout => map_layout
44     else
45       changesets = conditions_nonempty(Changeset.all)
46
47       if @params[:display_name]
48         changesets = if user.data_public? || user == current_user
49                        changesets.where(:user_id => user.id)
50                      else
51                        changesets.where("false")
52                      end
53       elsif @params[:bbox]
54         changesets = conditions_bbox(changesets, BoundingBox.from_bbox_params(params))
55       elsif @params[:friends] && current_user
56         changesets = changesets.where(:user_id => current_user.friends.identifiable)
57       elsif @params[:nearby] && current_user
58         changesets = changesets.where(:user_id => current_user.nearby)
59       end
60
61       changesets = changesets.where("changesets.id <= ?", @params[:max_id]) if @params[:max_id]
62
63       @changesets = changesets.order("changesets.id DESC").limit(20).preload(:user, :changeset_tags, :comments)
64
65       render :action => :index, :layout => false
66     end
67   end
68
69   ##
70   # list edits as an atom feed
71   def feed
72     index
73   end
74
75   private
76
77   #------------------------------------------------------------
78   # utility functions below.
79   #------------------------------------------------------------
80
81   ##
82   # if a bounding box was specified do some sanity checks.
83   # restrict changesets to those enclosed by a bounding box
84   # we need to return both the changesets and the bounding box
85   def conditions_bbox(changesets, bbox)
86     if bbox
87       bbox.check_boundaries
88       bbox = bbox.to_scaled
89
90       changesets.where("min_lon < ? and max_lon > ? and min_lat < ? and max_lat > ?",
91                        bbox.max_lon.to_i, bbox.min_lon.to_i,
92                        bbox.max_lat.to_i, bbox.min_lat.to_i)
93     else
94       changesets
95     end
96   end
97
98   ##
99   # eliminate empty changesets (where the bbox has not been set)
100   # this should be applied to all changeset list displays
101   def conditions_nonempty(changesets)
102     changesets.where("num_changes > 0")
103   end
104 end