]> git.openstreetmap.org Git - rails.git/blob - app/controllers/changesets_controller.rb
Merge pull request #4296 from AntonKhorev/header-flex
[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
6   layout "site"
7   require "xml/libxml"
8
9   before_action :authorize_web
10   before_action :set_locale
11   before_action -> { check_database_readable(:need_api => true) }, :only => [:index, :feed]
12
13   authorize_resource
14
15   around_action :web_timeout
16
17   # Helper methods for checking consistency
18   include ConsistencyValidations
19
20   ##
21   # list non-empty changesets in reverse chronological order
22   def index
23     @params = params.permit(:display_name, :bbox, :friends, :nearby, :max_id, :list)
24
25     if request.format == :atom && @params[:max_id]
26       redirect_to url_for(@params.merge(:max_id => nil)), :status => :moved_permanently
27       return
28     end
29
30     if @params[:display_name]
31       user = User.find_by(:display_name => @params[:display_name])
32       if !user || !user.active?
33         render_unknown_user @params[:display_name]
34         return
35       end
36     end
37
38     if (@params[:friends] || @params[:nearby]) && !current_user
39       require_user
40       return
41     end
42
43     if request.format == :html && !@params[:list]
44       require_oauth
45       render :action => :history, :layout => map_layout
46     else
47       changesets = conditions_nonempty(Changeset.all)
48
49       if @params[:display_name]
50         changesets = if user.data_public? || user == current_user
51                        changesets.where(:user => user)
52                      else
53                        changesets.where("false")
54                      end
55       elsif @params[:bbox]
56         changesets = conditions_bbox(changesets, BoundingBox.from_bbox_params(params))
57       elsif @params[:friends] && current_user
58         changesets = changesets.where(:user => current_user.friends.identifiable)
59       elsif @params[:nearby] && current_user
60         changesets = changesets.where(:user => current_user.nearby)
61       end
62
63       changesets = changesets.where("changesets.id <= ?", @params[:max_id]) if @params[:max_id]
64
65       @changesets = changesets.order("changesets.id DESC").limit(20).preload(:user, :changeset_tags, :comments)
66
67       render :action => :index, :layout => false
68     end
69   end
70
71   ##
72   # list edits as an atom feed
73   def feed
74     index
75   end
76
77   private
78
79   #------------------------------------------------------------
80   # utility functions below.
81   #------------------------------------------------------------
82
83   ##
84   # if a bounding box was specified do some sanity checks.
85   # restrict changesets to those enclosed by a bounding box
86   def conditions_bbox(changesets, bbox)
87     if bbox
88       bbox.check_boundaries
89       bbox = bbox.to_scaled
90
91       changesets.where("min_lon < ? and max_lon > ? and min_lat < ? and max_lat > ?",
92                        bbox.max_lon.to_i, bbox.min_lon.to_i,
93                        bbox.max_lat.to_i, bbox.min_lat.to_i)
94     else
95       changesets
96     end
97   end
98
99   ##
100   # eliminate empty changesets (where the bbox has not been set)
101   # this should be applied to all changeset list displays
102   def conditions_nonempty(changesets)
103     changesets.where("num_changes > 0")
104   end
105 end