1 # The ChangesetController is the RESTful interface to Changeset objects
 
   3 class ChangesetsController < ApplicationController
 
   5   include PaginationMethods
 
   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
 
  16   around_action :web_timeout
 
  19   # list non-empty changesets in reverse chronological order
 
  21     param! :before, Integer, :min => 1
 
  22     param! :after, Integer, :min => 1
 
  24     @params = params.permit(:display_name, :bbox, :friends, :nearby, :before, :after, :list)
 
  26     if request.format == :atom && (@params[:before] || @params[:after])
 
  27       redirect_to url_for(@params.merge(:before => nil, :after => nil)), :status => :moved_permanently
 
  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]
 
  39     if (@params[:friends] || @params[:nearby]) && !current_user
 
  44     if request.format == :html && !@params[:list]
 
  46       render :action => :history, :layout => map_layout
 
  48       changesets = conditions_nonempty(Changeset.all)
 
  50       if @params[:display_name]
 
  51         changesets = if user.data_public? || user == current_user
 
  52                        changesets.where(:user => user)
 
  54                        changesets.where("false")
 
  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
 
  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)
 
  67       @changesets, @newer_changesets_id, @older_changesets_id = get_page_items(changesets, :includes => [:user, :changeset_tags, :comments])
 
  69       render :action => :index, :layout => false
 
  74   # list edits as an atom feed
 
  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 }
 
  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 }
 
  93       @comments = if current_user&.moderator?
 
  94                     @changeset.comments.unscope(:where => :visible).includes(:author)
 
  96                     @changeset.comments.includes(:author)
 
  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
 
 106       render :layout => map_layout
 
 108   rescue ActiveRecord::RecordNotFound
 
 109     render :template => "browse/not_found", :status => :not_found, :layout => map_layout
 
 114   #------------------------------------------------------------
 
 115   # utility functions below.
 
 116   #------------------------------------------------------------
 
 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
 
 126     changesets = changesets.where("min_lat < ? and max_lat > ?", db_max_lat, db_min_lat)
 
 128     if max_lon - min_lon >= 360
 
 129       # the query bbox spans the entire world, therefore no lon checks are necessary
 
 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)
 
 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)
 
 147     ((lon + 180) % 360) - 180
 
 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")