Comment subscriptions

A comprehensive solution for E-mailing blog comment authors when someone else comments on an entry is in development. In the mean time, does anyone have a module they’ve created that E-mails people when another comment is added? Just something rudimentary to put in place until ContentBox implements it in the core would be good.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Brad, very simple code at its best and what I use on my site. Currently no unsubscribe at the moment, just haven’t gotten around too it.

comment.cfc (Entity)

property name=“isSubscribed” notnull=“true” ormtype=“boolean” default=“false” dbdefault=“0” index=“idx_contentComment,idx_subscribed”;

Custom Interceptor

//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
public void function cbui_onCommentPost(event, interceptData) eventPattern="^contentbox-ui" {

if(interceptData.comment.getCommentId() != “”) {
var comment = commentService.get(interceptData.comment.getCommentId());
comment.setIsSubscribed(event.getValue(‘blogSubscribe’, false));
commentService.save( comment );

if(!interceptData.moderationResults.moderated) {
sendMailNotifications(interceptData.comment.getCommentId());

}
}
}

//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
private void function sendMailNotifications(required string commentId) {

var comments = commentService.getAll(listToArray(arguments.commentID), “createdDate asc”);
var settings = settingService.getAllSettings(asStruct=true);

for(var comment in comments) {
// Todo: People can only subscribe with blog posts at the moment, there might be a switch added later.
if(comment.getrelatedContent().getContentType() == ‘entry’) {
var results = {};
var criteria = commentService.newCriteria();
criteria.eq(“isApproved”, javaCast(“boolean”, true));
criteria.eq(“isSubscribed”, javaCast(“boolean”, true));
criteria.eq(“relatedContent.contentID”, javaCast(“int”, comment.getrelatedContent().getContentId() ));
criteria.ne( “authorEmail”, comment.getAuthorEmail() );

criteria.withProjections(distinct = “authorEmail:email”);
results = criteria.list();

var bodyTokens = comment.getMemento();
bodyTokens[“commentURL”] = CBHelper.linkComment( comment );
bodyTokens[“contentURL”] = CBHelper.linkContent( comment.getRelatedContent() );
bodyTokens[“contentTitle”] = comment.getParentTitle();

for(var email in results) {
log.info(“Sending email for moderated Post Id: #comment.getCommentId()# - sending email out to #email#”);

var mail = mailservice.newMail(to = email,
from = settings.cb_site_outgoingEmail,
subject = “New comment made for post: #bodyTokens.contentTitle#”,
type = “html”,
bodyTokens = bodyTokens,
server = settings.cb_site_mail_server,
username = settings.cb_site_mail_username,
password = settings.cb_site_mail_password,
port = settings.cb_site_mail_smtp,
useTLS = settings.cb_site_mail_tls,
useSSL = settings.cb_site_mail_ssl);

mail.setBody( renderer.get().renderExternalView(view="/contentbox/email_templates/comment_notify") );
mailService.send( mail );
}
}
}
}

Custom CommentForm Plugin - That extends the original but changes the renderIt() to included text for moderation and the following.



#html.checkBox( name = "blogSubscribe", checked = event.getValue("blogSubscribe", false))# #html.label( field = "test", content = "Subcribe to be notified of comments posted.", class="inline")#

Thanks Andrew! Did you add the new property directly to the core comment entity?

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Yes I couldn’t think of a better way to do it, as I have to add it again each time that gets updated.

I wrapped this up into a module and put it on ForgeBox for people to use until ContentBox rolls out official support for this.

I simplified it a bit to just automatically subscribe everyone. I Also enhanced it to catch when moderated comments are approved and send the E-mail then.

http://www.coldbox.org/forgebox/view/Comment-E-mailer

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Automatically subscribe people!

That will annoy people who don’t wish to receive things, I also did not do the update because if you approve the cooment it gets sent, sending it out again when unapproved and reapproved is not a good idea.

Automatically subscribe people!

The only people I’ve had who don’t subscribe to comments on my blog are spam and those using fake E-mail addresess. I don’t have many comments on my blog so I’m not too worried about it. If someone complains, i’ll just edit their comment and remove the E-mail address from it.

That will annoy people who don’t wish to receive things

Then they can stay away from my blog or don’t give me their E-mail address :slight_smile: I never put my E-mail into any website I don’t want to hear from again.

unapproved and reapproved

Why would I ever un-approve and re-approve a comment? Once it’s approved I never touch it again. Sure, it’s possible, but hardly worth worrying about.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

I know, but the thing is Brad I went through all scenarios and re-wrote that code that many times, but to automatically subscribe to people something that they have not requested to subscribe too, is not how you keep customers on your site.

As for the un-approve / re-approve then why did you add that into the interception point then?

sigh

Brad forget what I said…

I just realised what I was NOT saying…

It was my bad, here is the code I use

//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
public void function cbadmin_onCommentStatusUpdate(event, interceptData) {
log.info(“cbadmin_onCommentStatusUpdate #interceptData.status# = #listLen(interceptData.commentID)# Comment(s) #interceptData.commentID#”);

if(interceptData.status == ‘approve’) {
sendMailNotifications(interceptData.commentID);
}
}

So it is identical, except you seem to do something I don’t. Never had an issue with that myself, as I thought it was called on each comment approved anyway. Might have changed since I originally wrote that.

Just one last thing, best move the interceptor into a folder called interceptors, juts so that it keeps it tidy.

Yeah, I did a listToArray on the commentID in case multiple ones got approved and then call the sendMailNotifications() for each one.

Funny story-- I actually started to create an interceptors folder, but then I looked at one of the core interceptors in the main contentbox module and it was just a regular model in the site and it felt dumb having a folder inside of another empty folder for a single CFC so i just left it in the root. I’ve actually talked about creating an “interceptor” convention, but since they are referenced by class name it wouldn’t really matter.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Yeah, the CB way has always been a folder called interceptors so I just follow that, keeps everything where they are suppose to be. No big deal as long as people can identify that this is not a model or service, should be fine.

With the multiple approval, I am sure when I tested this with multiple approves, there is a loop that calls that interception, which is why I did what I did. But having said that, this code was written in the days ContentBox was V1.1 so the logic may have changed in ContentBox as well.

Actually, looking at the code Brad.

ContentBox does pass it a list, then the update passes that list to the sentMailNotification anyway, which then gets the list of approved emails. I think I will stick with my way, yours will send duplicate emails.

You’re right, I didn’t notice you already had a loop over comments. Mine won’t send duplicates though-- All i did was move the loop so the sendMailNotifications() only gets called with a single commentID at a time as opposed to being called once with a list of IDs. I’ll remove the unnecessary loop I added though since it’s not doing any good.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Cool, do you mind adding that the code is a modified version written by me?

Also I don’t recall seeing that you need to add the boolean property to the Entity either, might need to make sure that is clearly mentioned.

Sure.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

I left that out so people could install it without touching the core. That’s why everyone is subscribed :slight_smile:

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Yeah, I did look at the code afterwards and realized why you said what you said then.