Modified SendCommentMail to supress emails for spam comments, but need suggestions to make it better

Topics: ASP.NET 2.0
Jun 11, 2010 at 5:30 PM

Howdy folks,

I wrote a tiny hack for BlogEngine.NET's built-in SendCommentMail extension which supresses email for spam comments but still allows non-black listed comments to get sent. Here's what I did:

private static void Post_CommentAdded(object sender, EventArgs e)
    {
        Post post = (Post)((Comment)sender).Parent;
        if (post != null && BlogSettings.Instance.SendMailOnComment && !Thread.CurrentPrincipal.Identity.IsAuthenticated)
        {
            Comment comment = post.Comments[post.Comments.Count - 1];
            //ADDED: Inserted this if statement below to check to see if this comment was filtered by the black list or not.
            //If so, then don't send any email for this comment.
            if (!comment.ModeratedBy.Contains("Rule:black list"))
            {
             /* The rest of the SendCommentMail event handler.. */

             Utils.SendMailMessageAsync(mail);
            }
        }
    }

Just checking for a hard-coded "Rule:black list" value seems pretty brittle to me, so what I'd like to know from any of the developers who are more familiar with BlogEngine.NET's comment filter system is if there is a more robust way to check to see if a comment has been flagged as spam by any of the BlogEngine.NET comment filters. In addition, are there some negative repercussions to this method of filtering them (like, for instance, if somebody doesn't have moderation turned on in their BlogEngine.NET settings, could the ModeratedBy filed be null potentially?)

Thanks in advance!

Coordinator
Jun 11, 2010 at 11:21 PM

In the current source, email is not sent if comment is spam by checking "IsApproved" property:

// Do not send email if comment auto-moderated and is spam
if (BlogSettings.Instance.ModerationType == BlogSettings.Moderation.Auto && !comment.IsApproved) return;

Jun 11, 2010 at 11:55 PM

Isn't "IsApproved" only set once a human moderator has approved the comment?

Coordinator
Jun 12, 2010 at 1:20 AM

Only if you use manual moderation. This is why this check also looking for Moderation.Auto. With auto moderation, comment approved by default - and if any filter down the road rejects it, only then comment reverted to unapproved status.

Jun 12, 2010 at 2:21 AM

That makes sense - as for the source code you showed, which version of BlogEngine.NET is that? 1.6.1?

Coordinator
Jun 12, 2010 at 4:10 AM

Yes, 1.6.1 definitely have it.

Jun 12, 2010 at 7:40 AM

Well damn! I guess I should have updated fron 1.6 then before I went off and wrote that hack :p