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