3     class DownloadsController < ApiController
 
   4       before_action :setup_user_auth
 
   6       authorize_resource :changeset
 
   8       before_action :set_request_formats
 
  11       # download the changeset as an osmChange document.
 
  13       # to make it easier to revert diffs it would be better if the osmChange
 
  14       # format were reversible, i.e: contained both old and new versions of
 
  15       # modified elements. but it doesn't at the moment...
 
  17       # this method cannot order the database changes fully (i.e: timestamp and
 
  18       # version number may be too coarse) so the resulting diff may not apply
 
  19       # to a different database. however since changesets are not atomic this
 
  20       # behaviour cannot be guaranteed anyway and is the result of a design
 
  23         changeset = Changeset.find(params[:changeset_id])
 
  25         # get all the elements in the changeset which haven't been redacted
 
  26         # and stick them in a big array.
 
  27         elements = if show_redactions?
 
  30                       changeset.old_relations].flatten
 
  32                      [changeset.old_nodes.unredacted,
 
  33                       changeset.old_ways.unredacted,
 
  34                       changeset.old_relations.unredacted].flatten
 
  37         # sort the elements by timestamp and version number, as this is the
 
  38         # almost sensible ordering available. this would be much nicer if
 
  39         # global (SVN-style) versioning were used - then that would be
 
  41         elements.sort_by! { |e| [e.timestamp, e.version] }
 
  43         # generate an output element for each operation. note: we avoid looking
 
  44         # at the history because it is simpler - but it would be more correct to
 
  45         # check these assertions.
 
  50         elements.each do |elt|
 
  52             # first version, so it must be newly-created.
 
  58             # if the element isn't visible then it must have been deleted
 
  63         respond_to do |format|
 
  71         current_user&.moderator? && params[:show_redactions] == "true"