]> git.openstreetmap.org Git - rails.git/blob - app/controllers/changesets_controller.rb
Merge remote-tracking branch 'upstream/pull/5936'
[rails.git] / app / controllers / changesets_controller.rb
1 # The ChangesetController is the RESTful interface to Changeset objects
2
3 class ChangesetsController < ApplicationController
4   include UserMethods
5   include PaginationMethods
6
7   layout "site"
8
9   before_action :authorize_web
10   before_action :set_locale
11   before_action -> { check_database_readable(:need_api => true) }
12   before_action :require_oauth, :only => :show
13
14   authorize_resource
15
16   around_action :web_timeout
17
18   ##
19   # list non-empty changesets in reverse chronological order
20   def index
21     param! :before, Integer, :min => 1
22     param! :after, Integer, :min => 1
23
24     @params = params.permit(:display_name, :bbox, :friends, :nearby, :before, :after, :list)
25
26     if request.format == :atom && (@params[:before] || @params[:after])
27       redirect_to url_for(@params.merge(:before => nil, :after => nil)), :status => :moved_permanently
28       return
29     end
30
31     if @params[:display_name]
32       user = User.find_by(:display_name => @params[:display_name])
33       if !user || !user.active?
34         render_unknown_user @params[:display_name]
35         return
36       end
37     end
38
39     if (@params[:friends] || @params[:nearby]) && !current_user
40       require_user
41       return
42     end
43
44     if request.format == :html && !@params[:list]
45       require_oauth
46       render :action => :history, :layout => map_layout
47     else
48       changesets = conditions_nonempty(Changeset.all)
49
50       if @params[:display_name]
51         changesets = if user.data_public? || user == current_user
52                        changesets.where(:user => user)
53                      else
54                        changesets.where("false")
55                      end
56       elsif @params[:bbox]
57         bbox_array = @params[:bbox].split(",").map(&:to_f)
58         raise OSM::APIBadUserInput, "The parameter bbox must be of the form min_lon,min_lat,max_lon,max_lat" unless bbox_array.count == 4
59
60         changesets = conditions_bbox(changesets, *bbox_array)
61       elsif @params[:friends] && current_user
62         changesets = changesets.where(:user => current_user.followings.identifiable)
63       elsif @params[:nearby] && current_user
64         changesets = changesets.where(:user => current_user.nearby)
65       end
66
67       @changesets, @newer_changesets_id, @older_changesets_id = get_page_items(changesets, :includes => [:user, :changeset_tags, :comments])
68
69       render :action => :index, :layout => false
70     end
71   end
72
73   ##
74   # list edits as an atom feed
75   def feed
76     index
77   end
78
79   def show
80     @type = "changeset"
81     @changeset = Changeset.find(params[:id])
82     case turbo_frame_request_id
83     when "changeset_nodes"
84       @node_pages, @nodes = paginate(:old_nodes, :conditions => { :changeset_id => @changeset.id }, :order => [:node_id, :version], :per_page => 20, :parameter => "node_page")
85       render :partial => "elements", :locals => { :type => "node", :elements => @nodes, :pages => @node_pages }
86     when "changeset_ways"
87       @way_pages, @ways = paginate(:old_ways, :conditions => { :changeset_id => @changeset.id }, :order => [:way_id, :version], :per_page => 20, :parameter => "way_page")
88       render :partial => "elements", :locals => { :type => "way", :elements => @ways, :pages => @way_pages }
89     when "changeset_relations"
90       @relation_pages, @relations = paginate(:old_relations, :conditions => { :changeset_id => @changeset.id }, :order => [:relation_id, :version], :per_page => 20, :parameter => "relation_page")
91       render :partial => "elements", :locals => { :type => "relation", :elements => @relations, :pages => @relation_pages }
92     else
93       @comments = if current_user&.moderator?
94                     @changeset.comments.unscope(:where => :visible).includes(:author)
95                   else
96                     @changeset.comments.includes(:author)
97                   end
98       @node_pages, @nodes = paginate(:old_nodes, :conditions => { :changeset_id => @changeset.id }, :order => [:node_id, :version], :per_page => 20, :parameter => "node_page")
99       @way_pages, @ways = paginate(:old_ways, :conditions => { :changeset_id => @changeset.id }, :order => [:way_id, :version], :per_page => 20, :parameter => "way_page")
100       @relation_pages, @relations = paginate(:old_relations, :conditions => { :changeset_id => @changeset.id }, :order => [:relation_id, :version], :per_page => 20, :parameter => "relation_page")
101       if @changeset.user.active? && @changeset.user.data_public?
102         changesets = conditions_nonempty(@changeset.user.changesets)
103         @next_by_user = changesets.where("id > ?", @changeset.id).reorder(:id => :asc).first
104         @prev_by_user = changesets.where(:id => ...@changeset.id).reorder(:id => :desc).first
105       end
106       render :layout => map_layout
107     end
108   rescue ActiveRecord::RecordNotFound
109     render :template => "browse/not_found", :status => :not_found, :layout => map_layout
110   end
111
112   private
113
114   #------------------------------------------------------------
115   # utility functions below.
116   #------------------------------------------------------------
117
118   ##
119   # restrict changesets to those enclosed by a bounding box
120   def conditions_bbox(changesets, min_lon, min_lat, max_lon, max_lat)
121     db_min_lat = (min_lat * GeoRecord::SCALE).to_i
122     db_max_lat = (max_lat * GeoRecord::SCALE).to_i
123     db_min_lon = (wrap_lon(min_lon) * GeoRecord::SCALE).to_i
124     db_max_lon = (wrap_lon(max_lon) * GeoRecord::SCALE).to_i
125
126     changesets = changesets.where("min_lat < ? and max_lat > ?", db_max_lat, db_min_lat)
127
128     if max_lon - min_lon >= 360
129       # the query bbox spans the entire world, therefore no lon checks are necessary
130       changesets
131     elsif db_min_lon <= db_max_lon
132       # the normal case when the query bbox doesn't include the antimeridian
133       changesets.where("min_lon < ? and max_lon > ?", db_max_lon, db_min_lon)
134     else
135       # the query bbox includes the antimeridian
136       # this case works as if there are two query bboxes:
137       #   [-180*SCALE .. db_max_lon], [db_min_lon .. 180*SCALE]
138       # it would be necessary to check if changeset bboxes intersect with either of the query bboxes:
139       #   (changesets.min_lon < db_max_lon and changesets.max_lon > -180*SCALE) or (changesets.min_lon < 180*SCALE and changesets.max_lon > db_min_lon)
140       # but the comparisons with -180*SCALE and 180*SCALE are unnecessary:
141       #   (changesets.min_lon < db_max_lon) or (changesets.max_lon > db_min_lon)
142       changesets.where("min_lon < ? or max_lon > ?", db_max_lon, db_min_lon)
143     end
144   end
145
146   def wrap_lon(lon)
147     ((lon + 180) % 360) - 180
148   end
149
150   ##
151   # eliminate empty changesets (where the bbox has not been set)
152   # this should be applied to all changeset list displays
153   def conditions_nonempty(changesets)
154     changesets.where("num_changes > 0")
155   end
156 end