]> git.openstreetmap.org Git - rails.git/blob - app/controllers/changesets_controller.rb
613a3ee2a9f919522a060361c063052c020ea879
[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, :show]
12   before_action :require_oauth, :only => :show
13   before_action :check_database_writable, :only => [:subscribe, :unsubscribe]
14
15   authorize_resource
16
17   around_action :web_timeout
18
19   # Helper methods for checking consistency
20   include ConsistencyValidations
21
22   ##
23   # list non-empty changesets in reverse chronological order
24   def index
25     @params = params.permit(:display_name, :bbox, :friends, :nearby, :max_id, :list)
26
27     if request.format == :atom && @params[:max_id]
28       redirect_to url_for(@params.merge(:max_id => nil)), :status => :moved_permanently
29       return
30     end
31
32     if @params[:display_name]
33       user = User.find_by(:display_name => @params[:display_name])
34       if !user || !user.active?
35         render_unknown_user @params[:display_name]
36         return
37       end
38     end
39
40     if (@params[:friends] || @params[:nearby]) && !current_user
41       require_user
42       return
43     end
44
45     if request.format == :html && !@params[:list]
46       require_oauth
47       render :action => :history, :layout => map_layout
48     else
49       changesets = conditions_nonempty(Changeset.all)
50
51       if @params[:display_name]
52         changesets = if user.data_public? || user == current_user
53                        changesets.where(:user => user)
54                      else
55                        changesets.where("false")
56                      end
57       elsif @params[:bbox]
58         changesets = conditions_bbox(changesets, BoundingBox.from_bbox_params(params))
59       elsif @params[:friends] && current_user
60         changesets = changesets.where(:user => current_user.friends.identifiable)
61       elsif @params[:nearby] && current_user
62         changesets = changesets.where(:user => current_user.nearby)
63       end
64
65       changesets = changesets.where("changesets.id <= ?", @params[:max_id]) if @params[:max_id]
66
67       @changesets = changesets.order("changesets.id DESC").limit(20).preload(:user, :changeset_tags, :comments)
68
69       render :action => :index, :layout => false
70     end
71   end
72
73   ##
74   # list edits as an atom feed
75   def feed
76     index
77   end
78
79   def show
80     @type = "changeset"
81     @changeset = Changeset.find(params[:id])
82     @comments = if current_user&.moderator?
83                   @changeset.comments.unscope(:where => :visible).includes(:author)
84                 else
85                   @changeset.comments.includes(:author)
86                 end
87     @node_pages, @nodes = paginate(:old_nodes, :conditions => { :changeset_id => @changeset.id }, :per_page => 20, :parameter => "node_page")
88     @way_pages, @ways = paginate(:old_ways, :conditions => { :changeset_id => @changeset.id }, :per_page => 20, :parameter => "way_page")
89     @relation_pages, @relations = paginate(:old_relations, :conditions => { :changeset_id => @changeset.id }, :per_page => 20, :parameter => "relation_page")
90     if @changeset.user.active? && @changeset.user.data_public?
91       @next_by_user = @changeset.user.changesets.where("id > ?", @changeset.id).reorder(:id => :asc).first
92       @prev_by_user = @changeset.user.changesets.where("id < ?", @changeset.id).reorder(:id => :desc).first
93     end
94     render :layout => map_layout
95   rescue ActiveRecord::RecordNotFound
96     render :template => "browse/not_found", :status => :not_found, :layout => map_layout
97   end
98
99   ##
100   # subscribe to a changeset
101   def subscribe
102     @changeset = Changeset.find(params[:id])
103
104     if request.post?
105       @changeset.subscribe(current_user) unless @changeset.subscribed?(current_user)
106
107       redirect_to changeset_path(@changeset)
108     end
109   rescue ActiveRecord::RecordNotFound
110     render :action => "no_such_entry", :status => :not_found
111   end
112
113   ##
114   # unsubscribe from a changeset
115   def unsubscribe
116     @changeset = Changeset.find(params[:id])
117
118     if request.post?
119       @changeset.unsubscribe(current_user)
120
121       redirect_to changeset_path(@changeset)
122     end
123   rescue ActiveRecord::RecordNotFound
124     render :action => "no_such_entry", :status => :not_found
125   end
126
127   private
128
129   #------------------------------------------------------------
130   # utility functions below.
131   #------------------------------------------------------------
132
133   ##
134   # if a bounding box was specified do some sanity checks.
135   # restrict changesets to those enclosed by a bounding box
136   def conditions_bbox(changesets, bbox)
137     if bbox
138       bbox.check_boundaries
139       bbox = bbox.to_scaled
140
141       changesets.where("min_lon < ? and max_lon > ? and min_lat < ? and max_lat > ?",
142                        bbox.max_lon.to_i, bbox.min_lon.to_i,
143                        bbox.max_lat.to_i, bbox.min_lat.to_i)
144     else
145       changesets
146     end
147   end
148
149   ##
150   # eliminate empty changesets (where the bbox has not been set)
151   # this should be applied to all changeset list displays
152   def conditions_nonempty(changesets)
153     changesets.where("num_changes > 0")
154   end
155 end