using System; using System.Collections.Generic; using System.Linq; using Umbraco.Core.Models; using Umbraco.Core.Models.Entities; using Umbraco.Core.Models.Membership; using Umbraco.Core.Persistence.Dtos; using Umbraco.Core.Scoping; namespace Umbraco.Core.Persistence.Repositories.Implement { public class NotificationsRepository : INotificationsRepository { private readonly IScopeAccessor _scopeAccessor; public NotificationsRepository(IScopeAccessor scopeAccessor) { _scopeAccessor = scopeAccessor; } private IScope AmbientScope => _scopeAccessor.AmbientScope; public IEnumerable GetUsersNotifications(IEnumerable userIds, string action, IEnumerable nodeIds, Guid objectType) { var nodeIdsA = nodeIds.ToArray(); var sql = AmbientScope.SqlContext.Sql() .Select("DISTINCT umbracoNode.id nodeId, umbracoUser.id userId, umbracoNode.nodeObjectType, umbracoUser2NodeNotify.action") .From() .InnerJoin().On(left => left.NodeId, right => right.NodeId) .InnerJoin().On(left => left.UserId, right => right.Id) .Where(x => x.NodeObjectType == objectType) .Where(x => x.Disabled == false) // only approved users .Where(x => x.Action == action); // on the specified action if (nodeIdsA.Length > 0) sql .WhereIn(x => x.NodeId, nodeIdsA); // for the specified nodes sql .OrderBy(x => x.Id) .OrderBy(dto => dto.NodeId); return AmbientScope.Database.Fetch(sql).Select(x => new Notification(x.nodeId, x.userId, x.action, objectType)); } public IEnumerable GetUserNotifications(IUser user) { var sql = AmbientScope.SqlContext.Sql() .Select("DISTINCT umbracoNode.id, umbracoUser2NodeNotify.userId, umbracoNode.nodeObjectType, umbracoUser2NodeNotify.action") .From() .InnerJoin() .On(dto => dto.NodeId, dto => dto.NodeId) .Where(dto => dto.UserId == (int)user.Id) .OrderBy(dto => dto.NodeId); var dtos = AmbientScope.Database.Fetch(sql); //need to map the results return dtos.Select(d => new Notification(d.id, d.userId, d.action, d.nodeObjectType)).ToList(); } public IEnumerable SetNotifications(IUser user, IEntity entity, string[] actions) { DeleteNotifications(user, entity); return actions.Select(action => CreateNotification(user, entity, action)).ToList(); } public IEnumerable GetEntityNotifications(IEntity entity) { var sql = AmbientScope.SqlContext.Sql() .Select("DISTINCT umbracoNode.id, umbracoUser2NodeNotify.userId, umbracoNode.nodeObjectType, umbracoUser2NodeNotify.action") .From() .InnerJoin() .On(dto => dto.NodeId, dto => dto.NodeId) .Where(dto => dto.NodeId == entity.Id) .OrderBy(dto => dto.NodeId); var dtos = AmbientScope.Database.Fetch(sql); //need to map the results return dtos.Select(d => new Notification(d.id, d.userId, d.action, d.nodeObjectType)).ToList(); } public int DeleteNotifications(IEntity entity) { return AmbientScope.Database.Delete("WHERE nodeId = @nodeId", new { nodeId = entity.Id }); } public int DeleteNotifications(IUser user) { return AmbientScope.Database.Delete("WHERE userId = @userId", new { userId = user.Id }); } public int DeleteNotifications(IUser user, IEntity entity) { // delete all settings on the node for this user return AmbientScope.Database.Delete("WHERE userId = @userId AND nodeId = @nodeId", new { userId = user.Id, nodeId = entity.Id }); } public Notification CreateNotification(IUser user, IEntity entity, string action) { var sql = AmbientScope.SqlContext.Sql() .Select("DISTINCT nodeObjectType") .From() .Where(nodeDto => nodeDto.NodeId == entity.Id); var nodeType = AmbientScope.Database.ExecuteScalar(sql); var dto = new User2NodeNotifyDto { Action = action, NodeId = entity.Id, UserId = user.Id }; AmbientScope.Database.Insert(dto); return new Notification(dto.NodeId, dto.UserId, dto.Action, nodeType); } } }