2014-01-10 17:03:00 +11:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2021-02-09 10:22:42 +01:00
|
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
|
using Umbraco.Cms.Core.Models.Entities;
|
|
|
|
|
|
using Umbraco.Cms.Core.Models.Membership;
|
|
|
|
|
|
using Umbraco.Cms.Core.Persistence.Repositories;
|
2021-02-15 11:41:12 +01:00
|
|
|
|
using Umbraco.Cms.Core.Scoping;
|
2021-02-12 13:36:50 +01:00
|
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
|
|
|
|
|
|
using Umbraco.Extensions;
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2021-02-12 13:36:50 +01:00
|
|
|
|
namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
|
2014-01-10 17:03:00 +11:00
|
|
|
|
{
|
2017-12-14 17:04:44 +01:00
|
|
|
|
public class NotificationsRepository : INotificationsRepository
|
2014-01-10 17:03:00 +11:00
|
|
|
|
{
|
2017-12-14 17:04:44 +01:00
|
|
|
|
private readonly IScopeAccessor _scopeAccessor;
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2017-12-14 17:04:44 +01:00
|
|
|
|
public NotificationsRepository(IScopeAccessor scopeAccessor)
|
2014-01-10 17:03:00 +11:00
|
|
|
|
{
|
2017-12-14 17:04:44 +01:00
|
|
|
|
_scopeAccessor = scopeAccessor;
|
2014-01-10 17:03:00 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-14 17:04:44 +01:00
|
|
|
|
private IScope AmbientScope => _scopeAccessor.AmbientScope;
|
|
|
|
|
|
|
2016-09-01 15:25:40 +02:00
|
|
|
|
public IEnumerable<Notification> GetUsersNotifications(IEnumerable<int> userIds, string action, IEnumerable<int> nodeIds, Guid objectType)
|
2016-09-01 12:13:58 +02:00
|
|
|
|
{
|
|
|
|
|
|
var nodeIdsA = nodeIds.ToArray();
|
2017-12-14 17:04:44 +01:00
|
|
|
|
var sql = AmbientScope.SqlContext.Sql()
|
2016-09-01 15:25:40 +02:00
|
|
|
|
.Select("DISTINCT umbracoNode.id nodeId, umbracoUser.id userId, umbracoNode.nodeObjectType, umbracoUser2NodeNotify.action")
|
2016-11-03 10:31:44 +01:00
|
|
|
|
.From<User2NodeNotifyDto>()
|
|
|
|
|
|
.InnerJoin<NodeDto>().On<User2NodeNotifyDto, NodeDto>(left => left.NodeId, right => right.NodeId)
|
|
|
|
|
|
.InnerJoin<UserDto>().On<User2NodeNotifyDto, UserDto>(left => left.UserId, right => right.Id)
|
2016-09-01 15:25:40 +02:00
|
|
|
|
.Where<NodeDto>(x => x.NodeObjectType == objectType)
|
2016-09-01 12:13:58 +02:00
|
|
|
|
.Where<UserDto>(x => x.Disabled == false) // only approved users
|
|
|
|
|
|
.Where<User2NodeNotifyDto>(x => x.Action == action); // on the specified action
|
|
|
|
|
|
if (nodeIdsA.Length > 0)
|
|
|
|
|
|
sql
|
|
|
|
|
|
.WhereIn<NodeDto>(x => x.NodeId, nodeIdsA); // for the specified nodes
|
|
|
|
|
|
sql
|
2016-11-03 10:31:44 +01:00
|
|
|
|
.OrderBy<UserDto>(x => x.Id)
|
|
|
|
|
|
.OrderBy<NodeDto>(dto => dto.NodeId);
|
2021-08-31 05:10:50 +12:00
|
|
|
|
return AmbientScope.Database.Fetch<UserNotificationDto>(sql).Select(x => new Notification(x.NodeId, x.UserId, x.Action, objectType));
|
2016-09-01 12:13:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-01-10 17:03:00 +11:00
|
|
|
|
public IEnumerable<Notification> GetUserNotifications(IUser user)
|
|
|
|
|
|
{
|
2017-12-14 17:04:44 +01:00
|
|
|
|
var sql = AmbientScope.SqlContext.Sql()
|
2021-08-31 05:10:50 +12:00
|
|
|
|
.Select("DISTINCT umbracoNode.id AS nodeId, umbracoUser2NodeNotify.userId, umbracoNode.nodeObjectType, umbracoUser2NodeNotify.action")
|
2016-04-12 15:11:07 +02:00
|
|
|
|
.From<User2NodeNotifyDto>()
|
|
|
|
|
|
.InnerJoin<NodeDto>()
|
|
|
|
|
|
.On<User2NodeNotifyDto, NodeDto>(dto => dto.NodeId, dto => dto.NodeId)
|
|
|
|
|
|
.Where<User2NodeNotifyDto>(dto => dto.UserId == (int)user.Id)
|
|
|
|
|
|
.OrderBy<NodeDto>(dto => dto.NodeId);
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2021-08-31 05:10:50 +12:00
|
|
|
|
var dtos = AmbientScope.Database.Fetch<UserNotificationDto>(sql);
|
2014-01-10 17:03:00 +11:00
|
|
|
|
//need to map the results
|
2021-08-31 05:10:50 +12:00
|
|
|
|
return dtos.Select(d => new Notification(d.NodeId, d.UserId, d.Action, d.NodeObjectType)).ToList();
|
2014-01-10 17:03:00 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-22 14:39:08 +01:00
|
|
|
|
public IEnumerable<Notification> SetNotifications(IUser user, IEntity entity, string[] actions)
|
|
|
|
|
|
{
|
2016-05-30 19:47:49 +02:00
|
|
|
|
DeleteNotifications(user, entity);
|
|
|
|
|
|
return actions.Select(action => CreateNotification(user, entity, action)).ToList();
|
2016-03-22 14:39:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-01-10 17:03:00 +11:00
|
|
|
|
public IEnumerable<Notification> GetEntityNotifications(IEntity entity)
|
|
|
|
|
|
{
|
2017-12-14 17:04:44 +01:00
|
|
|
|
var sql = AmbientScope.SqlContext.Sql()
|
2021-08-31 05:10:50 +12:00
|
|
|
|
.Select("DISTINCT umbracoNode.id as nodeId, umbracoUser2NodeNotify.userId, umbracoNode.nodeObjectType, umbracoUser2NodeNotify.action")
|
2016-04-12 15:11:07 +02:00
|
|
|
|
.From<User2NodeNotifyDto>()
|
|
|
|
|
|
.InnerJoin<NodeDto>()
|
|
|
|
|
|
.On<User2NodeNotifyDto, NodeDto>(dto => dto.NodeId, dto => dto.NodeId)
|
|
|
|
|
|
.Where<User2NodeNotifyDto>(dto => dto.NodeId == entity.Id)
|
|
|
|
|
|
.OrderBy<NodeDto>(dto => dto.NodeId);
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2021-08-31 05:10:50 +12:00
|
|
|
|
var dtos = AmbientScope.Database.Fetch<UserNotificationDto>(sql);
|
2014-01-10 17:03:00 +11:00
|
|
|
|
//need to map the results
|
2021-08-31 05:10:50 +12:00
|
|
|
|
return dtos.Select(d => new Notification(d.NodeId, d.UserId, d.Action, d.NodeObjectType)).ToList();
|
2014-01-10 17:03:00 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int DeleteNotifications(IEntity entity)
|
|
|
|
|
|
{
|
2017-12-14 17:04:44 +01:00
|
|
|
|
return AmbientScope.Database.Delete<User2NodeNotifyDto>("WHERE nodeId = @nodeId", new { nodeId = entity.Id });
|
2014-01-10 17:03:00 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int DeleteNotifications(IUser user)
|
|
|
|
|
|
{
|
2017-12-14 17:04:44 +01:00
|
|
|
|
return AmbientScope.Database.Delete<User2NodeNotifyDto>("WHERE userId = @userId", new { userId = user.Id });
|
2014-01-10 17:03:00 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int DeleteNotifications(IUser user, IEntity entity)
|
|
|
|
|
|
{
|
|
|
|
|
|
// delete all settings on the node for this user
|
2017-12-14 17:04:44 +01:00
|
|
|
|
return AmbientScope.Database.Delete<User2NodeNotifyDto>("WHERE userId = @userId AND nodeId = @nodeId", new { userId = user.Id, nodeId = entity.Id });
|
2014-01-10 17:03:00 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Notification CreateNotification(IUser user, IEntity entity, string action)
|
|
|
|
|
|
{
|
2017-12-14 17:04:44 +01:00
|
|
|
|
var sql = AmbientScope.SqlContext.Sql()
|
2014-01-10 17:03:00 +11:00
|
|
|
|
.Select("DISTINCT nodeObjectType")
|
2016-04-12 15:11:07 +02:00
|
|
|
|
.From<NodeDto>()
|
|
|
|
|
|
.Where<NodeDto>(nodeDto => nodeDto.NodeId == entity.Id);
|
2017-12-14 17:04:44 +01:00
|
|
|
|
var nodeType = AmbientScope.Database.ExecuteScalar<Guid>(sql);
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
|
var dto = new User2NodeNotifyDto
|
2014-01-10 17:03:00 +11:00
|
|
|
|
{
|
|
|
|
|
|
Action = action,
|
|
|
|
|
|
NodeId = entity.Id,
|
2017-09-22 15:23:46 +02:00
|
|
|
|
UserId = user.Id
|
2014-01-10 17:03:00 +11:00
|
|
|
|
};
|
2017-12-14 17:04:44 +01:00
|
|
|
|
AmbientScope.Database.Insert(dto);
|
2014-01-10 17:03:00 +11:00
|
|
|
|
return new Notification(dto.NodeId, dto.UserId, dto.Action, nodeType);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|