Skip to content

Commit d23fd8d

Browse files
committed
add ability to mark comment as spam from the frontend
1 parent 37ab429 commit d23fd8d

File tree

7 files changed

+68
-49
lines changed

7 files changed

+68
-49
lines changed

app/controllers/application_controller.rb

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class ApplicationController < ActionController::Base
77
helper_method :current_user
88
helper_method :viewing_self?
99
helper_method :is_admin?
10+
helper_method :is_moderator?
1011
helper_method :viewing_user
1112
helper_method :round
1213

@@ -206,6 +207,10 @@ def is_moderator?
206207
signed_in? && current_user.role.in?(%w(admin moderator))
207208
end
208209

210+
def require_moderator!
211+
return head(:forbidden) unless is_moderator?
212+
end
213+
209214
def iphone_user_agent?
210215
request.env["HTTP_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/(Mobile\/.+Safari)/]
211216
end

app/controllers/comments_controller.rb

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class CommentsController < ApplicationController
22

33
before_action :access_required, only: [:update, :destroy]
4-
before_action :lookup_comment, only: [:edit, :update, :destroy, :like]
4+
5+
before_action :lookup_comment, only: [:edit, :update, :destroy, :like, :mark_as_spam]
56
before_action :verify_ownership, only: [:edit, :update, :destroy]
67
before_action :lookup_protip, only: [:create]
8+
before_action :require_moderator!, only: [:mark_as_spam]
79

810
def create
9-
create_comment_params = params.require(:comment).permit(:comment)
10-
11-
redirect_to_signup_if_unauthenticated(request.referer + "?" + (create_comment_params.try(:to_query) || ""), "You must signin/signup to add a comment") do
12-
@comment = @protip.comments.build(create_comment_params)
11+
redirect_to_signup_if_unauthenticated(request.referer + "?" + (comment_params.try(:to_query) || ""), "You must signin/signup to add a comment") do
12+
@comment = @protip.comments.build(comment_params)
1313

1414
@comment.user = current_user
1515
@comment.request_format = request.format.to_s
@@ -27,10 +27,8 @@ def create
2727
end
2828

2929
def update
30-
update_comment_params = params.require(:comment).permit(:comment)
31-
3230
respond_to do |format|
33-
if @comment.update_attributes(update_comment_params)
31+
if @comment.update_attributes(comment_params)
3432
format.html { redirect_to protip_path(params[:protip_id]) }
3533
format.json { head :ok }
3634
else
@@ -59,11 +57,19 @@ def like
5957
end
6058
end
6159

60+
def mark_as_spam
61+
@comment.mark_as_spam
62+
respond_to do |format|
63+
format.json { head :ok }
64+
format.js { head :ok }
65+
end
66+
end
67+
6268
private
6369

6470
def lookup_comment
65-
@comment = Comment.find(params[:id])
66-
lookup_protip
71+
@comment = Comment.includes(:protip).find(params[:id])
72+
@protip = @comment.protip
6773
end
6874

6975
def lookup_protip
@@ -73,4 +79,8 @@ def lookup_protip
7379
def verify_ownership
7480
redirect_to(root_url) unless (is_admin? or (@comment && @comment.authored_by?(current_user)))
7581
end
82+
83+
def comment_params
84+
params.require(:comment).permit(:comment)
85+
end
7686
end

app/models/comment.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Comment < ActiveRecord::Base
2525
include AuthorDetails
2626
include SpamFilter
2727

28-
belongs_to :protip
28+
belongs_to :protip, touch: true
2929
has_many :likes, as: :likable, dependent: :destroy
3030
after_create :generate_event
3131
after_save :commented_callback
@@ -47,6 +47,10 @@ class Comment < ActiveRecord::Base
4747
event :mark_as_spam do
4848
transition any => :marked_as_spam
4949
end
50+
51+
after_transition any => :marked_as_spam do |comment|
52+
comment.spam!
53+
end
5054
end
5155

5256
def commented_callback

app/views/comments/_comment.html.haml

-32
This file was deleted.

app/views/comments/_comment.html.slim

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
li.cf.comment class=(top_comment?(comment, comment_counter) ? 'top-comment' : '') id="comment_#{comment.id}" itemscope=true itemtype=meta_comment_schema_url itemprop=:comment
2+
meta itemprop=:commentTime content=comment.created_at
3+
meta itemprop=:name content=comment.id
4+
header.cf itemprop="creator"itemscope=true itemtype=meta_person_schema_url
5+
meta itemprop=:name content=comment.user.display_name
6+
meta itemprop=:alternateName content=comment.user.username
7+
.comment-avatar
8+
= image_tag(users_image_path(comment.user), class: 'avatar')
9+
10+
=link_to comment.user.username, profile_path(comment.user.username), class: 'comment-user', 'data-reply-to' => comment.user.username, 'itemprop' => 'author'
11+
=link_to comment_likes(comment), like_protip_comment_path(comment.protip.public_id , comment.id), 'data-remote' => 'true', 'data-method' => :post, class: "like #{comment_liked_class(comment)}", rel: "nofollow"
12+
=link_to('Spam!', mark_as_spam_protip_comment_path(comment.protip.public_id , comment.id), 'data-remote' => 'true', 'data-method' => :post, rel: 'nofollow') if is_moderator?
13+
14+
.comment itemprop=:commentText
15+
= raw sanitize(formatted_comment(comment.body))
16+
/ TODO: Rework the comment editing bar outside of the same element as the commentText
17+
- if can_edit_comment?(comment)
18+
.edit-comment.hidden
19+
= form_for [comment.protip, comment] do |f|
20+
= f.text_area :comment, label: false, rows: 5
21+
input type='submit' value='Save' class='button save'
22+
input type='button' value='Cancel' class='button cancel'
23+
- if signed_in?
24+
ul.edit-del.cf
25+
li.hidden.show-for-user data-user=comment.user.username
26+
=link_to 'Edit', '#', class: 'edit', onclick: 'return false;'
27+
li.hidden.show-for-user data-user=comment.user.username
28+
=link_to 'Delete', protip_comment_path(comment.protip.public_id, comment.id), class: 'delete', 'data-method' => :delete
29+
li.remove-for-user data-user=comment.user.username
30+
=link_to 'Reply', '#add-comment', class: 'reply', rel: 'nofollow'

app/views/protips/_protip.html.haml

+1-3
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@
110110
%h2.comments-header
111111
%i.fa.fa-comments
112112
Comments
113-
-# HACK: Ignore protip comments where the owner is non-existant
114-
-# TODO: Clean out old comments where the is no User associated
115-
%ul.comment-list= render protip.comments.select { |comment| comment.user }
113+
%ul.comment-list = render protip.comments.with_states(:active,:reported_as_spam)
116114
= render 'comments/add_comment'
117115

118116
- if defined?(:job) && !job.nil?

config/routes.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# == Route Map
22
#
3-
# GET /.json(.:format) #<Proc:0x007fcb9ed50810@/vagrant/config/routes.rb:243>
4-
# GET /teams/.json(.:format) #<Proc:0x007fcb9ed54dc0@/vagrant/config/routes.rb:244>
3+
# GET /.json(.:format) #<Proc:0x007f1cf36de838@/home/abdelkader/RubymineProjects/coderwall/config/routes.rb:234>
4+
# GET /teams/.json(.:format) #<Proc:0x007f1cf36dc4c0@/home/abdelkader/RubymineProjects/coderwall/config/routes.rb:235>
55
# /mail_view MailPreview
66
# protips_update GET|PUT /protips/update(.:format) protips#update
77
# protip_update GET|PUT /protip/update(.:format) protip#update
@@ -37,6 +37,7 @@
3737
# feature_protip POST /p/:id/feature(.:format) protips#feature
3838
# delete_tag_protip POST /p/:id/delete_tag/:topic(.:format) protips#delete_tag {:topic=>/[A-Za-z0-9#\$\+\-_\.(%23)(%24)(%2B)]+/}
3939
# like_protip_comment POST /p/:protip_id/comments/:id/like(.:format) comments#like {:id=>/\d+/}
40+
# mark_as_spam_protip_comment POST /p/:protip_id/comments/:id/mark_as_spam(.:format) comments#mark_as_spam {:id=>/\d+/}
4041
# protip_comments GET /p/:protip_id/comments(.:format) comments#index {:id=>/\d+/}
4142
# POST /p/:protip_id/comments(.:format) comments#create {:id=>/\d+/}
4243
# new_protip_comment GET /p/:protip_id/comments/new(.:format) comments#new {:id=>/\d+/}
@@ -288,7 +289,10 @@
288289
post 'delete_tag/:topic' => 'protips#delete_tag', as: :delete_tag, :topic => topic_regex
289290
end
290291
resources :comments, constraints: { id: /\d+/ } do
291-
member { post 'like' }
292+
member do
293+
post 'like'
294+
post 'mark_as_spam'
295+
end
292296
end
293297
end
294298

0 commit comments

Comments
 (0)