]> git.openstreetmap.org Git - rails.git/blob - app/controllers/changesets_controller.rb
Merge remote-tracking branch 'upstream/pull/7633'
[rails.git] / app / controllers / changesets_controller.rb
1 # frozen_string_literal: true
2
3 # The ChangesetController is the RESTful interface to Changeset objects
4
5 class ChangesetsController < ApplicationController
6   include UserMethods
7   include PaginationMethods
8
9   layout :site_layout
10
11   before_action :authorize_web
12   before_action :set_locale
13   before_action -> { check_database_readable(:need_api => true) }
14   before_action :require_oauth, :only => :show
15
16   authorize_resource
17
18   around_action :web_timeout
19
20   ELEMENTS_PER_PAGE = 20
21
22   ##
23   # list non-empty changesets in reverse chronological order
24   def index
25     param! :before, Integer, :min => 1
26     param! :after, Integer, :min => 1
27     param! :bbox, String, :custom => lambda { |bbox|
28       raise RailsParam::InvalidParameterError, "bbox must be of the form min_lon,min_lat,max_lon,max_lat" unless bbox.count(",") == 3
29     }
30
31     @params = params.permit(:display_name, :bbox, :friends, :nearby, :before, :after, :list)
32
33     if request.format == :atom && (@params[:before] || @params[:after])
34       redirect_to url_for(@params.merge(:before => nil, :after => nil)), :status => :moved_permanently
35       return
36     end
37
38     if @params[:display_name]
39       user = User.find_by(:display_name => @params[:display_name])
40       if !user || !user.active?
41         render_unknown_user @params[:display_name]
42         return
43       end
44     end
45
46     if (@params[:friends] || @params[:nearby]) && !current_user
47       require_user
48       return
49     end
50
51     if request.format == :html && !@params[:list]
52       require_oauth
53       render :action => :history, :layout => map_layout
54     else
55       changesets = conditions_nonempty(Changeset.all)
56
57       if @params[:display_name]
58         changesets = if user.data_public? || user == current_user
59                        changesets.where(:user => user)
60                      else
61                        changesets.where("false")
62                      end
63       elsif @params[:bbox]
64         bbox_array = @params[:bbox].split(",").map(&:to_f)
65         changesets = conditions_bbox(changesets, *bbox_array)
66       elsif @params[:friends] && current_user
67         changesets = changesets.where(:user => current_user.followings.identifiable)
68       elsif @params[:nearby] && current_user
69         changesets = changesets.where(:user => current_user.nearby)
70       end
71
72       @changesets = get_page_items(changesets, :includes => [:user, :changeset_tags, :comments])
73
74       respond_to do |format|
75         format.any(:html, :atom) { render :action => :index, :layout => false }
76       end
77     end
78   end
79
80   ##
81   # list edits as an atom feed
82   def feed
83     index
84   end
85
86   def show
87     @type = "changeset"
88     @changeset = Changeset.find(params.expect(:id))
89     case turbo_frame_request_id
90     when "changeset_nodes"
91       load_nodes
92       render :partial => "elements", :locals => { :type => "node", :elements => @nodes, :elements_count => @nodes_count, :current_page => @current_node_page }
93     when "changeset_ways"
94       load_ways
95       render :partial => "elements", :locals => { :type => "way", :elements => @ways, :elements_count => @ways_count, :current_page => @current_way_page }
96     when "changeset_relations"
97       load_relations
98       render :partial => "elements", :locals => { :type => "relation", :elements => @relations, :elements_count => @relations_count, :current_page => @current_relation_page }
99     else
100       @comments = if current_user&.moderator?
101                     @changeset.comments.unscope(:where => :visible).includes(:author)
102                   else
103                     @changeset.comments.includes(:author)
104                   end
105       load_nodes
106       load_ways
107       load_relations
108       if @changeset.user.active? && @changeset.user.data_public?
109         changesets = conditions_nonempty(@changeset.user.changesets)
110         @next_by_user = changesets.where("id > ?", @changeset.id).reorder(:id => :asc).first
111         @prev_by_user = changesets.where(:id => ...@changeset.id).reorder(:id => :desc).first
112       end
113       respond_to do |format|
114         format.html { render :layout => map_layout }
115       end
116     end
117   rescue ActiveRecord::RecordNotFound
118     render :template => "browse/not_found", :status => :not_found, :layout => map_layout
119   end
120
121   private
122
123   #------------------------------------------------------------
124   # utility functions below.
125   #------------------------------------------------------------
126
127   ##
128   # restrict changesets to those enclosed by a bounding box
129   def conditions_bbox(changesets, min_lon, min_lat, max_lon, max_lat)
130     db_min_lat = (min_lat * GeoRecord::SCALE).to_i
131     db_max_lat = (max_lat * GeoRecord::SCALE).to_i
132     db_min_lon = (wrap_lon(min_lon) * GeoRecord::SCALE).to_i
133     db_max_lon = (wrap_lon(max_lon) * GeoRecord::SCALE).to_i
134
135     changesets = changesets.where("min_lat < ? and max_lat > ?", db_max_lat, db_min_lat)
136
137     if max_lon - min_lon >= 360
138       # the query bbox spans the entire world, therefore no lon checks are necessary
139       changesets
140     elsif db_min_lon <= db_max_lon
141       # the normal case when the query bbox doesn't include the antimeridian
142       changesets.where("min_lon < ? and max_lon > ?", db_max_lon, db_min_lon)
143     else
144       # the query bbox includes the antimeridian
145       # this case works as if there are two query bboxes:
146       #   [-180*SCALE .. db_max_lon], [db_min_lon .. 180*SCALE]
147       # it would be necessary to check if changeset bboxes intersect with either of the query bboxes:
148       #   (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)
149       # but the comparisons with -180*SCALE and 180*SCALE are unnecessary:
150       #   (changesets.min_lon < db_max_lon) or (changesets.max_lon > db_min_lon)
151       changesets.where("min_lon < ? or max_lon > ?", db_max_lon, db_min_lon)
152     end
153   end
154
155   def wrap_lon(lon)
156     ((lon + 180) % 360) - 180
157   end
158
159   ##
160   # eliminate empty changesets (where the bbox has not been set)
161   # this should be applied to all changeset list displays
162   def conditions_nonempty(changesets)
163     changesets.where("num_changes > 0")
164   end
165
166   def load_nodes
167     @nodes_count = @changeset.actual_num_changed_nodes
168     @current_node_page = params.fetch(:node_page, "1").to_i.clamp(1, element_pages_count(@nodes_count))
169     @nodes = @changeset.old_nodes
170                        .order(:node_id, :version)
171                        .offset(ELEMENTS_PER_PAGE * (@current_node_page - 1))
172                        .limit(ELEMENTS_PER_PAGE)
173                        .preload(:old_tags)
174   end
175
176   def load_ways
177     @ways_count = @changeset.actual_num_changed_ways
178     @current_way_page = params.fetch(:way_page, "1").to_i.clamp(1, element_pages_count(@ways_count))
179     @ways = @changeset.old_ways
180                       .order(:way_id, :version)
181                       .offset(ELEMENTS_PER_PAGE * (@current_way_page - 1))
182                       .limit(ELEMENTS_PER_PAGE)
183                       .preload(:old_tags)
184   end
185
186   def load_relations
187     @relations_count = @changeset.actual_num_changed_relations
188     @current_relation_page = params.fetch(:relation_page, "1").to_i.clamp(1, element_pages_count(@relations_count))
189     @relations = @changeset.old_relations
190                            .order(:relation_id, :version)
191                            .offset(ELEMENTS_PER_PAGE * (@current_relation_page - 1))
192                            .limit(ELEMENTS_PER_PAGE)
193                            .preload(:old_tags)
194   end
195
196   helper_method def element_pages_count(elements_count)
197     [1, 1 + ((elements_count - 1) / ELEMENTS_PER_PAGE)].max
198   end
199
200   helper_method def element_range_values(elements_count, page)
201     { :x => (ELEMENTS_PER_PAGE * (page - 1)) + 1,
202       :y => [ELEMENTS_PER_PAGE * page, elements_count].min,
203       :count => elements_count }
204   end
205 end