1 # frozen_string_literal: true
 
   5     class DownloadsController < ApiController
 
   6       before_action :setup_user_auth
 
   8       authorize_resource :changeset
 
  10       before_action :set_request_formats
 
  13       # download the changeset as an osmChange document.
 
  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...
 
  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
 
  25         changeset = Changeset.find(params[:changeset_id])
 
  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?
 
  32                       changeset.old_relations].flatten
 
  34                      [changeset.old_nodes.unredacted,
 
  35                       changeset.old_ways.unredacted,
 
  36                       changeset.old_relations.unredacted].flatten
 
  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
 
  43         elements.sort_by! { |e| [e.timestamp, e.version] }
 
  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.
 
  52         elements.each do |elt|
 
  54             # first version, so it must be newly-created.
 
  60             # if the element isn't visible then it must have been deleted
 
  65         respond_to do |format|
 
  73         current_user&.moderator? && params[:show_redactions] == "true"