]> git.openstreetmap.org Git - rails.git/blob - app/controllers/changeset_comments_controller.rb
Rename comments_feed to index
[rails.git] / app / controllers / changeset_comments_controller.rb
1 class ChangesetCommentsController < ApplicationController
2   before_action :authorize_web, :only => [:index]
3   before_action :set_locale, :only => [:index]
4   before_action :authorize, :only => [:create, :hide_comment, :unhide_comment]
5   before_action :require_moderator, :only => [:hide_comment, :unhide_comment]
6   before_action :require_allow_write_api, :only => [:create, :hide_comment, :unhide_comment]
7   before_action :require_public_data, :only => [:create]
8   before_action :check_api_writable, :only => [:create, :hide_comment, :unhide_comment]
9   before_action :check_api_readable, :except => [:create, :index]
10   before_action(:only => [:index]) { |c| c.check_database_readable(true) }
11   around_action :api_call_handle_error, :except => [:index]
12   around_action :api_call_timeout, :except => [:index]
13   around_action :web_timeout, :only => [:index]
14
15   ##
16   # Add a comment to a changeset
17   def create
18     # Check the arguments are sane
19     raise OSM::APIBadUserInput, "No id was given" unless params[:id]
20     raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
21
22     # Extract the arguments
23     id = params[:id].to_i
24     body = params[:text]
25
26     # Find the changeset and check it is valid
27     changeset = Changeset.find(id)
28     raise OSM::APIChangesetNotYetClosedError, changeset if changeset.is_open?
29
30     # Add a comment to the changeset
31     comment = changeset.comments.create(:changeset => changeset,
32                                         :body => body,
33                                         :author => current_user)
34
35     # Notify current subscribers of the new comment
36     changeset.subscribers.visible.each do |user|
37       Notifier.changeset_comment_notification(comment, user).deliver_later if current_user != user
38     end
39
40     # Add the commenter to the subscribers if necessary
41     changeset.subscribers << current_user unless changeset.subscribers.exists?(current_user.id)
42
43     # Return a copy of the updated changeset
44     render :xml => changeset.to_xml.to_s
45   end
46
47   ##
48   # Sets visible flag on comment to false
49   def hide_comment
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     render :xml => comment.changeset.to_xml.to_s
64   end
65
66   ##
67   # Sets visible flag on comment to true
68   def unhide_comment
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     render :xml => comment.changeset.to_xml.to_s
83   end
84
85   ##
86   # Get a feed of recent changeset comments
87   def index
88     if params[:id]
89       # Extract the arguments
90       id = params[:id].to_i
91
92       # Find the changeset
93       changeset = Changeset.find(id)
94
95       # Return comments for this changeset only
96       @comments = changeset.comments.includes(:author, :changeset).limit(comments_limit)
97     else
98       # Return comments
99       @comments = ChangesetComment.includes(:author, :changeset).where(:visible => true).order("created_at DESC").limit(comments_limit).preload(:changeset)
100     end
101
102     # Render the result
103     respond_to do |format|
104       format.rss
105     end
106   rescue OSM::APIBadUserInput
107     head :bad_request
108   end
109
110   private
111
112   ##
113   # Get the maximum number of comments to return
114   def comments_limit
115     if params[:limit]
116       if params[:limit].to_i.positive? && params[:limit].to_i <= 10000
117         params[:limit].to_i
118       else
119         raise OSM::APIBadUserInput, "Comments limit must be between 1 and 10000"
120       end
121     else
122       100
123     end
124   end
125 end