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