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