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