]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/old_controller.rb
API JSON output
[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     # Set format to xml unless client requires a specific format
22     def default_format_xml
23       request.format = "xml" unless params[:format]
24     end
25
26     def history
27       # the .where() method used in the lookup_old_element_versions
28       # call won't throw an error if no records are found, so we have
29       # to do that ourselves.
30       raise OSM::APINotFoundError if @elements.empty?
31
32       # determine visible elements
33       @elems = if show_redactions?
34                  @elements
35                else
36                  @elements.unredacted
37                end
38
39       # Render the result
40       respond_to do |format|
41         format.xml
42         format.json
43       end
44     end
45
46     def version
47       if @old_element.redacted? && !show_redactions?
48         head :forbidden
49
50       else
51         response.last_modified = @old_element.timestamp
52
53         # Render the result
54         respond_to do |format|
55           format.xml
56           format.json
57         end
58       end
59     end
60
61     def redact
62       redaction_id = params["redaction"]
63       if redaction_id.nil?
64         # if no redaction ID was provided, then this is an unredact
65         # operation.
66         @old_element.redact!(nil)
67       else
68         # if a redaction ID was specified, then set this element to
69         # be redacted in that redaction.
70         redaction = Redaction.find(redaction_id.to_i)
71         @old_element.redact!(redaction)
72       end
73
74       # just return an empty 200 OK for success
75       head :ok
76     end
77
78     private
79
80     def show_redactions?
81       current_user&.moderator? && params[:show_redactions] == "true"
82     end
83   end
84 end