Skip to content

Commit

Permalink
comment unsubscribe for anonymous commenters
Browse files Browse the repository at this point in the history
  • Loading branch information
bborn committed Aug 8, 2009
1 parent 357a66e commit 3cca71c
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -7,6 +7,7 @@
* upgrades to Event functionality, including RSVPs (eksatx)
* Photo albums, Static pages and messages controller test (juafrlo)
* calendar_date_select gem is now required
* added ability to unsubscribe from comment notifications for anonymous comments

= 1.0.2
* Rails 2.3 compatibility
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/base_controller.rb
Expand Up @@ -162,6 +162,6 @@ def commentable_comments_url(commentable)
else
"#{polymorphic_path(commentable)}#comments"
end
end
end

end
102 changes: 56 additions & 46 deletions app/controllers/comments_controller.rb
@@ -1,5 +1,6 @@
class CommentsController < BaseController
before_filter :login_required, :except => [:index]
helper :comments
before_filter :login_required, :except => [:index, :unsubscribe]
before_filter :admin_or_moderator_required, :only => [:delete_selected]

if AppConfig.allow_anonymous_commenting
Expand Down Expand Up @@ -125,61 +126,70 @@ def delete_selected
redirect_to admin_comments_path
end
end

def unsubscribe
@comment = Comment.find(params[:comment_id])
if @comment.token.eql?(params[:token])
@comment.unsubscribe_notifications
flash[:notice] = :comment_unsubscribe_succeeded
end
redirect_to commentable_url(@comment)
end


private

def comment_type
return "User" unless params[:commentable_type]
params[:commentable_type].camelize
end

def comment_type
return "User" unless params[:commentable_type]
params[:commentable_type].camelize
end

def comment_id
params[:commentable_id] || params[:user_id]
end
def comment_id
params[:commentable_id] || params[:user_id]
end

def comment_link
params[:commentable_id] ? comments_path(@commentable.class.to_s.underscore, @commentable.id) : user_comments_path(@user)
end
def comment_link
params[:commentable_id] ? comments_path(@commentable.class.to_s.underscore, @commentable.id) : user_comments_path(@user)
end

def full_comment_link
"#{application_url}#{comment_link}"
end
def full_comment_link
"#{application_url}#{comment_link}"
end

def comment_rss_link
params[:commentable_id] ? comments_path(@commentable.class.to_s.underscore, @commentable.id, :format => :rss) : user_comments_path(@user, :format => :rss)
end
def comment_rss_link
params[:commentable_id] ? comments_path(@commentable.class.to_s.underscore, @commentable.id, :format => :rss) : user_comments_path(@user, :format => :rss)
end

def comment_title
def comment_title

return @comments.first.commentable_name if @comments.first
return @comments.first.commentable_name if @comments.first

type = comment_type.underscore
case type
when 'user'
@commentable.login
when 'post'
@commentable.title
when 'clipping'
@commentable.description || "Clipping from #{@user.login}"
when 'photo'
@commentable.description || "Photo from #{@user.login}"
else
@commentable.class.to_s.humanize
end
end
type = comment_type.underscore
case type
when 'user'
@commentable.login
when 'post'
@commentable.title
when 'clipping'
@commentable.description || "Clipping from #{@user.login}"
when 'photo'
@commentable.description || "Photo from #{@user.login}"
else
@commentable.class.to_s.humanize
end
end

def render_comments_rss_feed_for(comments, title)
render_rss_feed_for(comments,
{ :class => @commentable.class,
:feed => { :title => title,
:link => full_comment_link },
:item => { :title => :title_for_rss,
:description => :comment,
:link => Proc.new {|comment| commentable_url(comment)},
:pub_date => :created_at
}
})
end
def render_comments_rss_feed_for(comments, title)
render_rss_feed_for(comments,
{ :class => @commentable.class,
:feed => { :title => title,
:link => full_comment_link },
:item => { :title => :title_for_rss,
:description => :comment,
:link => Proc.new {|comment| commentable_url(comment)},
:pub_date => :created_at
}
})
end

end
3 changes: 1 addition & 2 deletions app/helpers/base_helper.rb
Expand Up @@ -10,8 +10,7 @@ def commentable_url(comment)
user_url(comment.recipient)+"#comment_#{comment.id}"
end
end



def forum_page?
%w(forums topics sb_posts).include?(@controller.controller_name)
end
Expand Down
1 change: 1 addition & 0 deletions app/helpers/comments_helper.rb
@@ -1,2 +1,3 @@
module CommentsHelper

end
11 changes: 11 additions & 0 deletions app/models/comment.rb
Expand Up @@ -106,6 +106,17 @@ def send_notifications
self.notify_previous_anonymous_commenters if AppConfig.allow_anonymous_commenting
end

def token
Digest::SHA1.hexdigest("#{id}--#{(user && user.email) || author_email}--#{created_at}")
end

def unsubscribe_notifications
update_attribute :notify_by_email, false
commentable.comments.find_all_by_author_email(author_email).each do |previous_comment|
previous_comment.update_attribute :notify_by_email, false
end
end

