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