2012-08-15 05:32:59 +04:00
|
|
|
get "#{APIPREFIX}/comments/:comment_id" do |comment_id|
|
2015-12-14 10:33:55 +03:00
|
|
|
@comment = comment
|
|
|
|
comment_hash = @comment.to_hash(recursive: bool_recursive)
|
|
|
|
verify_or_fix_cached_comment_count(@comment, comment_hash)
|
|
|
|
comment_hash.to_json
|
2012-08-15 05:32:59 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
put "#{APIPREFIX}/comments/:comment_id" do |comment_id|
|
2014-07-02 00:57:45 +04:00
|
|
|
filter_blocked_content params["body"]
|
2014-07-03 05:20:07 +04:00
|
|
|
updated_content = params.slice(*%w[body endorsed])
|
|
|
|
if params.has_key?("endorsed")
|
|
|
|
new_endorsed_val = Boolean.mongoize(params["endorsed"])
|
|
|
|
if new_endorsed_val != comment.endorsed
|
2015-07-28 01:26:16 +03:00
|
|
|
if params["endorsement_user_id"].nil?
|
|
|
|
endorsement = nil
|
|
|
|
else
|
|
|
|
endorsement = {:user_id => params["endorsement_user_id"], :time => DateTime.now}
|
|
|
|
end
|
2014-07-03 05:20:07 +04:00
|
|
|
updated_content["endorsement"] = new_endorsed_val ? endorsement : nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
comment.update_attributes(updated_content)
|
2012-08-15 05:32:59 +04:00
|
|
|
if comment.errors.any?
|
|
|
|
error 400, comment.errors.full_messages.to_json
|
|
|
|
else
|
|
|
|
comment.to_hash.to_json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
post "#{APIPREFIX}/comments/:comment_id" do |comment_id|
|
2014-07-02 00:57:45 +04:00
|
|
|
filter_blocked_content params["body"]
|
2012-08-15 05:32:59 +04:00
|
|
|
sub_comment = comment.children.new(params.slice(*%w[body course_id]))
|
|
|
|
sub_comment.anonymous = bool_anonymous || false
|
2012-09-14 13:48:32 +04:00
|
|
|
sub_comment.anonymous_to_peers = bool_anonymous_to_peers || false
|
2012-08-15 05:32:59 +04:00
|
|
|
sub_comment.author = user
|
|
|
|
sub_comment.comment_thread = comment.comment_thread
|
2015-12-14 10:33:55 +03:00
|
|
|
sub_comment.child_count = 0
|
2012-08-15 05:32:59 +04:00
|
|
|
sub_comment.save
|
|
|
|
if sub_comment.errors.any?
|
|
|
|
error 400, sub_comment.errors.full_messages.to_json
|
|
|
|
else
|
2015-12-14 10:33:55 +03:00
|
|
|
comment.update_cached_child_count
|
|
|
|
if comment.errors.any?
|
|
|
|
error 400, comment.errors.full_messages.to_json
|
|
|
|
else
|
|
|
|
user.subscribe(comment.comment_thread) if bool_auto_subscribe
|
|
|
|
sub_comment.to_hash.to_json
|
|
|
|
end
|
2012-08-15 05:32:59 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
delete "#{APIPREFIX}/comments/:comment_id" do |comment_id|
|
2015-12-14 10:33:55 +03:00
|
|
|
parent_id = comment.parent_id
|
2012-08-15 05:32:59 +04:00
|
|
|
comment.destroy
|
2015-12-14 10:33:55 +03:00
|
|
|
unless parent_id.nil?
|
|
|
|
begin
|
|
|
|
parent_comment = Comment.find(parent_id)
|
|
|
|
parent_comment.update_cached_child_count
|
|
|
|
rescue Mongoid::Errors::DocumentNotFound
|
|
|
|
pass
|
|
|
|
end
|
|
|
|
end
|
2012-08-15 05:32:59 +04:00
|
|
|
comment.to_hash.to_json
|
|
|
|
end
|