Browse Source

Get reply emails going out

master
Peter J. Jones 11 years ago
parent
commit
0ef84d2853

+ 1
- 1
app/controllers/comments_controller.rb View File

@@ -30,6 +30,7 @@ class CommentsController < ApplicationController
################################################################################
def new
@comment = Comment.new
@comment.cached_parent_url = params[:back]
render(:action => (request.xhr? ? 'new.js.rjs' : 'new.html.erb'))
end
@@ -44,7 +45,6 @@ class CommentsController < ApplicationController
if @comment.save
# dude, rails is pissing me off by not rendering the correct action
render(:action => (request.xhr? ? 'create.js.rjs' : 'create.html.erb'))
BanterMailer.deliver_moderation_message(@comment)
return
end

+ 11
- 0
app/helpers/comments_helper.rb View File

@@ -36,7 +36,18 @@ module CommentsHelper
:controller => 'comments',
:action => 'new',
:reply_id => (comment ? comment.id : nil),
:back => comments_this_site_url + request.request_uri,
})
end
################################################################################
# Returns a URL that points back to the top of this site.
def comments_this_site_url
url = 'http'
url << 's' if request.ssl?
url << '://' << request.host
url << ':' << request.port.to_s unless [80, 443].include?(request.port)
url
end

end

+ 8
- 0
app/models/banter_mailer.rb View File

@@ -32,4 +32,12 @@ class BanterMailer < ActionMailer::Base
body(:comment => comment)
end
################################################################################
def reply_message (comment, watchers)
from(Banter::Config.from_email)
bcc(watchers)
subject(Banter::Config.subject_prefix + "Comment Reply")
body(:comment => comment)
end
end

+ 34
- 2
app/models/comment.rb View File

@@ -24,12 +24,16 @@
################################################################################
class Comment < ActiveRecord::Base
################################################################################
EMAIL_RE = /^[^@\s]+@(?:[-a-z0-9]+\.)+[a-z]{2,}$/i
################################################################################
attr_accessible(:reply_id, :author_name, :author_email, :author_url,
:wants_replies, :content)
:cached_parent_url, :wants_replies, :content)
################################################################################
validates_presence_of(:author_name, :content)
validates_format_of(:author_email, :with => EMAIL_RE, :allow_blank => true)
################################################################################
belongs_to(:banter, :polymorphic => true)
@@ -45,6 +49,27 @@ class Comment < ActiveRecord::Base
:order => "#{table_name}.created_at ASC",
})
end

################################################################################
# Approve the comment with the given ID, or approval code
def self.approve! (id)
conditions = ['']
conditions.first << (id.to_s.match(/^\d+$/) ? 'id=?' : 'approval_code=?')
conditions << id
if comment = find(:first, :conditions => conditions)
comment.approved = true
comment.save!
end
end
################################################################################
# Returns a list of email addresses of people that are monitoring
# the comment with the given ID.
def self.watchers (id)
comments = find(:all, :conditions => ['(id=? OR reply_id=?) AND wants_replies=?', id, id, true])
comments.map(&:author_email).reject {|e| e.blank?}
end
################################################################################
# Returns the author's URL, cleaned up if necessary.
@@ -52,7 +77,7 @@ class Comment < ActiveRecord::Base
url = self[:author_url]
url.blank? ? nil : "http://#{url}" unless url.match(/^http:\/\//i)
end
################################################################################
# Return a textual description of the comment's parent object.
def parent_description
@@ -99,6 +124,13 @@ class Comment < ActiveRecord::Base
before_save do |comment|
# Cache the contents as HTML
comment.content_cache = RedCloth.new(comment.content).to_html

# Send out reply notices
if comment.approved? and !comment.mail_sent? and !comment.reply_id.blank?
watchers = Comment.watchers(comment.reply_id)
comment.mail_sent = true
BanterMailer.deliver_reply_message(comment, watchers) unless watchers.empty?
end
end
end

+ 16
- 0
app/views/banter_mailer/reply_message.rhtml View File

@@ -0,0 +1,16 @@
A comment has been posted to a thread that you are watching:

<%= @comment.parent_description %>
<%= @comment.cached_parent_url %>
------
From: <%= @comment.author_name %>
<%= @comment.content %>
------


Comming Soon:
* Reply to this message to post a response
* Unsubscribe from this thread
* Way cool features

+ 1
- 0
app/views/comments/_form.html.erb View File

@@ -3,6 +3,7 @@

<% remote_form_for([@parent, @comment], :html => {:id => 'comment-form'}) do |form| %>
<%= error_messages_for(:comment) %>
<%= form.hidden_field(:cached_parent_url) %>
<%= hidden_field_tag('reply_id', params[:reply_id]) %>
<p>

+ 18
- 16
db/migrate/001_create_initial_schema.rb View File

@@ -4,22 +4,24 @@ class CreateInitialSchema < ActiveRecord::Migration
################################################################################
def self.up
create_table(:comments) do |t|
t.column(:banter_id, :integer)
t.column(:banter_type, :string)
t.column(:user_id, :integer)
t.column(:reply_id, :integer)
t.column(:author_name, :string, :default => 'Anonymous')
t.column(:author_email, :string)
t.column(:author_url, :string)
t.column(:author_ip, :string)
t.column(:wants_replies, :boolean, :default => false)
t.column(:was_edited, :boolean, :default => false)
t.column(:approved, :boolean, :default => false)
t.column(:approval_code, :string)
t.column(:content, :text)
t.column(:content_cache, :text)
t.column(:created_at, :datetime)
t.column(:updated_at, :datetime)
t.column(:banter_id, :integer)
t.column(:banter_type, :string)
t.column(:user_id, :integer)
t.column(:reply_id, :integer)
t.column(:author_name, :string, :default => 'Anonymous')
t.column(:author_email, :string)
t.column(:author_url, :string)
t.column(:author_ip, :string)
t.column(:mail_sent, :boolean, :default => false)
t.column(:wants_replies, :boolean, :default => false)
t.column(:was_edited, :boolean, :default => false)
t.column(:approved, :boolean, :default => false)
t.column(:approval_code, :string)
t.column(:cached_parent_url, :text)
t.column(:content, :text)
t.column(:content_cache, :text)
t.column(:created_at, :datetime)
t.column(:updated_at, :datetime)
end
end

Loading…
Cancel
Save