]> git.openstreetmap.org Git - rails.git/blob - app/controllers/changesets_controller.rb
Move the notes api methods into a controller in the api namespace
[rails.git] / app / controllers / changesets_controller.rb
1 # The ChangesetController is the RESTful interface to Changeset objects
2
3 class ChangesetsController < ApplicationController
4   layout "site"
5   require "xml/libxml"
6
7   skip_before_action :verify_authenticity_token, :except => [:index]
8   before_action :authorize_web
9   before_action :set_locale
10
11   authorize_resource
12
13   before_action(:only => [:index, :feed]) { |c| c.check_database_readable(true) }
14   around_action :web_timeout
15
16   # Helper methods for checking consistency
17   include ConsistencyValidations
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_id => user.id)
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_id => current_user.friend_users.identifiable)
58       elsif @params[:nearby] && current_user
59         changesets = changesets.where(:user_id => current_user.nearby)
60       end
61
62       changesets = changesets.where("changesets.id <= ?", @params[:max_id]) if @params[:max_id]
63
64       @edits = 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   private
77
78   #------------------------------------------------------------
79   # utility functions below.
80   #------------------------------------------------------------
81
82   ##
83   # if a bounding box was specified do some sanity checks.
84   # restrict changesets to those enclosed by a bounding box
85   # we need to return both the changesets and the 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