]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/changesets/downloads_controller.rb
Add frozen_string_literal comments to ruby files
[rails.git] / app / controllers / api / changesets / downloads_controller.rb
1 # frozen_string_literal: true
2
3 module Api
4   module Changesets
5     class DownloadsController < ApiController
6       before_action :setup_user_auth
7
8       authorize_resource :changeset
9
10       before_action :set_request_formats
11
12       ##
13       # download the changeset as an osmChange document.
14       #
15       # to make it easier to revert diffs it would be better if the osmChange
16       # format were reversible, i.e: contained both old and new versions of
17       # modified elements. but it doesn't at the moment...
18       #
19       # this method cannot order the database changes fully (i.e: timestamp and
20       # version number may be too coarse) so the resulting diff may not apply
21       # to a different database. however since changesets are not atomic this
22       # behaviour cannot be guaranteed anyway and is the result of a design
23       # choice.
24       def show
25         changeset = Changeset.find(params[:changeset_id])
26
27         # get all the elements in the changeset which haven't been redacted
28         # and stick them in a big array.
29         elements = if show_redactions?
30                      [changeset.old_nodes,
31                       changeset.old_ways,
32                       changeset.old_relations].flatten
33                    else
34                      [changeset.old_nodes.unredacted,
35                       changeset.old_ways.unredacted,
36                       changeset.old_relations.unredacted].flatten
37                    end
38
39         # sort the elements by timestamp and version number, as this is the
40         # almost sensible ordering available. this would be much nicer if
41         # global (SVN-style) versioning were used - then that would be
42         # unambiguous.
43         elements.sort_by! { |e| [e.timestamp, e.version] }
44
45         # generate an output element for each operation. note: we avoid looking
46         # at the history because it is simpler - but it would be more correct to
47         # check these assertions.
48         @created = []
49         @modified = []
50         @deleted = []
51
52         elements.each do |elt|
53           if elt.version == 1
54             # first version, so it must be newly-created.
55             @created << elt
56           elsif elt.visible
57             # must be a modify
58             @modified << elt
59           else
60             # if the element isn't visible then it must have been deleted
61             @deleted << elt
62           end
63         end
64
65         respond_to do |format|
66           format.xml
67         end
68       end
69
70       private
71
72       def show_redactions?
73         current_user&.moderator? && params[:show_redactions] == "true"
74       end
75     end
76   end
77 end