Sep 15 2009

Adding delete comment support to the Commentor extension

Category: Applications | .NetRory Primrose @ 09:24

The Commentor extension for BlogEngine.Net written by rtur is a great extension for catching comment spam on the blog. Unfortunately the custom rules you can apply to comments only support a Block or Allow action.

What I want is to prevent the comments being saved at all if a manual rule has been violated. This is better for my needs as I don’t need to clean up the comments on the blog and I don’t get hassled by constant emails regarding comments that require moderation.

The changes to the extension to support this are relatively minor.

First is a change to the User controls\Commentor\Settings.aspx user control. The action dropdown now has a Delete action.

<td>
    <asp:DropDownList ID="ddAction" runat="server">
        <asp:ListItem Text="Block" Value="Block" Selected=true></asp:ListItem>
        <asp:ListItem Text="Allow" Value="Allow" Selected=false></asp:ListItem>
        <asp:ListItem Text="Delete" Value="Delete" Selected=false></asp:ListItem>
    </asp:DropDownList>
</td>

The next set of changes were to implement the Delete action in the App_Code\Extensions\Commentor.cs class.

The ManualFilter enum definition has been extended to include the Delete action.

private enum ManualFilter { None, Block, Allow, Delete }

The Post_AddingComment method now supports this enum value in its processing of a new comment.

// validate against filter
switch (CheckFilter(comment))
{
    case ManualFilter.Block:
        comment.IsApproved = false;
        return;
    case ManualFilter.Allow:
        comment.IsApproved = true;
        return;
    case ManualFilter.Delete:
        comment.IsApproved = false;
        e.Cancel = true;
        return;
    default:
        break;
}

And finally, the CheckRow method has been updated to use the new Delete action.

if (match)
{
    if (action == "Block")
        return ManualFilter.Block;
    if (action == "Delete")
        return ManualFilter.Delete;
    return ManualFilter.Allow;
}

Make these changes and you will be able to use delete actions to prevent comments from being saved.

NOTE: The settings for existing rules will need to be updated where they are stored (most likely in a database or xml file) in order for them to use the Delete action. A restart of the website will be required to pick up these changes that were made directly against the settings datastore. Alternatively you will need to delete and recreate any existing custom rules that you want to use the Delete action with.

My Commentor extension settings now look like the following.

Commentor settings

Tags: , ,

Comments

1.
rtur rtur United States says:

Totally make sense, thanks for sharing. I might add something like it to comment management in the next BE release.

2.
Chad Albrecht Chad Albrecht United States says:

Thanks!  I was going to do this a bit differently but I decided to go with the way you did it.

3.
Juergen Juergen Germany says:

Hi,
thanks for this information. Since I wouild need the "Commentor" plugin - which is not included up to now I have to set the prerequisites for your adaptions. So I've bookmarked this post and hope that I can follow your instructions step by step as soon as I have installed the Commentor.

4.
Juergen Juergen Germany says:

Hi,
for your information:

As soon as I submitted my previous comment, there was a strange string between my comment and it's predecessor which looks like:

WhQECkfCHsw8Cyqvt2wMC1eX3owwCruLkxgwC/ubfrwUCwYnNwAkCwYnlwAkCwond3....[continued]....

5.
Rory Rory Australia says:

I think that is something to do with the spam calculation with the remote spam services. I haven't looked into this as yet, but it isn't a big issue other than how it looks.

6.
Keith Bauer Keith Bauer United States says:

I really am having issues with making this work. I'm pretty sure that

private enum ManualFilter { None, Block, Allow }  needs to be
private enum ManualFilter { None, Block, Allow, Delete } .

Once I changed that over, it does not have any errors, but it still does not delete the incoming blogs that are supposed to be deleted.

7.
Rory Rory Australia says:

Thanks Keith. I must have copied the original code for that snippet rather than the new code. Did you follow all the other steps?

Does the UI display the new Delete option?
Can you add a rule with the new delete option?
What happens when you add a comment that violates the new delete rule?
Can you debug saving a comment with Visual Studio?

8.
Keith Bauer Keith Bauer United States says:

Yea, I did get it to work. Everything shows up and works. This is very helpful code.

I do have another issue though with Commentor. I want to allow all messages to post immediately. In order for the delete function to work, I need to enable moderation. But when I enable moderation, it blocks all messages from being posted immediately and they need to be approved. If I turn it off, then it approves everything but the delete function no longer works.

I need the blog to accept all blog immediately unless it doesn't pass one of the manual filter rules.

9.
Rory Rory Australia says:

That is an interesting scenario. I've had a quick look and I think there is an easy solution. Keep in mind though that I have done nothing but eyeball the BE code so you will have to test this thoroughly yourself.

I think all you might need to do is open up \User controls\CommentView.ascx.cs and find the following line (on or around line 74)

comment.IsApproved = !BlogSettings.Instance.EnableCommentsModeration;

Change this to

comment.IsApproved = true;

This marks the comment as approved by default regardless of whether moderation is turned on. Events then fire throughout the application that process comments being added which includes Commentor. The moderation of comments still needs to be turned on as any of these event handlers should skip their processing if it is not turned on. Commentor should then run the rules and process the approval according to your rules. If all the rules pass, the comment should remain as approved.

Let me know if this works.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading