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