Posted on 14/09/2012 14:37:24
Currently, there is no built-in way to let an administrator approve comments before they are published on the site.
However, with clever use of notifications you can actually make something that may be usable: First, subscribe to "Dynamicweb.Notifications.Commenting.OnAfterComment" and mark new comments in some way. In the code samples below, we add the string "[not approved]" before the Name on new comments.
[Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.Commenting.OnAfterComment)]
public class CommentingOnAfterCommentObserver1 : Dynamicweb.Extensibility.NotificationSubscriber {
public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args) {
Dynamicweb.Notifications.Commenting.CommentArgs commentArgs = args as Dynamicweb.Notifications.Commenting.CommentArgs;
// Mark comment as not approved
var comment = commentArgs.Comment;
comment.Name = string.Format("[not approved]", comment.Name);
comment.Save();
}
}
Then, subscribe to "Dynamicweb.Notifications.Commenting.OnBeforeRenderComments" and remove comments that are not yet approved, i.e. the ones with a Name starting with the string "[not approved]".
[Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.Commenting.OnBeforeRenderComments)]
public class CommentingOnBeforeRenderCommentsObserver1 : Dynamicweb.Extensibility.NotificationSubscriber {
public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args) {
Dynamicweb.Notifications.Commenting.OnBeforeRenderCommentsArgs onBeforeRenderCommentsArgs = args as Dynamicweb.Notifications.Commenting.OnBeforeRenderCommentsArgs;
var nonApprovedComments = new Dynamicweb.Content.Commenting.CommentCollection();
// Get non-approved comments
foreach (var comment in onBeforeRenderCommentsArgs.Comments) {
if (comment.Name.StartsWith("[not approved]")) {
nonApprovedComments.Add(comment);
}
}
// Remove all non-approved comments
foreach (var comment in nonApprovedComments) {
onBeforeRenderCommentsArgs.Comments.Remove(comment);
}
}
}
Now an administrator can edit comments and remove "[not approved]" from the Name to approved comments. This is a bit of a hack, but that doesn't mean that it's not useful.
Note: The template tag <!--@Comments.Count--> doesn't reflect the number of comments after filtering, but, if need be, this can be worked around by counting comments using JavaScript.
Best regards,
Mikkel