protected
def whitelist_attributes
self.comment = white_list(self.comment)
Expand Down
3 changes: 3 additions & 0 deletions app/models/user_notifier.rb
Expand Up @@ -55,6 +55,9 @@ def follow_up_comment_notice_anonymous(email, comment)
@subject += "#{comment.username} has commented on a #{comment.commentable_type} that you also commented on."
@body[:url] = commentable_url(comment)
@body[:comment] = comment
if comment.notify_by_email
@body[:unsubscribe_link] = url_for(:controller => 'comments', :action => 'unsubscribe', :comment_id => comment.id, :token => comment.token)
end
end

def new_forum_post_notice(user, post)
Expand Down
3 changes: 1 addition & 2 deletions app/views/user_notifier/comment_notice.html.erb
Expand Up @@ -5,5 +5,4 @@ Hey <%= @user.login %>!
<%= @url %>

Thanks,
The <%= AppConfig.community_name %> team

The <%= AppConfig.community_name %> team
Expand Up @@ -10,4 +10,9 @@ The <%= AppConfig.community_name %> team

---------------------------------------------------------------------------------
If you believe you received this notice in error, please contact support at
<%= AppConfig.support_email %>
<%= AppConfig.support_email %>
<% if @unsubscribe_link %>
To stop receiving updates about new comments on this <%= @comment.commentable_type %>, visit this URL:
<%= @unsubscribe_link %>
<% end %>
1 change: 1 addition & 0 deletions lang/base.yml
Expand Up @@ -119,6 +119,7 @@
#en: comments_deleted: Comments deleted
#en: comments_rss: Comments RSS
#en: community_tagline: CommunityEngine Rocks!
#en: comment_unsubscribe_succeeded: Thanks, you have been unsubscribed from notifications on this comment.
#en: compose: Compose
#en: compose_message: Compose message
#en: confirm_password: Confirm Password
Expand Down
2 changes: 2 additions & 0 deletions lang/ui/en-US.yml
Expand Up @@ -255,6 +255,8 @@ en:
comments_rss: Comments RSS
#en: community_tagline: CommunityEngine Rocks!
community_tagline: CommunityEngine Rocks!
#en: comment_unsubscribe_succeeded: Thanks, you have been unsubscribed from notifications on this comment.
comment_unsubscribe_succeeded: "Thanks, you have been unsubscribed from notifications on this comment."
#en: compose: Compose
compose: Compose
#en: compose_message: Compose message
Expand Down
19 changes: 19 additions & 0 deletions test/functional/comments_controller_test.rb
Expand Up @@ -148,9 +148,28 @@ def test_should_show_comments_index_rss_if_logged_in
assert !assigns(:comments).empty?
assert_response :success
end

def test_should_unsubscribe_with_token
AppConfig.allow_anonymous_commenting = true
comment = Comment.create!(:comment => 'foo', :author_email => 'bar@foo.com', :author_ip => '123.123.123', :recipient => users(:quentin), :commentable => users(:quentin), :notify_by_email => true)
AppConfig.allow_anonymous_commenting = false

post :unsubscribe, :comment_id => comment.id, :token => comment.token
assert comment.reload.notify_by_email.eql?(false)
end

def test_should_no_unsubscribe_with_bad_token
AppConfig.allow_anonymous_commenting = true
comment = Comment.create!(:comment => 'foo', :author_email => 'bar@foo.com', :author_ip => '123.123.123', :recipient => users(:quentin), :commentable => users(:quentin), :notify_by_email => true)
AppConfig.allow_anonymous_commenting = false

post :unsubscribe, :commentable_type => 'User', :commentable_id => users(:quentin).to_param, :comment_id => comment.id, :token => 'badtokengoeshere'
assert comment.reload.notify_by_email.eql?(true)
end


protected

def create_user_comment(options = {})
post :create, {:commentable_type => (options[:commentable_type] || "user"),
:commentable_id => (options[:user_id] || users(:quentin).id),
Expand Down
13 changes: 13 additions & 0 deletions test/unit/comment_test.rb
Expand Up @@ -73,6 +73,19 @@ def test_should_be_created_without_recipient
end
end

def test_should_unsubscribe_notifications
AppConfig.allow_anonymous_commenting = true
first_comment = Comment.create!(:comment => 'foo', :author_email => 'alicia@foo.com', :author_ip => '123.123.123', :recipient => users(:quentin), :commentable => users(:quentin), :notify_by_email => true)
comment = Comment.create!(:comment => 'bar', :author_email => 'alicia@foo.com', :author_ip => '123.123.123', :recipient => users(:quentin), :commentable => users(:quentin), :notify_by_email => true)
assert_equal first_comment.notify_by_email, true
assert_equal comment.notify_by_email, true
AppConfig.allow_anonymous_commenting = false

comment.unsubscribe_notifications
assert comment.reload.notify_by_email.eql?(false)
assert first_comment.reload.notify_by_email.eql?(false)
end


end

0 comments on commit 3cca71c

Please sign in to comment.