I'm Keyvan Nayyeri, a 25 years old Ph.D. student at
the Computer Science department of
the University of Texas at San Antonio.
I'm also
a Software Architect and Developer and previously held a B.Sc.
degree in Applied Mathematics.
This is my blog where I publish content about various topics specifically Programming Languages and Compilers, Software
Engineering and Programming.
After a short while of being away from Graffiti stuff let me get back to Graffiti community by a new plug-in that focuses on another aspect of Graffiti API.
One of the features that is missing in default Graffiti installation is a way to track comments on a post for anonymous users. Site administrators, editors or contributors are able to receive email notifications for new comments on a post but anonymous users can't track comments for a specified post. There are two possible solutions for this and they are RSS feeds and email notifications. Neither RSS feed nor email subscriptions have been added to default Graffiti application so it's not easy to track comments for specified posts.
For some reasons such a feature is helpful because on a collaborative site users are discussing about a topic and may need to be aware of replies to their comments. Moreover, in a blog scenario this can keep commenters updated about replies to what they have left which is common, though.
Recently I found that having a plug-in for this purpose can be helpful at least for my own site where some commenters leave their comments, thoughts or questions but can't track comments. Of course, there are some solutions like CoComment that can be integrated but not all users are using it and I personally haven't integrated it with my site.
Wrapping up on this idea, I finally ended up with a Graffiti plug-in that automatically sends email notifications to any commenter that has left his or her email address on the site and is included in replies for a comment. So if a new commenter replies to a previous commenter then he or she will receive an email notification from the site.
The implementation for this plug-in is simple. AfterCommit event comes handy here and lets you get access to the new comment and work with it. The main body of the implementation is the event handler for this event that is shown below.
void ga_AfterCommit(DataBuddyBase dataObject, EventArgs e)
{
Comment comment = dataObject as Comment;
if (comment != null)
{
try
{
Column postID = new Column("PostId", DbType.Int32, typeof(Int32), "PostId", false, false);
CommentCollection comments = CommentCollection.FetchByColumn(postID, comment.PostId);
List<string> receivers = new List<string>();
Regex regEx = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
foreach (Comment tempComment in comments)
{
if (comment.Body.ToLower().Contains("@" + tempComment.Name.ToLower()))
{
if ((!string.IsNullOrEmpty(tempComment.Email)) &&
(regEx.IsMatch(tempComment.Email)))
{
receivers.Add(tempComment.Email);
}
}
}
if (receivers.Count > 0)
{
MailMessage email = new MailMessage(SiteSettings.Get().EmailFrom,
string.Join(";", receivers.ToArray()));
email.Subject = string.Format("New Reply to Your Comment on {0}",
SiteSettings.Get().Title);
email.Body = string.Format(_emailFormat, comment.Post.Title, comment.Name,
comment.Body, new Macros().FullUrl(comment.Post.Url));
email.IsBodyHtml = true;
email.Priority = MailPriority.Normal;
if (Emailer.SendMailMessage(email))
Log.Info(this.Name + " Plug-In", "Email sent.");
else
Log.Error(this.Name + " Plug-In", "Email could not be sent.");
}
}
catch (Exception ex)
{
Log.Error(this.Name + " Plug-In", ex.Message + ex.StackTrace);
}
}
}
This implementation heavily relies on two points in Graffiti API. One is about fetching comments for a specified post and the other is sending emails in Graffiti API.
Fetching the comments list for a specified post is as simple as calling the CommentCollection.FetchByColumn by passing the PostId column with the value of the identifier of the post.
In the next step I iterated through the list of comments and added the list of all recipients emails to a list then created a MailMessage with a specified template to send an email to all the recipients.
There are two ways to send an email in Graffiti. One is based on the existing email templates that let you send emails for pre-defined scenarios and the other is based on the .NET 2.0 (and later) emailing mechanism that uses a System.Net.Mail.MailMessage object.
Since my case was a special case I had to use my own email template and use a lower level API with MailMessage object to send the email. All I had to do was creating an instance of this object and setting its properties then sending the email by calling Emailer.SendMailMessage function.
Comment Reply Notifier plug-in is a part of Graffiti Extras project and will be included in version 1.0 but you can get your hands on the latest bits that contain this plug-in from here.
The usage is so easy. When you want to reply to someone, you just need to use an @ character before the name that he or she has used to leave the comment somewhere in your comment body. For example, you can refer to Keyvan by using "@Keyvan" text somewhere in the comment. Plug-in does the rest for you!
There are three points to mention. One is that Comment Reply Notifier is case-insensitive so you don't need to worry about anything about this. The other is you can include as many names as you like in a single comment and it will send emails to all of them. The last point is any registered or anonymous commenter can reply to other commenters and there isn't any limitation.
Scott Watermasysk
Feb 12, 2008 3:31 PM
#
Instead of:
Column postID = new Column("PostId", DbType.Int32, typeof(Int32), "PostId", false, false);
You can use: Comment.Columns.PostId.
HTH,
Scott
Keyvan Nayyeri
Feb 12, 2008 8:31 PM
#
@Scott Watermasysk:
Thank you for the point, Scott :-)
Ronald Widha
Apr 10, 2009 8:41 AM
#
FYI, I've never gotten comment reply notifier to work until now. I think it might have something to do with disabling: Disable Spam Comment Notification.
Very cool plugin.
Leave a Comment