]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/changesets_controller.rb
Fix test for change to field name
[rails.git] / app / controllers / api / changesets_controller.rb
1 # The ChangesetController is the RESTful interface to Changeset objects
2
3 module Api
4   class ChangesetsController < ApiController
5     include QueryMethods
6
7     before_action :check_api_writable, :only => [:create, :update]
8     before_action :setup_user_auth, :only => [:show]
9     before_action :authorize, :only => [:create, :update]
10
11     authorize_resource
12
13     before_action :require_public_data, :only => [:create, :update]
14     before_action :set_request_formats, :except => [:create]
15
16     # Helper methods for checking consistency
17     include ConsistencyValidations
18
19     ##
20     # query changesets by bounding box, time, user or open/closed status.
21     def index
22       raise OSM::APIBadUserInput, "cannot use order=oldest with time" if params[:time] && params[:order] == "oldest"
23
24       # find any bounding box
25       bbox = BoundingBox.from_bbox_params(params) if params["bbox"]
26
27       # create the conditions that the user asked for. some or all of
28       # these may be nil.
29       changesets = Changeset.all
30       changesets = conditions_bbox(changesets, bbox)
31       changesets = conditions_user(changesets, params["user"], params["display_name"])
32       changesets = conditions_time(changesets, params["time"])
33       changesets = query_conditions_time(changesets)
34       changesets = conditions_open(changesets, params["open"])
35       changesets = conditions_closed(changesets, params["closed"])
36       changesets = conditions_ids(changesets, params["changesets"])
37
38       # sort the changesets
39       changesets = if params[:order] == "oldest"
40                      changesets.order(:created_at => :asc)
41                    else
42                      changesets.order(:created_at => :desc)
43                    end
44
45       # limit the result
46       changesets = query_limit(changesets)
47
48       # preload users, tags and comments, and render result
49       @changesets = changesets.preload(:user, :changeset_tags, :comments)
50
51       respond_to do |format|
52         format.xml
53         format.json
54       end
55     end
56
57     ##
58     # Return XML giving the basic info about the changeset. Does not
59     # return anything about the nodes, ways and relations in the changeset.
60     def show
61       @changeset = Changeset.find(params[:id])
62       if params[:include_discussion].presence
63         @comments = @changeset.comments
64         @comments = @comments.unscope(:where => :visible) if params[:show_hidden_comments].presence && can?(:create, :changeset_comment_visibility)
65         @comments = @comments.includes(:author)
66       end
67
68       respond_to do |format|
69         format.xml
70         format.json
71       end
72     end
73
74     # Create a changeset from XML.
75     def create
76       cs = Changeset.from_xml(request.raw_post, :create => true)
77
78       # Assume that Changeset.from_xml has thrown an exception if there is an error parsing the xml
79       cs.user = current_user
80       cs.save_with_tags!
81
82       # Subscribe user to changeset comments
83       cs.subscribers << current_user
84
85       render :plain => cs.id.to_s
86     end
87
88     ##
89     # updates a changeset's tags. none of the changeset's attributes are
90     # user-modifiable, so they will be ignored.
91     #
92     # changesets are not (yet?) versioned, so we don't have to deal with
93     # history tables here. changesets are locked to a single user, however.
94     #
95     # after succesful update, returns the XML of the changeset.
96     def update
97       @changeset = Changeset.find(params[:id])
98       new_changeset = Changeset.from_xml(request.raw_post)
99
100       check_changeset_consistency(@changeset, current_user)
101       @changeset.update_from(new_changeset, current_user)
102       render "show"
103
104       respond_to do |format|
105         format.xml
106         format.json
107       end
108     end
109
110     private
111
112     #------------------------------------------------------------
113     # utility functions below.
114     #------------------------------------------------------------
115
116     ##
117     # if a bounding box was specified do some sanity checks.
118     # restrict changesets to those enclosed by a bounding box
119     def conditions_bbox(changesets, bbox)
120       if bbox
121         bbox.check_boundaries
122         bbox = bbox.to_scaled
123
124         changesets.where("min_lon < ? and max_lon > ? and min_lat < ? and max_lat > ?",
125                          bbox.max_lon.to_i, bbox.min_lon.to_i,
126                          bbox.max_lat.to_i, bbox.min_lat.to_i)
127       else
128         changesets
129       end
130     end
131
132     ##
133     # restrict changesets to those by a particular user
134     def conditions_user(changesets, user, name)
135       if user.nil? && name.nil?
136         changesets
137       else
138         # shouldn't provide both name and UID
139         raise OSM::APIBadUserInput, "provide either the user ID or display name, but not both" if user && name
140
141         # use either the name or the UID to find the user which we're selecting on.
142         u = if name.nil?
143               # user input checking, we don't have any UIDs < 1
144               raise OSM::APIBadUserInput, "invalid user ID" if user.to_i < 1
145
146               u = User.find_by(:id => user.to_i)
147             else
148               u = User.find_by(:display_name => name)
149             end
150
151         # make sure we found a user
152         raise OSM::APINotFoundError if u.nil?
153
154         # should be able to get changesets of public users only, or
155         # our own changesets regardless of public-ness.
156         unless u.data_public?
157           # get optional user auth stuff so that users can see their own
158           # changesets if they're non-public
159           setup_user_auth
160
161           raise OSM::APINotFoundError if current_user.nil? || current_user != u
162         end
163
164         changesets.where(:user => u)
165       end
166     end
167
168     ##
169     # restrict changesets to those during a particular time period
170     def conditions_time(changesets, time)
171       if time.nil?
172         changesets
173       elsif time.count(",") == 1
174         # if there is a range, i.e: comma separated, then the first is
175         # low, second is high - same as with bounding boxes.
176
177         # check that we actually have 2 elements in the array
178         times = time.split(",")
179         raise OSM::APIBadUserInput, "bad time range" if times.size != 2
180
181         from, to = times.collect { |t| Time.parse(t).utc }
182         changesets.where("closed_at >= ? and created_at <= ?", from, to)
183       else
184         # if there is no comma, assume its a lower limit on time
185         changesets.where(:closed_at => Time.parse(time).utc..)
186       end
187       # stupid Time seems to throw both of these for bad parsing, so
188       # we have to catch both and ensure the correct code path is taken.
189     rescue ArgumentError, RuntimeError => e
190       raise OSM::APIBadUserInput, e.message.to_s
191     end
192
193     ##
194     # return changesets which are open (haven't been closed yet)
195     # we do this by seeing if the 'closed at' time is in the future. Also if we've
196     # hit the maximum number of changes then it counts as no longer open.
197     # if parameter 'open' is nill then open and closed changesets are returned
198     def conditions_open(changesets, open)
199       if open.nil?
200         changesets
201       else
202         changesets.where("closed_at >= ? and num_changes <= ?",
203                          Time.now.utc, Changeset::MAX_ELEMENTS)
204       end
205     end
206
207     ##
208     # query changesets which are closed
209     # ('closed at' time has passed or changes limit is hit)
210     def conditions_closed(changesets, closed)
211       if closed.nil?
212         changesets
213       else
214         changesets.where("closed_at < ? or num_changes > ?",
215                          Time.now.utc, Changeset::MAX_ELEMENTS)
216       end
217     end
218
219     ##
220     # query changesets by a list of ids
221     # (either specified as array or comma-separated string)
222     def conditions_ids(changesets, ids)
223       if ids.nil?
224         changesets
225       elsif ids.empty?
226         raise OSM::APIBadUserInput, "No changesets were given to search for"
227       else
228         ids = ids.split(",").collect(&:to_i)
229         changesets.where(:id => ids)
230       end
231     end
232   end
233 end