This article extends the sample given in our documentation. The article depicts how all comments send their notifications while the comment is waiting to be approved. This however poses a problem - why do frontend users need to be notified that a comment has been posted while they are unable to see it before it is approved and published. Therefore this sample will extend the functionality such that Administrator users get notified then the commend is send for approval and all other users get a notification once the comment has been published.
Continuing from where the article leaves us we will need to only modify the implementation of the ICommentNotificationStrategy interface. First off we will need to modify the GetSubscriptionListKeys such that it returns an empty list of subscriber keys when the comment's status is marked as Waiting for Approval:
protected
override
IEnumerable<
string
> GetSubscriptionListKeys(ICommentEvent @
event
)
{
if
( @
event
.Item.Status == StatusConstants.WaitingForApproval)
{
return
new
List<
string
>();
}
else
{
return
base
.GetSubscriptionListKeys(@
event
);
}
}
Next we need to create a new list and populate it with admin users. This list will help us construct a new ISubscriberRequest list which will include all the comment subscribers for this notification. Since we want the comments to go to administrators only we filter the users based on their role and create a new SubscriberRequest for each user. All of this needs to be done in the GetDynamicSubscribers method.
protected
override
IEnumerable<ISubscriberRequest> GetDynamicSubscribers(ICommentEvent @
event
)
{
if
(@
event
.Item.Status == StatusConstants.WaitingForApproval)
{
var Admins =
new
List<ISubscriberRequest>();
List<User> users =
new
List<User>();
RoleManager roleMan = RoleManager.GetManager(SecurityManager.ApplicationRolesProviderName);
users = roleMan.GetUsersInRole(
"Administrators"
).ToList();
foreach
(var item
in
users)
{
var subscriberRequest =
new
SubscriberRequestProxy();
subscriberRequest.Email = item.Email;
subscriberRequest.FirstName = item.FirstName;
subscriberRequest.LastName = item.LastName;
subscriberRequest.ResolveKey = item.Id.ToString();
subscriberRequest.Disabled =
false
;
Admins.Add(subscriberRequest);
}
return
Admins;
}
else
{
return
null
;
}
At this point you are all set. You can find the complete code sample here.