From c3048193c129b2ada3f293c6e9d98df11bc2d821 Mon Sep 17 00:00:00 2001 From: Kenn Jacobsen Date: Mon, 1 Mar 2021 20:12:36 +0100 Subject: [PATCH] Removed most of the static events in MediaService --- .../Events/DeleteRevisionsEventArgs.cs | 69 ------ .../Cache/DistributedCacheBinder_Handlers.cs | 15 -- .../Compose/NotificationsHandler.cs | 4 +- .../Compose/RelateOnTrashComposer.cs | 7 - ...shComponent.cs => RelateOnTrashHandler.cs} | 102 +++----- .../FileUploadPropertyEditor.cs | 45 ++-- .../ImageCropperPropertyEditor.cs | 36 +-- .../PropertyEditorsComponent.cs | 14 -- .../Services/CancelableNotification.cs | 33 ++- .../Services/Implement/ContentService.cs | 29 ++- .../Services/Implement/MediaService.cs | 219 +++++++----------- 11 files changed, 164 insertions(+), 409 deletions(-) delete mode 100644 src/Umbraco.Core/Events/DeleteRevisionsEventArgs.cs delete mode 100644 src/Umbraco.Infrastructure/Compose/RelateOnTrashComposer.cs rename src/Umbraco.Infrastructure/Compose/{RelateOnTrashComponent.cs => RelateOnTrashHandler.cs} (59%) diff --git a/src/Umbraco.Core/Events/DeleteRevisionsEventArgs.cs b/src/Umbraco.Core/Events/DeleteRevisionsEventArgs.cs deleted file mode 100644 index a7eb48107e..0000000000 --- a/src/Umbraco.Core/Events/DeleteRevisionsEventArgs.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System; - -namespace Umbraco.Cms.Core.Events -{ - public class DeleteRevisionsEventArgs : DeleteEventArgs, IEquatable - { - public DeleteRevisionsEventArgs(int id, bool canCancel, int specificVersion = default, bool deletePriorVersions = false, DateTime dateToRetain = default) - : base(id, canCancel) - { - DeletePriorVersions = deletePriorVersions; - SpecificVersion = specificVersion; - DateToRetain = dateToRetain; - } - - public DeleteRevisionsEventArgs(int id, int specificVersion = default, bool deletePriorVersions = false, DateTime dateToRetain = default) - : base(id) - { - DeletePriorVersions = deletePriorVersions; - SpecificVersion = specificVersion; - DateToRetain = dateToRetain; - } - - public bool DeletePriorVersions { get; } - public int SpecificVersion { get; } - public DateTime DateToRetain { get; } - - /// - /// Returns true if we are deleting a specific revision - /// - public bool IsDeletingSpecificRevision => SpecificVersion != default; - - public bool Equals(DeleteRevisionsEventArgs other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return base.Equals(other) && DateToRetain.Equals(other.DateToRetain) && DeletePriorVersions == other.DeletePriorVersions && SpecificVersion.Equals(other.SpecificVersion); - } - - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != GetType()) return false; - return Equals((DeleteRevisionsEventArgs) obj); - } - - public override int GetHashCode() - { - unchecked - { - int hashCode = base.GetHashCode(); - hashCode = (hashCode * 397) ^ DateToRetain.GetHashCode(); - hashCode = (hashCode * 397) ^ DeletePriorVersions.GetHashCode(); - hashCode = (hashCode * 397) ^ SpecificVersion.GetHashCode(); - return hashCode; - } - } - - public static bool operator ==(DeleteRevisionsEventArgs left, DeleteRevisionsEventArgs right) - { - return Equals(left, right); - } - - public static bool operator !=(DeleteRevisionsEventArgs left, DeleteRevisionsEventArgs right) - { - return !Equals(left, right); - } - } -} diff --git a/src/Umbraco.Infrastructure/Cache/DistributedCacheBinder_Handlers.cs b/src/Umbraco.Infrastructure/Cache/DistributedCacheBinder_Handlers.cs index eaa3c0df86..1aa4906029 100644 --- a/src/Umbraco.Infrastructure/Cache/DistributedCacheBinder_Handlers.cs +++ b/src/Umbraco.Infrastructure/Cache/DistributedCacheBinder_Handlers.cs @@ -183,14 +183,6 @@ namespace Umbraco.Cms.Core.Cache _distributedCache.RefreshContentCache(args.Changes.ToArray()); } - // TODO: our weird events handling wants this for now - private void ContentService_Deleted(IContentService sender, DeleteEventArgs e) { } - private void ContentService_Moved(IContentService sender, MoveEventArgs e) { } - private void ContentService_Trashed(IContentService sender, MoveEventArgs e) { } - private void ContentService_EmptiedRecycleBin(IContentService sender, RecycleBinEventArgs e) { } - private void ContentService_Published(IContentService sender, PublishEventArgs e) { } - private void ContentService_Unpublished(IContentService sender, PublishEventArgs e) { } - //private void ContentService_SavedBlueprint(IContentService sender, SaveEventArgs e) //{ // _distributedCache.RefreshUnpublishedPageCache(e.SavedEntities.ToArray()); @@ -396,13 +388,6 @@ namespace Umbraco.Cms.Core.Cache _distributedCache.RefreshMediaCache(args.Changes.ToArray()); } - // TODO: our weird events handling wants this for now - private void MediaService_Saved(IMediaService sender, SaveEventArgs e) { } - private void MediaService_Deleted(IMediaService sender, DeleteEventArgs e) { } - private void MediaService_Moved(IMediaService sender, MoveEventArgs e) { } - private void MediaService_Trashed(IMediaService sender, MoveEventArgs e) { } - private void MediaService_EmptiedRecycleBin(IMediaService sender, RecycleBinEventArgs e) { } - #endregion #region MemberService diff --git a/src/Umbraco.Infrastructure/Compose/NotificationsHandler.cs b/src/Umbraco.Infrastructure/Compose/NotificationsHandler.cs index 34c855286f..cfd7121191 100644 --- a/src/Umbraco.Infrastructure/Compose/NotificationsHandler.cs +++ b/src/Umbraco.Infrastructure/Compose/NotificationsHandler.cs @@ -27,7 +27,7 @@ namespace Umbraco.Cms.Core.Compose INotificationHandler>, INotificationHandler>, INotificationHandler>, - INotificationHandler>, + INotificationHandler>, INotificationHandler>, INotificationHandler>, INotificationHandler>, @@ -104,7 +104,7 @@ namespace Umbraco.Cms.Core.Compose } } - public void Handle(TrashedNotification notification) => _notifier.Notify(_actions.GetAction(), notification.MoveInfoCollection.Select(m => m.Entity).ToArray()); + public void Handle(MovedToRecycleBinNotification notification) => _notifier.Notify(_actions.GetAction(), notification.MoveInfoCollection.Select(m => m.Entity).ToArray()); public void Handle(CopiedNotification notification) => _notifier.Notify(_actions.GetAction(), notification.Original); diff --git a/src/Umbraco.Infrastructure/Compose/RelateOnTrashComposer.cs b/src/Umbraco.Infrastructure/Compose/RelateOnTrashComposer.cs deleted file mode 100644 index 8394dfc993..0000000000 --- a/src/Umbraco.Infrastructure/Compose/RelateOnTrashComposer.cs +++ /dev/null @@ -1,7 +0,0 @@ -using Umbraco.Cms.Core.Composing; - -namespace Umbraco.Cms.Core.Compose -{ - public sealed class RelateOnTrashComposer : ComponentComposer, ICoreComposer - { } -} diff --git a/src/Umbraco.Infrastructure/Compose/RelateOnTrashComponent.cs b/src/Umbraco.Infrastructure/Compose/RelateOnTrashHandler.cs similarity index 59% rename from src/Umbraco.Infrastructure/Compose/RelateOnTrashComponent.cs rename to src/Umbraco.Infrastructure/Compose/RelateOnTrashHandler.cs index cdba2d49bc..216b051c86 100644 --- a/src/Umbraco.Infrastructure/Compose/RelateOnTrashComponent.cs +++ b/src/Umbraco.Infrastructure/Compose/RelateOnTrashHandler.cs @@ -1,10 +1,8 @@ using System.Linq; -using Umbraco.Cms.Core.Composing; using Umbraco.Cms.Core.Events; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Scoping; using Umbraco.Cms.Core.Services; -using Umbraco.Cms.Core.Services.Implement; using Umbraco.Cms.Infrastructure.Services; using Umbraco.Extensions; @@ -14,7 +12,9 @@ namespace Umbraco.Cms.Core.Compose // TODO: lots of duplicate code in this one, refactor public sealed class RelateOnTrashHandler : INotificationHandler>, - INotificationHandler> + INotificationHandler>, + INotificationHandler>, + INotificationHandler> { private readonly IRelationService _relationService; private readonly IEntityService _entityService; @@ -38,9 +38,9 @@ namespace Umbraco.Cms.Core.Compose public void Handle(MovedNotification notification) { - foreach (var item in notification.MoveInfoCollection.Where(x => x.OriginalPath.Contains(Cms.Core.Constants.System.RecycleBinContentString))) + foreach (var item in notification.MoveInfoCollection.Where(x => x.OriginalPath.Contains(Constants.System.RecycleBinContentString))) { - const string relationTypeAlias = Cms.Core.Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias; + const string relationTypeAlias = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias; var relations = _relationService.GetByChildId(item.Entity.Id); foreach (var relation in relations.Where(x => x.RelationType.Alias.InvariantEquals(relationTypeAlias))) @@ -50,22 +50,20 @@ namespace Umbraco.Cms.Core.Compose } } - public void Handle(TrashedNotification notification) + public void Handle(MovedToRecycleBinNotification notification) { using (var scope = _scopeProvider.CreateScope()) { - const string relationTypeAlias = Cms.Core.Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias; + const string relationTypeAlias = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias; var relationType = _relationService.GetRelationTypeByAlias(relationTypeAlias); // check that the relation-type exists, if not, then recreate it if (relationType == null) { - var documentObjectType = Cms.Core.Constants.ObjectTypes.Document; - const string relationTypeName = - Cms.Core.Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteName; + var documentObjectType = Constants.ObjectTypes.Document; + const string relationTypeName = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteName; - relationType = new RelationType(relationTypeName, relationTypeAlias, false, documentObjectType, - documentObjectType); + relationType = new RelationType(relationTypeName, relationTypeAlias, false, documentObjectType, documentObjectType); _relationService.Save(relationType); } @@ -74,7 +72,7 @@ namespace Umbraco.Cms.Core.Compose var originalPath = item.OriginalPath.ToDelimitedList(); var originalParentId = originalPath.Count > 2 ? int.Parse(originalPath[originalPath.Count - 2]) - : Cms.Core.Constants.System.Root; + : Constants.System.Root; //before we can create this relation, we need to ensure that the original parent still exists which //may not be the case if the encompassing transaction also deleted it when this item was moved to the bin @@ -82,118 +80,76 @@ namespace Umbraco.Cms.Core.Compose if (_entityService.Exists(originalParentId)) { // Add a relation for the item being deleted, so that we can know the original parent for if we need to restore later - var relation = _relationService.GetByParentAndChildId(originalParentId, item.Entity.Id, relationType) ?? - new Relation(originalParentId, item.Entity.Id, relationType); + var relation = _relationService.GetByParentAndChildId(originalParentId, item.Entity.Id, relationType) ?? new Relation(originalParentId, item.Entity.Id, relationType); _relationService.Save(relation); _auditService.Add(AuditType.Delete, item.Entity.WriterId, item.Entity.Id, ObjectTypes.GetName(UmbracoObjectTypes.Document), - string.Format(_textService.Localize( - "recycleBin/contentTrashed"), - item.Entity.Id, originalParentId)); + string.Format(_textService.Localize("recycleBin/contentTrashed"), item.Entity.Id, originalParentId) + ); } } scope.Complete(); } } - } - - public sealed class RelateOnTrashComponent : IComponent - { - private readonly IRelationService _relationService; - private readonly IEntityService _entityService; - private readonly ILocalizedTextService _textService; - private readonly IAuditService _auditService; - private readonly IScopeProvider _scopeProvider; - - public RelateOnTrashComponent( - IRelationService relationService, - IEntityService entityService, - ILocalizedTextService textService, - IAuditService auditService, - IScopeProvider scopeProvider) + public void Handle(MovedNotification notification) { - _relationService = relationService; - _entityService = entityService; - _textService = textService; - _auditService = auditService; - _scopeProvider = scopeProvider; - } - - public void Initialize() - { - MediaService.Moved += MediaService_Moved; - MediaService.Trashed += MediaService_Trashed; - } - - public void Terminate() - { - MediaService.Moved -= MediaService_Moved; - MediaService.Trashed -= MediaService_Trashed; - } - - private void MediaService_Moved(IMediaService sender, MoveEventArgs e) - { - foreach (var item in e.MoveInfoCollection.Where(x => x.OriginalPath.Contains(Cms.Core.Constants.System.RecycleBinMediaString))) + foreach (var item in notification.MoveInfoCollection.Where(x => x.OriginalPath.Contains(Constants.System.RecycleBinMediaString))) { - const string relationTypeAlias = Cms.Core.Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteAlias; + const string relationTypeAlias = Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteAlias; var relations = _relationService.GetByChildId(item.Entity.Id); foreach (var relation in relations.Where(x => x.RelationType.Alias.InvariantEquals(relationTypeAlias))) { _relationService.Delete(relation); } } + } - public void MediaService_Trashed(IMediaService sender, MoveEventArgs e) + public void Handle(MovedToRecycleBinNotification notification) { using (var scope = _scopeProvider.CreateScope()) { - const string relationTypeAlias = - Cms.Core.Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteAlias; + const string relationTypeAlias = Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteAlias; var relationType = _relationService.GetRelationTypeByAlias(relationTypeAlias); // check that the relation-type exists, if not, then recreate it if (relationType == null) { - var documentObjectType = Cms.Core.Constants.ObjectTypes.Document; - const string relationTypeName = - Cms.Core.Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteName; - relationType = new RelationType(relationTypeName, relationTypeAlias, false, documentObjectType, - documentObjectType); + var documentObjectType = Constants.ObjectTypes.Document; + const string relationTypeName = Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteName; + relationType = new RelationType(relationTypeName, relationTypeAlias, false, documentObjectType, documentObjectType); _relationService.Save(relationType); } - foreach (var item in e.MoveInfoCollection) + foreach (var item in notification.MoveInfoCollection) { var originalPath = item.OriginalPath.ToDelimitedList(); var originalParentId = originalPath.Count > 2 ? int.Parse(originalPath[originalPath.Count - 2]) - : Cms.Core.Constants.System.Root; + : Constants.System.Root; //before we can create this relation, we need to ensure that the original parent still exists which //may not be the case if the encompassing transaction also deleted it when this item was moved to the bin if (_entityService.Exists(originalParentId)) { // Add a relation for the item being deleted, so that we can know the original parent for if we need to restore later - var relation = - _relationService.GetByParentAndChildId(originalParentId, item.Entity.Id, relationType) ?? - new Relation(originalParentId, item.Entity.Id, relationType); + var relation = _relationService.GetByParentAndChildId(originalParentId, item.Entity.Id, relationType) ?? new Relation(originalParentId, item.Entity.Id, relationType); _relationService.Save(relation); _auditService.Add(AuditType.Delete, item.Entity.CreatorId, item.Entity.Id, ObjectTypes.GetName(UmbracoObjectTypes.Media), - string.Format(_textService.Localize( - "recycleBin/mediaTrashed"), - item.Entity.Id, originalParentId)); + string.Format(_textService.Localize("recycleBin/mediaTrashed"), item.Entity.Id, originalParentId) + ); } } scope.Complete(); } + } } } diff --git a/src/Umbraco.Infrastructure/PropertyEditors/FileUploadPropertyEditor.cs b/src/Umbraco.Infrastructure/PropertyEditors/FileUploadPropertyEditor.cs index aa9e8089f2..6ef2be84ce 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/FileUploadPropertyEditor.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/FileUploadPropertyEditor.cs @@ -26,7 +26,9 @@ namespace Umbraco.Cms.Core.PropertyEditors Group = Constants.PropertyEditors.Groups.Media, Icon = "icon-download-alt")] // TODO: insert these notification handlers in core composition - public class FileUploadPropertyEditor : DataEditor, IMediaUrlGenerator, INotificationHandler>, INotificationHandler> + public class FileUploadPropertyEditor : DataEditor, IMediaUrlGenerator, + INotificationHandler>, INotificationHandler>, + INotificationHandler>, INotificationHandler> { private readonly IMediaFileSystem _mediaFileSystem; private readonly ContentSettings _contentSettings; @@ -124,37 +126,6 @@ namespace Umbraco.Cms.Core.PropertyEditors } } - /// - /// After a media has been created, auto-fill the properties. - /// - /// The event sender. - /// The event arguments. - internal void MediaServiceCreated(IMediaService sender, NewEventArgs args) - { - AutoFillProperties(args.Entity); - } - - /// - /// After a media has been saved, auto-fill the properties. - /// - /// The event sender. - /// The event arguments. - public void MediaServiceSaving(IMediaService sender, SaveEventArgs args) - { - foreach (var entity in args.SavedEntities) - AutoFillProperties(entity); - } - - /// - /// After a content item has been saved, auto-fill the properties. - /// - /// The event sender. - /// The event arguments. - public void ContentServiceSaving(IContentService sender, SaveEventArgs args) - { - foreach (var entity in args.SavedEntities) - AutoFillProperties(entity); - } public void Handle(CopiedNotification notification) { @@ -190,6 +161,16 @@ namespace Umbraco.Cms.Core.PropertyEditors public void Handle(DeletedNotification notification) => notification.MediaFilesToDelete.AddRange(ServiceDeleted(notification.DeletedEntities.OfType())); + public void Handle(DeletedNotification notification) => notification.MediaFilesToDelete.AddRange(ServiceDeleted(notification.DeletedEntities.OfType())); + + public void Handle(SavingNotification notification) + { + foreach (var entity in notification.SavedEntities) + { + AutoFillProperties(entity); + } + } + /// /// Auto-fill properties (or clear). /// diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ImageCropperPropertyEditor.cs b/src/Umbraco.Infrastructure/PropertyEditors/ImageCropperPropertyEditor.cs index 26b78cade5..ab1c4d9e67 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ImageCropperPropertyEditor.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ImageCropperPropertyEditor.cs @@ -33,7 +33,9 @@ namespace Umbraco.Cms.Core.PropertyEditors Group = Constants.PropertyEditors.Groups.Media, Icon = "icon-crop")] // TODO: insert these notification handlers in core composition - public class ImageCropperPropertyEditor : DataEditor, IMediaUrlGenerator, INotificationHandler>, INotificationHandler> + public class ImageCropperPropertyEditor : DataEditor, IMediaUrlGenerator, + INotificationHandler>, INotificationHandler>, + INotificationHandler>, INotificationHandler> { private readonly IMediaFileSystem _mediaFileSystem; private readonly ContentSettings _contentSettings; @@ -215,36 +217,14 @@ namespace Umbraco.Cms.Core.PropertyEditors public void Handle(DeletedNotification notification) => notification.MediaFilesToDelete.AddRange(ServiceDeleted(notification.DeletedEntities.OfType())); - /// - /// After a media has been created, auto-fill the properties. - /// - /// The event sender. - /// The event arguments. - public void MediaServiceCreated(IMediaService sender, NewEventArgs args) - { - AutoFillProperties(args.Entity); - } + public void Handle(DeletedNotification notification) => notification.MediaFilesToDelete.AddRange(ServiceDeleted(notification.DeletedEntities.OfType())); - /// - /// After a media has been saved, auto-fill the properties. - /// - /// The event sender. - /// The event arguments. - public void MediaServiceSaving(IMediaService sender, SaveEventArgs args) + public void Handle(SavingNotification notification) { - foreach (var entity in args.SavedEntities) - AutoFillProperties(entity); - } - - /// - /// After a content item has been saved, auto-fill the properties. - /// - /// The event sender. - /// The event arguments. - public void ContentServiceSaving(IContentService sender, SaveEventArgs args) - { - foreach (var entity in args.SavedEntities) + foreach (var entity in notification.SavedEntities) + { AutoFillProperties(entity); + } } /// diff --git a/src/Umbraco.Infrastructure/PropertyEditors/PropertyEditorsComponent.cs b/src/Umbraco.Infrastructure/PropertyEditors/PropertyEditorsComponent.cs index c870d85139..37407bd856 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/PropertyEditorsComponent.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/PropertyEditorsComponent.cs @@ -40,13 +40,6 @@ namespace Umbraco.Cms.Core.PropertyEditors private void Initialize(FileUploadPropertyEditor fileUpload) { - MediaService.Saving += fileUpload.MediaServiceSaving; - _terminate.Add(() => MediaService.Saving -= fileUpload.MediaServiceSaving); - - void mediaServiceDeleted(IMediaService sender, DeleteEventArgs args) => args.MediaFilesToDelete.AddRange(fileUpload.ServiceDeleted(args.DeletedEntities.Cast())); - MediaService.Deleted += mediaServiceDeleted; - _terminate.Add(() => MediaService.Deleted -= mediaServiceDeleted); - void memberServiceDeleted(IMemberService sender, DeleteEventArgs args) => args.MediaFilesToDelete.AddRange(fileUpload.ServiceDeleted(args.DeletedEntities.Cast())); MemberService.Deleted += memberServiceDeleted; _terminate.Add(() => MemberService.Deleted -= memberServiceDeleted); @@ -54,13 +47,6 @@ namespace Umbraco.Cms.Core.PropertyEditors private void Initialize(ImageCropperPropertyEditor imageCropper) { - MediaService.Saving += imageCropper.MediaServiceSaving; - _terminate.Add(() => MediaService.Saving -= imageCropper.MediaServiceSaving); - - void mediaServiceDeleted(IMediaService sender, DeleteEventArgs args) => args.MediaFilesToDelete.AddRange(imageCropper.ServiceDeleted(args.DeletedEntities.Cast())); - MediaService.Deleted += mediaServiceDeleted; - _terminate.Add(() => MediaService.Deleted -= mediaServiceDeleted); - void memberServiceDeleted(IMemberService sender, DeleteEventArgs args) => args.MediaFilesToDelete.AddRange(imageCropper.ServiceDeleted(args.DeletedEntities.Cast())); MemberService.Deleted += memberServiceDeleted; _terminate.Add(() => MemberService.Deleted -= memberServiceDeleted); diff --git a/src/Umbraco.Infrastructure/Services/CancelableNotification.cs b/src/Umbraco.Infrastructure/Services/CancelableNotification.cs index bc4de2ce49..e564d87c5e 100644 --- a/src/Umbraco.Infrastructure/Services/CancelableNotification.cs +++ b/src/Umbraco.Infrastructure/Services/CancelableNotification.cs @@ -205,26 +205,26 @@ namespace Umbraco.Cms.Infrastructure.Services public IEnumerable> MoveInfoCollection => Target; } - public class TrashingNotification : CancelableObjectNotification>> + public class MovingToRecycleBinNotification : CancelableObjectNotification>> { - public TrashingNotification(MoveEventInfo target, EventMessages messages) : base(new[] { target }, messages) + public MovingToRecycleBinNotification(MoveEventInfo target, EventMessages messages) : base(new[] { target }, messages) { } - public TrashingNotification(IEnumerable> target, EventMessages messages) : base(target, messages) + public MovingToRecycleBinNotification(IEnumerable> target, EventMessages messages) : base(target, messages) { } public IEnumerable> MoveInfoCollection => Target; } - public class TrashedNotification : ObjectNotification>> + public class MovedToRecycleBinNotification : ObjectNotification>> { - public TrashedNotification(MoveEventInfo target, EventMessages messages) : base(new[] { target }, messages) + public MovedToRecycleBinNotification(MoveEventInfo target, EventMessages messages) : base(new[] { target }, messages) { } - public TrashedNotification(IEnumerable> target, EventMessages messages) : base(target, messages) + public MovedToRecycleBinNotification(IEnumerable> target, EventMessages messages) : base(target, messages) { } @@ -324,34 +324,27 @@ namespace Umbraco.Cms.Infrastructure.Services public IEnumerable UnpublishedEntities => Target; } - public class EmptiedRecycleBinNotification : INotification + public class EmptiedRecycleBinNotification : INotification where T : class { - public EmptiedRecycleBinNotification(Guid nodeObjectType, EventMessages messages) + public EmptiedRecycleBinNotification(EventMessages messages) { - NodeObjectType = nodeObjectType; Messages = messages; } - public Guid NodeObjectType { get; } - public EventMessages Messages { get; } - - public bool IsContentRecycleBin => NodeObjectType == Constants.ObjectTypes.Document; - - public bool IsMediaRecycleBin => NodeObjectType == Constants.ObjectTypes.Media; } - public class EmptyingRecycleBinNotification : EmptiedRecycleBinNotification, ICancelableNotification + public class EmptyingRecycleBinNotification : EmptiedRecycleBinNotification, ICancelableNotification where T : class { - public EmptyingRecycleBinNotification(Guid nodeObjectType, EventMessages messages) - : base(nodeObjectType, messages) + public EmptyingRecycleBinNotification(EventMessages messages) + : base(messages) { } public bool Cancel { get; set; } } - public class DeletedVersionsNotification : INotification + public class DeletedVersionsNotification : INotification where T : class { public DeletedVersionsNotification(int id, EventMessages messages, int specificVersion = default, bool deletePriorVersions = false, DateTime dateToRetain = default) { @@ -373,7 +366,7 @@ namespace Umbraco.Cms.Infrastructure.Services public DateTime DateToRetain { get; } } - public class DeletingVersionsNotification : DeletedVersionsNotification, ICancelableNotification + public class DeletingVersionsNotification : DeletedVersionsNotification, ICancelableNotification where T : class { public DeletingVersionsNotification(int id, EventMessages messages, int specificVersion = default, bool deletePriorVersions = false, DateTime dateToRetain = default) : base(id, messages, specificVersion, deletePriorVersions, dateToRetain) diff --git a/src/Umbraco.Infrastructure/Services/Implement/ContentService.cs b/src/Umbraco.Infrastructure/Services/Implement/ContentService.cs index 64ff7260c9..e8013ac5d9 100644 --- a/src/Umbraco.Infrastructure/Services/Implement/ContentService.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/ContentService.cs @@ -1796,7 +1796,7 @@ namespace Umbraco.Cms.Core.Services.Implement _eventAggregator.Publish(new UnpublishedNotification(content, evtMsgs)); } - DeleteLocked(scope, content); + DeleteLocked(scope, content, evtMsgs); scope.Events.Dispatch(TreeChanged, this, new TreeChange(content, TreeChangeTypes.Remove).ToEventArgs()); Audit(AuditType.Delete, userId, content.Id); @@ -1807,10 +1807,8 @@ namespace Umbraco.Cms.Core.Services.Implement return OperationResult.Succeed(evtMsgs); } - private void DeleteLocked(IScope scope, IContent content) + private void DeleteLocked(IScope scope, IContent content, EventMessages evtMsgs) { - var evtMsgs = EventMessagesFactory.Get(); - void DoDelete(IContent c) { _documentRepository.Delete(c); @@ -1849,7 +1847,7 @@ namespace Umbraco.Cms.Core.Services.Implement using (var scope = ScopeProvider.CreateScope()) { - var notification = new DeletingVersionsNotification(id, evtMsgs, dateToRetain: versionDate); + var notification = new DeletingVersionsNotification(id, evtMsgs, dateToRetain: versionDate); _eventAggregator.Publish(notification); if (notification.Cancel) { @@ -1860,7 +1858,7 @@ namespace Umbraco.Cms.Core.Services.Implement scope.WriteLock(Cms.Core.Constants.Locks.ContentTree); _documentRepository.DeleteVersions(id, versionDate); - _eventAggregator.Publish(new DeletedVersionsNotification(id, evtMsgs, dateToRetain: versionDate)); + _eventAggregator.Publish(new DeletedVersionsNotification(id, evtMsgs, dateToRetain: versionDate)); Audit(AuditType.Delete, userId, Cms.Core.Constants.System.Root, "Delete (by version date)"); scope.Complete(); @@ -1881,7 +1879,7 @@ namespace Umbraco.Cms.Core.Services.Implement using (var scope = ScopeProvider.CreateScope()) { - var notification = new DeletingVersionsNotification(id, evtMsgs, specificVersion: versionId); + var notification = new DeletingVersionsNotification(id, evtMsgs, specificVersion: versionId); _eventAggregator.Publish(notification); if (notification.Cancel) { @@ -1900,7 +1898,7 @@ namespace Umbraco.Cms.Core.Services.Implement if (c.VersionId != versionId && c.PublishedVersionId != versionId) // don't delete the current or published version _documentRepository.DeleteVersion(versionId); - _eventAggregator.Publish(new DeletedVersionsNotification(id, evtMsgs, specificVersion: versionId)); + _eventAggregator.Publish(new DeletedVersionsNotification(id, evtMsgs, specificVersion: versionId)); Audit(AuditType.Delete, userId, Cms.Core.Constants.System.Root, "Delete (by version)"); scope.Complete(); @@ -1924,7 +1922,7 @@ namespace Umbraco.Cms.Core.Services.Implement var originalPath = content.Path; var moveEventInfo = new MoveEventInfo(content, originalPath, Cms.Core.Constants.System.RecycleBinContent); - var notification = new TrashingNotification(moveEventInfo, evtMsgs); + var notification = new MovingToRecycleBinNotification(moveEventInfo, evtMsgs); _eventAggregator.Publish(notification); if (notification.Cancel) { @@ -1945,7 +1943,7 @@ namespace Umbraco.Cms.Core.Services.Implement .Select(x => new MoveEventInfo(x.Item1, x.Item2, x.Item1.ParentId)) .ToArray(); - _eventAggregator.Publish(new TrashedNotification(moveInfo, evtMsgs)); + _eventAggregator.Publish(new MovedToRecycleBinNotification(moveInfo, evtMsgs)); Audit(AuditType.Move, userId, content.Id, "Moved to recycle bin"); scope.Complete(); @@ -2091,7 +2089,6 @@ namespace Umbraco.Cms.Core.Services.Implement /// public OperationResult EmptyRecycleBin(int userId = Cms.Core.Constants.Security.SuperUserId) { - var nodeObjectType = Cms.Core.Constants.ObjectTypes.Document; var deleted = new List(); var evtMsgs = EventMessagesFactory.Get(); @@ -2104,7 +2101,7 @@ namespace Umbraco.Cms.Core.Services.Implement // are managed by Delete, and not here. // no idea what those events are for, keep a simplified version - var notification = new EmptyingRecycleBinNotification(nodeObjectType, evtMsgs); + var notification = new EmptyingRecycleBinNotification(evtMsgs); _eventAggregator.Publish(notification); if (notification.Cancel) { @@ -2117,11 +2114,11 @@ namespace Umbraco.Cms.Core.Services.Implement var contents = _documentRepository.Get(query).ToArray(); foreach (var content in contents) { - DeleteLocked(scope, content); + DeleteLocked(scope, content, evtMsgs); deleted.Add(content); } - _eventAggregator.Publish(new EmptiedRecycleBinNotification(nodeObjectType, evtMsgs)); + _eventAggregator.Publish(new EmptiedRecycleBinNotification(evtMsgs)); scope.Events.Dispatch(TreeChanged, this, deleted.Select(x => new TreeChange(x, TreeChangeTypes.Remove)).ToEventArgs()); Audit(AuditType.Delete, userId, Cms.Core.Constants.System.RecycleBinContent, "Recycle bin emptied"); @@ -2843,7 +2840,7 @@ namespace Umbraco.Cms.Core.Services.Implement // delete content // triggers the deleted event (and handles the files) - DeleteLocked(scope, content); + DeleteLocked(scope, content, evtMsgs); changes.Add(new TreeChange(content, TreeChangeTypes.Remove)); } @@ -2852,7 +2849,7 @@ namespace Umbraco.Cms.Core.Services.Implement .ToArray(); if (moveInfos.Length > 0) { - _eventAggregator.Publish(new TrashedNotification(moveInfos, evtMsgs)); + _eventAggregator.Publish(new MovedToRecycleBinNotification(moveInfos, evtMsgs)); } scope.Events.Dispatch(TreeChanged, this, changes.ToEventArgs()); diff --git a/src/Umbraco.Infrastructure/Services/Implement/MediaService.cs b/src/Umbraco.Infrastructure/Services/Implement/MediaService.cs index 60061ed9bf..b7fe23dd7f 100644 --- a/src/Umbraco.Infrastructure/Services/Implement/MediaService.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/MediaService.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Globalization; using System.IO; @@ -13,6 +13,7 @@ using Umbraco.Cms.Core.Scoping; using Umbraco.Cms.Core.Services.Changes; using Umbraco.Cms.Core.Strings; using Umbraco.Cms.Infrastructure.Persistence.Querying; +using Umbraco.Cms.Infrastructure.Services; using Umbraco.Extensions; namespace Umbraco.Cms.Core.Services.Implement @@ -30,11 +31,13 @@ namespace Umbraco.Cms.Core.Services.Implement private readonly IMediaFileSystem _mediaFileSystem; + private readonly IEventAggregator _eventAggregator; + #region Constructors public MediaService(IScopeProvider provider, IMediaFileSystem mediaFileSystem, ILoggerFactory loggerFactory, IEventMessagesFactory eventMessagesFactory, IMediaRepository mediaRepository, IAuditRepository auditRepository, IMediaTypeRepository mediaTypeRepository, - IEntityRepository entityRepository, IShortStringHelper shortStringHelper) + IEntityRepository entityRepository, IShortStringHelper shortStringHelper, IEventAggregator eventAggregator) : base(provider, loggerFactory, eventMessagesFactory) { _mediaFileSystem = mediaFileSystem; @@ -43,6 +46,7 @@ namespace Umbraco.Cms.Core.Services.Implement _mediaTypeRepository = mediaTypeRepository; _entityRepository = entityRepository; _shortStringHelper = shortStringHelper; + _eventAggregator = eventAggregator; } #endregion @@ -289,19 +293,22 @@ namespace Umbraco.Cms.Core.Services.Implement private void CreateMedia(IScope scope, Core.Models.Media media, IMedia parent, int userId, bool withIdentity) { + var evtMsgs = EventMessagesFactory.Get(); + media.CreatorId = userId; if (withIdentity) { - // if saving is cancelled, media remains without an identity - var saveEventArgs = new SaveEventArgs(media); - if (Saving.IsRaisedEventCancelled(saveEventArgs, this)) + var notification = new SavingNotification(media, evtMsgs); + _eventAggregator.Publish(notification); + if (notification.Cancel) + { return; + } _mediaRepository.Save(media); - saveEventArgs.CanCancel = false; - scope.Events.Dispatch(Saved, this, saveEventArgs); + _eventAggregator.Publish(new SavedNotification(media, evtMsgs)); scope.Events.Dispatch(TreeChanged, this, new TreeChange(media, TreeChangeTypes.RefreshNode).ToEventArgs()); } @@ -659,11 +666,15 @@ namespace Umbraco.Cms.Core.Services.Implement using (var scope = ScopeProvider.CreateScope()) { - var saveEventArgs = new SaveEventArgs(media, evtMsgs); - if (raiseEvents && scope.Events.DispatchCancelable(Saving, this, saveEventArgs)) + if (raiseEvents) { - scope.Complete(); - return OperationResult.Attempt.Cancel(evtMsgs); + var notification = new SavingNotification(media, evtMsgs); + _eventAggregator.Publish(notification); + if (notification.Cancel) + { + scope.Complete(); + return OperationResult.Attempt.Cancel(evtMsgs); + } } // poor man's validation? @@ -682,8 +693,7 @@ namespace Umbraco.Cms.Core.Services.Implement _mediaRepository.Save(media); if (raiseEvents) { - saveEventArgs.CanCancel = false; - scope.Events.Dispatch(Saved, this, saveEventArgs); + _eventAggregator.Publish(new SavedNotification(media, evtMsgs)); } var changeType = TreeChangeTypes.RefreshNode; scope.Events.Dispatch(TreeChanged, this, new TreeChange(media, changeType).ToEventArgs()); @@ -708,11 +718,15 @@ namespace Umbraco.Cms.Core.Services.Implement using (var scope = ScopeProvider.CreateScope()) { - var saveEventArgs = new SaveEventArgs(mediasA, evtMsgs); - if (raiseEvents && scope.Events.DispatchCancelable(Saving, this, new SaveEventArgs(mediasA, evtMsgs))) + if (raiseEvents) { - scope.Complete(); - return OperationResult.Attempt.Cancel(evtMsgs); + var notification = new SavingNotification(mediasA, evtMsgs); + _eventAggregator.Publish(notification); + if (notification.Cancel) + { + scope.Complete(); + return OperationResult.Attempt.Cancel(evtMsgs); + } } var treeChanges = mediasA.Select(x => new TreeChange(x, TreeChangeTypes.RefreshNode)); @@ -727,8 +741,7 @@ namespace Umbraco.Cms.Core.Services.Implement if (raiseEvents) { - saveEventArgs.CanCancel = false; - scope.Events.Dispatch(Saved, this, saveEventArgs); + _eventAggregator.Publish(new SavedNotification(mediasA, evtMsgs)); } scope.Events.Dispatch(TreeChanged, this, treeChanges.ToEventArgs()); Audit(AuditType.Save, userId == -1 ? 0 : userId, Cms.Core.Constants.System.Root, "Bulk save media"); @@ -754,7 +767,9 @@ namespace Umbraco.Cms.Core.Services.Implement using (var scope = ScopeProvider.CreateScope()) { - if (scope.Events.DispatchCancelable(Deleting, this, new DeleteEventArgs(media, evtMsgs))) + var notification = new DeletingNotification(media, evtMsgs); + _eventAggregator.Publish(notification); + if (notification.Cancel) { scope.Complete(); return OperationResult.Attempt.Cancel(evtMsgs); @@ -762,7 +777,7 @@ namespace Umbraco.Cms.Core.Services.Implement scope.WriteLock(Cms.Core.Constants.Locks.MediaTree); - DeleteLocked(scope, media); + DeleteLocked(scope, media, evtMsgs); scope.Events.Dispatch(TreeChanged, this, new TreeChange(media, TreeChangeTypes.Remove).ToEventArgs()); Audit(AuditType.Delete, userId, media.Id); @@ -773,13 +788,12 @@ namespace Umbraco.Cms.Core.Services.Implement return OperationResult.Attempt.Succeed(evtMsgs); } - private void DeleteLocked(IScope scope, IMedia media) + private void DeleteLocked(IScope scope, IMedia media, EventMessages evtMsgs) { void DoDelete(IMedia c) { _mediaRepository.Delete(c); - var args = new DeleteEventArgs(c, false); // raise event & get flagged files - scope.Events.Dispatch(Deleted, this, args); + _eventAggregator.Publish(new DeletedNotification(c, evtMsgs)); // media files deleted by QueuingEventDispatcher } @@ -815,36 +829,25 @@ namespace Umbraco.Cms.Core.Services.Implement { DeleteVersions(scope, true, id, versionDate, userId); scope.Complete(); - - //if (uow.Events.DispatchCancelable(DeletingVersions, this, new DeleteRevisionsEventArgs(id, dateToRetain: versionDate))) - //{ - // uow.Complete(); - // return; - //} - - //uow.WriteLock(Constants.Locks.MediaTree); - //var repository = uow.CreateRepository(); - //repository.DeleteVersions(id, versionDate); - - //uow.Events.Dispatch(DeletedVersions, this, new DeleteRevisionsEventArgs(id, false, dateToRetain: versionDate)); - //Audit(uow, AuditType.Delete, "Delete Media by version date, userId, Constants.System.Root); - - //uow.Complete(); } } private void DeleteVersions(IScope scope, bool wlock, int id, DateTime versionDate, int userId = Cms.Core.Constants.Security.SuperUserId) { - var args = new DeleteRevisionsEventArgs(id, dateToRetain: versionDate); - if (scope.Events.DispatchCancelable(DeletingVersions, this, args)) + var evtMsgs = EventMessagesFactory.Get(); + + var notification = new DeletingVersionsNotification(id, evtMsgs, dateToRetain: versionDate); + _eventAggregator.Publish(notification); + if (notification.Cancel) + { return; + } if (wlock) scope.WriteLock(Cms.Core.Constants.Locks.MediaTree); _mediaRepository.DeleteVersions(id, versionDate); - args.CanCancel = false; - scope.Events.Dispatch(DeletedVersions, this, args); + _eventAggregator.Publish(new DeletedVersionsNotification(id, evtMsgs, dateToRetain: versionDate)); Audit(AuditType.Delete, userId, Cms.Core.Constants.System.Root, "Delete Media by version date"); } @@ -858,10 +861,13 @@ namespace Umbraco.Cms.Core.Services.Implement /// Optional Id of the User deleting versions of a Media object public void DeleteVersion(int id, int versionId, bool deletePriorVersions, int userId = Cms.Core.Constants.Security.SuperUserId) { + var evtMsgs = EventMessagesFactory.Get(); + using (var scope = ScopeProvider.CreateScope()) { - var args = new DeleteRevisionsEventArgs(id, /*specificVersion:*/ versionId); - if (scope.Events.DispatchCancelable(DeletingVersions, this, args)) + var notification = new DeletingVersionsNotification(id, evtMsgs, specificVersion: versionId); + _eventAggregator.Publish(notification); + if (notification.Cancel) { scope.Complete(); return; @@ -879,8 +885,7 @@ namespace Umbraco.Cms.Core.Services.Implement _mediaRepository.DeleteVersion(versionId); - args.CanCancel = false; - scope.Events.Dispatch(DeletedVersions, this, args); + _eventAggregator.Publish(new DeletedVersionsNotification(id, evtMsgs, specificVersion: versionId)); Audit(AuditType.Delete, userId, Cms.Core.Constants.System.Root, "Delete Media by version"); scope.Complete(); @@ -911,8 +916,10 @@ namespace Umbraco.Cms.Core.Services.Implement var originalPath = media.Path; var moveEventInfo = new MoveEventInfo(media, originalPath, Cms.Core.Constants.System.RecycleBinMedia); - var moveEventArgs = new MoveEventArgs(true, evtMsgs, moveEventInfo); - if (scope.Events.DispatchCancelable(Trashing, this, moveEventArgs, nameof(Trashing))) + + var notification = new MovingToRecycleBinNotification(moveEventInfo, evtMsgs); + _eventAggregator.Publish(notification); + if (notification.Cancel) { scope.Complete(); return OperationResult.Attempt.Cancel(evtMsgs); @@ -923,9 +930,7 @@ namespace Umbraco.Cms.Core.Services.Implement scope.Events.Dispatch(TreeChanged, this, new TreeChange(media, TreeChangeTypes.RefreshBranch).ToEventArgs()); var moveInfo = moves.Select(x => new MoveEventInfo(x.Item1, x.Item2, x.Item1.ParentId)) .ToArray(); - moveEventArgs.MoveInfoCollection = moveInfo; - moveEventArgs.CanCancel = false; - scope.Events.Dispatch(Trashed, this, moveEventArgs, nameof(Trashed)); + _eventAggregator.Publish(new MovedToRecycleBinNotification(moveInfo, evtMsgs)); Audit(AuditType.Move, userId, media.Id, "Move Media to recycle bin"); scope.Complete(); @@ -962,8 +967,9 @@ namespace Umbraco.Cms.Core.Services.Implement throw new InvalidOperationException("Parent does not exist or is trashed."); // causes rollback var moveEventInfo = new MoveEventInfo(media, media.Path, parentId); - var moveEventArgs = new MoveEventArgs(true, evtMsgs, moveEventInfo); - if (scope.Events.DispatchCancelable(Moving, this, moveEventArgs, nameof(Moving))) + var notification = new MovingNotification(moveEventInfo, evtMsgs); + _eventAggregator.Publish(notification); + if (notification.Cancel) { scope.Complete(); return OperationResult.Attempt.Cancel(evtMsgs); @@ -979,9 +985,7 @@ namespace Umbraco.Cms.Core.Services.Implement var moveInfo = moves //changes .Select(x => new MoveEventInfo(x.Item1, x.Item2, x.Item1.ParentId)) .ToArray(); - moveEventArgs.MoveInfoCollection = moveInfo; - moveEventArgs.CanCancel = false; - scope.Events.Dispatch(Moved, this, moveEventArgs, nameof(Moved)); + _eventAggregator.Publish(new MovedNotification(moveInfo, evtMsgs)); Audit(AuditType.Move, userId, media.Id); scope.Complete(); } @@ -1050,7 +1054,6 @@ namespace Umbraco.Cms.Core.Services.Implement /// Optional Id of the User emptying the Recycle Bin public OperationResult EmptyRecycleBin(int userId = Cms.Core.Constants.Security.SuperUserId) { - var nodeObjectType = Cms.Core.Constants.ObjectTypes.Media; var deleted = new List(); var evtMsgs = EventMessagesFactory.Get(); // TODO: and then? @@ -1063,23 +1066,23 @@ namespace Umbraco.Cms.Core.Services.Implement // v7 EmptyingRecycleBin and EmptiedRecycleBin events are greatly simplified since // each deleted items will have its own deleting/deleted events. so, files and such // are managed by Delete, and not here. - var args = new RecycleBinEventArgs(nodeObjectType, evtMsgs); - - if (scope.Events.DispatchCancelable(EmptyingRecycleBin, this, args)) + var notification = new EmptyingRecycleBinNotification(evtMsgs); + _eventAggregator.Publish(notification); + if (notification.Cancel) { scope.Complete(); return OperationResult.Cancel(evtMsgs); } + // emptying the recycle bin means deleting whatever is in there - do it properly! var query = Query().Where(x => x.ParentId == Cms.Core.Constants.System.RecycleBinMedia); var medias = _mediaRepository.Get(query).ToArray(); foreach (var media in medias) { - DeleteLocked(scope, media); + DeleteLocked(scope, media, evtMsgs); deleted.Add(media); } - args.CanCancel = false; - scope.Events.Dispatch(EmptiedRecycleBin, this, args); + _eventAggregator.Publish(new EmptiedRecycleBinNotification(new EventMessages())); scope.Events.Dispatch(TreeChanged, this, deleted.Select(x => new TreeChange(x, TreeChangeTypes.Remove)).ToEventArgs()); Audit(AuditType.Delete, userId, Cms.Core.Constants.System.RecycleBinMedia, "Empty Media recycle bin"); scope.Complete(); @@ -1105,13 +1108,19 @@ namespace Umbraco.Cms.Core.Services.Implement var itemsA = items.ToArray(); if (itemsA.Length == 0) return true; + var evtMsgs = EventMessagesFactory.Get(); + using (var scope = ScopeProvider.CreateScope()) { - var args = new SaveEventArgs(itemsA); - if (raiseEvents && scope.Events.DispatchCancelable(Saving, this, args)) + if (raiseEvents) { - scope.Complete(); - return false; + var notification = new SavingNotification(itemsA, evtMsgs); + _eventAggregator.Publish(notification); + if (notification.Cancel) + { + scope.Complete(); + return false; + } } var saved = new List(); @@ -1137,8 +1146,7 @@ namespace Umbraco.Cms.Core.Services.Implement if (raiseEvents) { - args.CanCancel = false; - scope.Events.Dispatch(Saved, this, args); + _eventAggregator.Publish(new SavedNotification(itemsA, evtMsgs)); } scope.Events.Dispatch(TreeChanged, this, saved.Select(x => new TreeChange(x, TreeChangeTypes.RefreshNode)).ToEventArgs()); Audit(AuditType.Sort, userId, 0); @@ -1216,66 +1224,6 @@ namespace Umbraco.Cms.Core.Services.Implement #region Event Handlers - /// - /// Occurs before Delete - /// - public static event TypedEventHandler> Deleting; - - /// - /// Occurs after Delete - /// - public static event TypedEventHandler> Deleted; - - /// - /// Occurs before Delete Versions - /// - public static event TypedEventHandler DeletingVersions; - - /// - /// Occurs after Delete Versions - /// - public static event TypedEventHandler DeletedVersions; - - /// - /// Occurs before Save - /// - public static event TypedEventHandler> Saving; - - /// - /// Occurs after Save - /// - public static event TypedEventHandler> Saved; - - /// - /// Occurs before Media is moved to Recycle Bin - /// - public static event TypedEventHandler> Trashing; - - /// - /// Occurs after Media is moved to Recycle Bin - /// - public static event TypedEventHandler> Trashed; - - /// - /// Occurs before Move - /// - public static event TypedEventHandler> Moving; - - /// - /// Occurs after Move - /// - public static event TypedEventHandler> Moved; - - /// - /// Occurs before the Recycle Bin is emptied - /// - public static event TypedEventHandler EmptyingRecycleBin; - - /// - /// Occurs after the Recycle Bin has been Emptied - /// - public static event TypedEventHandler EmptiedRecycleBin; - /// /// Occurs after change. /// @@ -1307,6 +1255,7 @@ namespace Umbraco.Cms.Core.Services.Implement var changes = new List>(); var moves = new List<(IMedia, string)>(); var mediaTypeIdsA = mediaTypeIds.ToArray(); + var evtMsgs = EventMessagesFactory.Get(); using (var scope = ScopeProvider.CreateScope()) { @@ -1315,7 +1264,9 @@ namespace Umbraco.Cms.Core.Services.Implement var query = Query().WhereIn(x => x.ContentTypeId, mediaTypeIdsA); var medias = _mediaRepository.Get(query).ToArray(); - if (scope.Events.DispatchCancelable(Deleting, this, new DeleteEventArgs(medias))) + var notification = new DeletingNotification(medias, evtMsgs); + _eventAggregator.Publish(notification); + if (notification.Cancel) { scope.Complete(); return; @@ -1338,14 +1289,16 @@ namespace Umbraco.Cms.Core.Services.Implement // delete media // triggers the deleted event (and handles the files) - DeleteLocked(scope, media); + DeleteLocked(scope, media, evtMsgs); changes.Add(new TreeChange(media, TreeChangeTypes.Remove)); } var moveInfos = moves.Select(x => new MoveEventInfo(x.Item1, x.Item2, x.Item1.ParentId)) .ToArray(); if (moveInfos.Length > 0) - scope.Events.Dispatch(Trashed, this, new MoveEventArgs(false, moveInfos), nameof(Trashed)); + { + _eventAggregator.Publish(new MovedToRecycleBinNotification(moveInfos, evtMsgs)); + } scope.Events.Dispatch(TreeChanged, this, changes.ToEventArgs()); Audit(AuditType.Delete, userId, Cms.Core.Constants.System.Root, $"Delete Media of types {string.Join(",", mediaTypeIdsA)}");