]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/changeset_comments_controller.rb
Refactor api controllers to inherit from a common ApiController
[rails.git] / app / controllers / api / changeset_comments_controller.rb
1 module Api
2   class ChangesetCommentsController < ApiController
3     before_action :authorize
4     before_action :api_deny_access_handler
5
6     authorize_resource
7
8     before_action :require_public_data, :only => [:create]
9     before_action :check_api_writable
10     before_action :check_api_readable, :except => [:create]
11     around_action :api_call_handle_error
12     around_action :api_call_timeout
13
14     ##
15     # Add a comment to a changeset
16     def create
17       # Check the arguments are sane
18       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
19       raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
20
21       # Extract the arguments
22       id = params[:id].to_i
23       body = params[:text]
24
25       # Find the changeset and check it is valid
26       changeset = Changeset.find(id)
27       raise OSM::APIChangesetNotYetClosedError, changeset if changeset.is_open?
28
29       # Add a comment to the changeset
30       comment = changeset.comments.create(:changeset => changeset,
31                                           :body => body,
32                                           :author => current_user)
33
34       # Notify current subscribers of the new comment
35       changeset.subscribers.visible.each do |user|
36         Notifier.changeset_comment_notification(comment, user).deliver_later if current_user != user
37       end
38
39       # Add the commenter to the subscribers if necessary
40       changeset.subscribers << current_user unless changeset.subscribers.exists?(current_user.id)
41
42       # Return a copy of the updated changeset
43       @changeset = changeset
44       render "api/changesets/changeset"
45     end
46
47     ##
48     # Sets visible flag on comment to false
49     def destroy
50       # Check the arguments are sane
51       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
52
53       # Extract the arguments
54       id = params[:id].to_i
55
56       # Find the changeset
57       comment = ChangesetComment.find(id)
58
59       # Hide the comment
60       comment.update(:visible => false)
61
62       # Return a copy of the updated changeset
63       @changeset = comment.changeset
64       render "api/changesets/changeset"
65     end
66
67     ##
68     # Sets visible flag on comment to true
69     def restore
70       # Check the arguments are sane
71       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
72
73       # Extract the arguments
74       id = params[:id].to_i
75
76       # Find the changeset
77       comment = ChangesetComment.find(id)
78
79       # Unhide the comment
80       comment.update(:visible => true)
81
82       # Return a copy of the updated changeset
83       @changeset = comment.changeset
84       render "api/changesets/changeset"
85     end
86   end
87 end