]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/changeset_subscriptions_controller.rb
Merge pull request #6394 from openstreetmap/dependabot/github_actions/ruby/setup...
[rails.git] / app / controllers / api / changeset_subscriptions_controller.rb
1 # frozen_string_literal: true
2
3 module Api
4   class ChangesetSubscriptionsController < ApiController
5     before_action :check_api_writable
6     before_action :authorize
7
8     authorize_resource
9
10     before_action :require_public_data
11     before_action :set_request_formats
12
13     ##
14     # Adds a subscriber to the changeset
15     def create
16       # Check the arguments are sane
17       raise OSM::APIBadUserInput, "No id was given" unless params[:changeset_id]
18
19       # Extract the arguments
20       changeset_id = params[:changeset_id].to_i
21
22       # Find the changeset and check it is valid
23       @changeset = Changeset.find(changeset_id)
24       raise OSM::APIChangesetAlreadySubscribedError, @changeset if @changeset.subscribers.include?(current_user)
25
26       # Add the subscriber
27       @changeset.subscribers << current_user
28
29       respond_to do |format|
30         format.xml
31         format.json
32       end
33     end
34
35     ##
36     # Removes a subscriber from the changeset
37     def destroy
38       # Check the arguments are sane
39       raise OSM::APIBadUserInput, "No id was given" unless params[:changeset_id]
40
41       # Extract the arguments
42       changeset_id = params[:changeset_id].to_i
43
44       # Find the changeset and check it is valid
45       @changeset = Changeset.find(changeset_id)
46       raise OSM::APIChangesetNotSubscribedError, @changeset unless @changeset.subscribers.include?(current_user)
47
48       # Remove the subscriber
49       @changeset.subscribers.delete(current_user)
50
51       respond_to do |format|
52         format.xml
53         format.json
54       end
55     end
56   end
57 end