diff --git a/app/assets/javascripts/answerbox/comment/destroy.coffee b/app/assets/javascripts/answerbox/comment/destroy.coffee new file mode 100644 index 00000000..19de53e5 --- /dev/null +++ b/app/assets/javascripts/answerbox/comment/destroy.coffee @@ -0,0 +1,18 @@ +$(document).on "click", "a[data-action=ab-comment-destroy]", (ev) -> + ev.preventDefault() + if confirm 'Are you sure?' + btn = $(this) + cid = btn[0].dataset.cId + $.ajax + url: '/ajax/destroy_comment' + type: 'POST' + data: + comment: cid + success: (data, status, jqxhr) -> + if data.success + $("li[data-comment-id=#{cid}]").slideUp() + showNotification data.message, data.success + error: (jqxhr, status, error) -> + console.log jqxhr, status, error + showNotification "An error occurred, a developer should check the console for details", false + complete: (jqxhr, status) -> \ No newline at end of file diff --git a/app/assets/javascripts/answerbox/comment/report.coffee b/app/assets/javascripts/answerbox/comment/report.coffee new file mode 100644 index 00000000..3f407a69 --- /dev/null +++ b/app/assets/javascripts/answerbox/comment/report.coffee @@ -0,0 +1,18 @@ +$(document).on "click", "a[data-action=ab-comment-report]", (ev) -> + ev.preventDefault() + if confirm 'Are you sure you want to report this comment?' + btn = $(this) + cid = btn[0].dataset.cId + $.ajax + url: '/ajax/report' + type: 'POST' + data: + id: cid + type: 'comment' + success: (data, status, jqxhr) -> + showNotification data.message, data.success + error: (jqxhr, status, error) -> + console.log jqxhr, status, error + showNotification "An error occurred, a developer should check the console for details", false + complete: (jqxhr, status) -> + btn.button "reset" \ No newline at end of file diff --git a/app/controllers/ajax/answer_controller.rb b/app/controllers/ajax/answer_controller.rb index 2ccf1797..47950c51 100644 --- a/app/controllers/ajax/answer_controller.rb +++ b/app/controllers/ajax/answer_controller.rb @@ -3,10 +3,10 @@ class Ajax::AnswerController < ApplicationController params.require :answer answer = Answer.find(params[:answer]) - - unless privileged? answer.user + + unless (current_user == answer.user) or (privileged? answer.user) @status = :nopriv - @message = "check yuor privlegs" + @message = "can't delete other people's answers" @success = false return end diff --git a/app/controllers/ajax/comment_controller.rb b/app/controllers/ajax/comment_controller.rb index 8fec4701..ead52c03 100644 --- a/app/controllers/ajax/comment_controller.rb +++ b/app/controllers/ajax/comment_controller.rb @@ -20,4 +20,29 @@ class Ajax::CommentController < ApplicationController @render = render_to_string(partial: 'shared/comments', locals: { a: answer }) @count = answer.comment_count end + + def destroy + params.require :comment + + @status = :err + @success = false + comment = Comment.find(params[:comment]) + + unless (current_user == comment.user) or (current_user == comment.answer.user) or (privileged? comment.user) + @status = :nopriv + @message = "can't delete other people's comments" + @success = false + return + end + + comment.user.decrement! :commented_count + comment.answer.decrement! :comment_count + Notification.denotify comment.answer.user, comment + @count = comment.answer.comment_count + comment.destroy + + @status = :okay + @message = "Successfully deleted comment." + @success = true + end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 5acd5940..cf7e294a 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -56,7 +56,7 @@ module ApplicationHelper end def privileged?(user) - (current_user && (current_user == user || current_user.admin?)) ? true : false + ((!current_user.nil?) && ((current_user == user) || current_user.mod?)) ? true : false end def gravatar_url(user) diff --git a/app/views/ajax/comment/destroy.json.jbuilder b/app/views/ajax/comment/destroy.json.jbuilder new file mode 100644 index 00000000..46291e5d --- /dev/null +++ b/app/views/ajax/comment/destroy.json.jbuilder @@ -0,0 +1,2 @@ +json.partial! 'ajax/shared/status' +json.count @count if @count \ No newline at end of file diff --git a/app/views/shared/_comments.html.haml b/app/views/shared/_comments.html.haml index dc9a2b4f..c4d8da41 100644 --- a/app/views/shared/_comments.html.haml +++ b/app/views/shared/_comments.html.haml @@ -15,17 +15,17 @@ %button.btn.btn-link.btn-sm.dropdown-toggle{data: { toggle: :dropdown }, aria: { expanded: :false }} %span.caret %ul.dropdown-menu.dropdown-menu-right{role: :menu} - - if privileged? a.user + - if privileged?(comment.user) or privileged?(a.user) %li.text-danger - %a{href: '#', name: 'ab-destroy', data: { a_id: comment.id }} + %a{href: '#', data: { action: 'ab-comment-destroy', c_id: comment.id }} %i.fa.fa-trash-o Delete %li - %a{href: '#', name: 'ab-report', data: { a_id: comment.id }} + %a{href: '#', data: { action: 'ab-comment-report', c_id: comment.id }} %i.fa.fa-exclamation-triangle Report %p.comments--content= comment.content - if user_signed_in? - .form-group.has-feedback{name: 'ab-comment-new-group', 'data-a-id' => a.id} + .form-group.has-feedback{name: 'ab-comment-new-group', data: { a_id: a.id }} %input.form-control.comments--box{type: :text, placeholder: 'Comment...', name: 'ab-comment-new', data: {a_id: a.id }} %span.text-muted.form-control-feedback.comments--count{id: "ab-comment-charcount-#{a.id}"} 160 \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index c1481105..db03651e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -62,6 +62,7 @@ Rails.application.routes.draw do match '/create_smile', to: 'smile#create', via: :post, as: :create_smile match '/destroy_smile', to: 'smile#destroy', via: :post, as: :destroy_smile match '/create_comment', to: 'comment#create', via: :post, as: :create_comment + match '/destroy_comment', to: 'comment#destroy', via: :post, as: :destroy_comment match '/report', to: 'report#create', via: :post, as: :report end