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