diff --git a/src/Umbraco.Core/Services/INotificationService.cs b/src/Umbraco.Core/Services/INotificationService.cs index bfb9938e46..ccd0821b8a 100644 --- a/src/Umbraco.Core/Services/INotificationService.cs +++ b/src/Umbraco.Core/Services/INotificationService.cs @@ -30,7 +30,7 @@ namespace Umbraco.Core.Services Func createBody); /// - /// Sends the notifications for the specified user regarding the specified node and action. + /// Sends the notifications for the specified user regarding the specified nodes and action. /// /// /// diff --git a/src/Umbraco.Core/Services/NotificationService.cs b/src/Umbraco.Core/Services/NotificationService.cs index 18ad945122..5d51e20008 100644 --- a/src/Umbraco.Core/Services/NotificationService.cs +++ b/src/Umbraco.Core/Services/NotificationService.cs @@ -56,42 +56,35 @@ namespace Umbraco.Core.Services Func createBody) { if ((entity is IContent) == false) - { throw new NotSupportedException(); - } - var content = (IContent) entity; - //we'll lazily get these if we need to send notifications - IEnumerable allVersions = null; + + var content = (IContent) entity; + + // lazily get versions - into a list to ensure we can enumerate multiple times + List allVersions = null; int totalUsers; var allUsers = _userService.GetAll(0, int.MaxValue, out totalUsers); - foreach (var u in allUsers) + foreach (var u in allUsers.Where(x => x.IsApproved)) { - if (u.IsApproved == false) continue; - var userNotifications = GetUserNotifications(u, content.Path).ToArray(); + var userNotifications = GetUserNotifications(u, content.Path); var notificationForAction = userNotifications.FirstOrDefault(x => x.Action == action); - if (notificationForAction != null) + if (notificationForAction == null) continue; + + if (allVersions == null) // lazy load + allVersions = _contentService.GetVersions(entity.Id).ToList(); + + try { - //lazy load versions if notifications are required - if (allVersions == null) - { - allVersions = _contentService.GetVersions(entity.Id); - } + SendNotification(operatingUser, u, content, allVersions, + actionName, http, createSubject, createBody); - try - { - SendNotification( - operatingUser, u, content, - allVersions, - actionName, http, createSubject, createBody); - - - _logger.Debug(string.Format("Notification type: {0} sent to {1} ({2})", action, u.Name, u.Email)); - } - catch (Exception ex) - { - _logger.Error("An error occurred sending notification", ex); - } + _logger.Debug(string.Format("Notification type: {0} sent to {1} ({2})", + action, u.Name, u.Email)); + } + catch (Exception ex) + { + _logger.Error("An error occurred sending notification", ex); } } } @@ -114,56 +107,41 @@ namespace Umbraco.Core.Services Func createBody) { if ((entities is IEnumerable) == false) - { throw new NotSupportedException(); - } - //we'll lazily get these if we need to send notifications - Dictionary> allVersionsDictionary = new Dictionary>(); + // ensure we can enumerate multiple times + var entitiesL = entities as List ?? entities.Cast().ToList(); + + // lazily get versions - into lists to ensure we can enumerate multiple times + var allVersionsDictionary = new Dictionary>(); int totalUsers; var allUsers = _userService.GetAll(0, int.MaxValue, out totalUsers); - foreach (var u in allUsers) + foreach (var u in allUsers.Where(x => x.IsApproved)) { - if (u.IsApproved == false) continue; var userNotifications = GetUserNotifications(u).ToArray(); - foreach (var entity in entities) + foreach (var content in entitiesL) { - var content = (IContent) entity; - var userNotificationsByPath = FilterUserNotificationsByPath(userNotifications, content.Path); - var notificationForAction = userNotificationsByPath - .FirstOrDefault(x => x.Action == action); + var notificationForAction = userNotificationsByPath.FirstOrDefault(x => x.Action == action); + if (notificationForAction == null) continue; - if (notificationForAction != null) + var allVersions = allVersionsDictionary.ContainsKey(content.Id) // lazy load + ? allVersionsDictionary[content.Id] + : allVersionsDictionary[content.Id] = _contentService.GetVersions(content.Id).ToList(); + + try { - IEnumerable allVersions = null; - if (allVersionsDictionary.ContainsKey(entity.Id)) - { - allVersions = allVersionsDictionary[entity.Id]; - } + SendNotification(operatingUser, u, content, allVersions, + actionName, http, createSubject, createBody); - //lazy load versions if notifications are required - if (allVersions == null) - { - allVersions = _contentService.GetVersions(entity.Id); - allVersionsDictionary[entity.Id] = allVersions; - } - - try - { - SendNotification( - operatingUser, u, content, - allVersions, - actionName, http, createSubject, createBody); - - _logger.Debug(string.Format("Notification type: {0} sent to {1} ({2})", action, u.Name, u.Email)); - } - catch (Exception ex) - { - _logger.Error("An error occurred sending notification", ex); - } + _logger.Debug(string.Format("Notification type: {0} sent to {1} ({2})", + action, u.Name, u.Email)); + } + catch (Exception ex) + { + _logger.Error("An error occurred sending notification", ex); } } } @@ -193,9 +171,8 @@ namespace Umbraco.Core.Services /// public IEnumerable GetUserNotifications(IUser user, string path) { - var userNotifications = GetUserNotifications(user).ToArray(); - var result = FilterUserNotificationsByPath(userNotifications, path); - return result; + var userNotifications = GetUserNotifications(user); + return FilterUserNotificationsByPath(userNotifications, path); } /// @@ -204,10 +181,10 @@ namespace Umbraco.Core.Services /// /// /// - public IEnumerable FilterUserNotificationsByPath(IEnumerable userNotifications, string path) { + public IEnumerable FilterUserNotificationsByPath(IEnumerable userNotifications, string path) + { var pathParts = path.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries); - var result = userNotifications.Where(r => pathParts.InvariantContains(r.EntityId.ToString(CultureInfo.InvariantCulture))).ToList(); - return result; + return userNotifications.Where(r => pathParts.InvariantContains(r.EntityId.ToString(CultureInfo.InvariantCulture))).ToList(); } /// diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/webservices/nodeSorter.asmx.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/webservices/nodeSorter.asmx.cs index 21568a7ccf..505d715c3e 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/webservices/nodeSorter.asmx.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/webservices/nodeSorter.asmx.cs @@ -173,19 +173,13 @@ namespace umbraco.presentation.webservices private void SortContent(string[] ids, int parentId) { - var contentService = base.ApplicationContext.Services.ContentService; - var sortedContent = new List(); + var contentService = ApplicationContext.Services.ContentService; try { - int [] intIds = ids.Select(id => int.Parse(id)).ToArray(); + var intIds = ids.Select(int.Parse).ToArray(); + var allContent = contentService.GetByIds(intIds).ToDictionary(x => x.Id, x => x); + var sortedContent = intIds.Select(x => allContent[x]); - sortedContent = contentService.GetByIds(intIds).ToList(); - - sortedContent = (from id in intIds - join content in sortedContent - on id equals content.Id - select content).ToList(); - // Save content with new sort order and update db+cache accordingly var sorted = contentService.Sort(sortedContent);