Files
Umbraco-CMS/src/Umbraco.Web/Strategies/NotificationsHandler.cs
Sebastiaan Janssen 6631a50a44 Merge branch 'v7/dev' into pr_5303
# Conflicts:
#	src/Umbraco.Web/Strategies/NotificationsHandler.cs
2019-04-22 13:03:00 +02:00

103 lines
4.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Models.EntityBase;
using Umbraco.Core.Services;
using umbraco;
using umbraco.BusinessLogic.Actions;
using Umbraco.Core.Models;
namespace Umbraco.Web.Strategies
{
/// <summary>
/// Subscribes to the relavent events in order to send out notifications
/// </summary>
public sealed class NotificationsHandler : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
base.ApplicationStarted(umbracoApplication, applicationContext);
ContentService.SentToPublish += (sender, args) =>
applicationContext.Services.NotificationService.SendNotification(
args.Entity, ActionToPublish.Instance, applicationContext);
//Send notifications for the published action
ContentService.Published += (sender, args) =>
args.PublishedEntities.ForEach(
content =>
applicationContext.Services.NotificationService.SendNotification(
content, ActionPublish.Instance, applicationContext));
//Send notifications for the update and created actions
ContentService.Saved += (sender, args) =>
{
var newEntities = new List<IContent>();
var updatedEntities = new List<IContent>();
//need to determine if this is updating or if it is new
foreach (var entity in args.SavedEntities)
{
var dirty = (IRememberBeingDirty) entity;
if (dirty.WasPropertyDirty("Id"))
{
//it's new
newEntities.Add(entity);
}
else
{
//it's updating
updatedEntities.Add(entity);
}
}
applicationContext.Services.NotificationService.SendNotification(newEntities, ActionNew.Instance, applicationContext);
applicationContext.Services.NotificationService.SendNotification(updatedEntities, ActionUpdate.Instance, applicationContext);
};
//Send notifications for the delete action
ContentService.Deleted += (sender, args) =>
args.DeletedEntities.ForEach(
content =>
applicationContext.Services.NotificationService.SendNotification(
content, ActionDelete.Instance, applicationContext));
//Send notifications for the unpublish action
ContentService.UnPublished += (sender, args) =>
args.PublishedEntities.ForEach(
content =>
applicationContext.Services.NotificationService.SendNotification(
content, ActionUnPublish.Instance, applicationContext));
//Send notifications for the rollback action
ContentService.RolledBack += (sender, args) => applicationContext.Services.NotificationService.SendNotification(
args.Entity, ActionRollback.Instance, applicationContext);
//Send notifications for the move and restore actions
ContentService.Moved += (sender, args) =>
{
// notify about the move for all moved items
foreach(var moveInfo in args.MoveInfoCollection)
{
applicationContext.Services.NotificationService.SendNotification(
moveInfo.Entity, ActionMove.Instance, applicationContext
);
}
// for any items being moved from the recycle bin (restored), explicitly notify about that too
foreach(var moveInfo in args.MoveInfoCollection.Where(m => m.OriginalPath.Contains(Constants.System.RecycleBinContentString)))
{
applicationContext.Services.NotificationService.SendNotification(
moveInfo.Entity, ActionRestore.Instance, applicationContext
);
}
};
}
}
}