]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/old_controller.rb
9a86bded590ea8c143634480a01023514f1d2be5
[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 < ApplicationController
6     require "xml/libxml"
7
8     skip_before_action :verify_authenticity_token
9     before_action :setup_user_auth, :only => [:history, :version]
10     before_action :api_deny_access_handler
11     before_action :authorize, :only => [:redact]
12
13     authorize_resource
14
15     before_action :check_api_readable
16     before_action :check_api_writable, :only => [:redact]
17     around_action :api_call_handle_error, :api_call_timeout
18     before_action :lookup_old_element, :except => [:history]
19     before_action :lookup_old_element_versions, :only => [:history]
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       doc = OSM::API.new.get_xml_doc
28
29       visible_elements = if show_redactions?
30                            @elements
31                          else
32                            @elements.unredacted
33                          end
34
35       visible_elements.each do |element|
36         doc.root << element.to_xml_node
37       end
38
39       render :xml => doc.to_s
40     end
41
42     def version
43       if @old_element.redacted? && !show_redactions?
44         head :forbidden
45
46       else
47         response.last_modified = @old_element.timestamp
48
49         doc = OSM::API.new.get_xml_doc
50         doc.root << @old_element.to_xml_node
51
52         render :xml => doc.to_s
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