]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/old_controller.rb
b68fde6b55bd5a72e9c101e6d11a5f385d48ea4e
[rails.git] / app / controllers / api / old_controller.rb
1 # this class pulls together the logic for all the old_* controllers
2 # into one place. as it turns out, the API methods for historical
3 # nodes, ways and relations are basically identical.
4 module Api
5   class OldController < ApiController
6     require "xml/libxml"
7
8     before_action :setup_user_auth, :only => [:history, :version]
9     before_action :authorize, :only => [:redact]
10
11     authorize_resource
12
13     before_action :check_api_readable
14     before_action :check_api_writable, :only => [:redact]
15     around_action :api_call_handle_error, :api_call_timeout
16     before_action :lookup_old_element, :except => [:history]
17     before_action :lookup_old_element_versions, :only => [:history]
18
19     before_action :default_format_xml
20
21     def history
22       # the .where() method used in the lookup_old_element_versions
23       # call won't throw an error if no records are found, so we have
24       # to do that ourselves.
25       raise OSM::APINotFoundError if @elements.empty?
26
27       # determine visible elements
28       @elems = if show_redactions?
29                  @elements
30                else
31                  @elements.unredacted
32                end
33
34       # Render the result
35       respond_to do |format|
36         format.xml
37         format.json
38       end
39     end
40
41     def version
42       if @old_element.redacted? && !show_redactions?
43         head :forbidden
44
45       else
46         response.last_modified = @old_element.timestamp
47
48         # Render the result
49         respond_to do |format|
50           format.xml
51           format.json
52         end
53       end
54     end
55
56     def redact
57       redaction_id = params["redaction"]
58       if redaction_id.nil?
59         # if no redaction ID was provided, then this is an unredact
60         # operation.
61         @old_element.redact!(nil)
62       else
63         # if a redaction ID was specified, then set this element to
64         # be redacted in that redaction.
65         redaction = Redaction.find(redaction_id.to_i)
66         @old_element.redact!(redaction)
67       end
68
69       # just return an empty 200 OK for success
70       head :ok
71     end
72
73     private
74
75     def show_redactions?
76       current_user&.moderator? && params[:show_redactions] == "true"
77     end
78   end
79 end