mirror of
https://github.com/bitwarden/server
synced 2025-12-28 06:03:29 +00:00
[PM-20041] Deleting Notifications when Task is completed (#5896)
* mark all notifications associated with a security task as deleted when the task is completed * fix spelling * formatting * refactor "Active" to "NonDeleted" * refactor "Active" to "NonDeleted" for stored procedure * only send notifications per user for each notification * move notification status updates into the DB layer to save on multiple queries and insertions from the C# * Only return UserIds from db layer * omit userId from `MarkTaskAsCompletedCommand` query. The userId from the notification will be used * update UserIds * consistency in comments regarding `taskId` and `UserId`
This commit is contained in:
@@ -32,4 +32,13 @@ public interface INotificationRepository : IRepository<Notification, Guid>
|
||||
/// </returns>
|
||||
Task<PagedResult<NotificationStatusDetails>> GetByUserIdAndStatusAsync(Guid userId, ClientType clientType,
|
||||
NotificationStatusFilter? statusFilter, PageOptions pageOptions);
|
||||
|
||||
/// <summary>
|
||||
/// Marks notifications as deleted by a taskId.
|
||||
/// </summary>
|
||||
/// <param name="taskId">The unique identifier of the task.</param>
|
||||
/// <returns>
|
||||
/// A collection of UserIds for the notifications that are now marked as deleted.
|
||||
/// </returns>
|
||||
Task<IEnumerable<Guid>> MarkNotificationsAsDeletedByTask(Guid taskId);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace Bit.Core.Vault.Commands.Interfaces;
|
||||
|
||||
public interface IMarkNotificationsForTaskAsDeletedCommand
|
||||
{
|
||||
/// <summary>
|
||||
/// Marks notifications associated with a given taskId as deleted.
|
||||
/// </summary>
|
||||
/// <param name="taskId">The unique identifier of the task to complete</param>
|
||||
/// <returns>A task representing the async operation</returns>
|
||||
Task MarkAsDeletedAsync(Guid taskId);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using Bit.Core.NotificationCenter.Repositories;
|
||||
using Bit.Core.Platform.Push;
|
||||
using Bit.Core.Vault.Commands.Interfaces;
|
||||
|
||||
namespace Bit.Core.Vault.Commands;
|
||||
|
||||
public class MarkNotificationsForTaskAsDeletedCommand : IMarkNotificationsForTaskAsDeletedCommand
|
||||
{
|
||||
private readonly INotificationRepository _notificationRepository;
|
||||
private readonly IPushNotificationService _pushNotificationService;
|
||||
|
||||
public MarkNotificationsForTaskAsDeletedCommand(
|
||||
INotificationRepository notificationRepository,
|
||||
IPushNotificationService pushNotificationService)
|
||||
{
|
||||
_notificationRepository = notificationRepository;
|
||||
_pushNotificationService = pushNotificationService;
|
||||
|
||||
}
|
||||
|
||||
public async Task MarkAsDeletedAsync(Guid taskId)
|
||||
{
|
||||
var userIds = await _notificationRepository.MarkNotificationsAsDeletedByTask(taskId);
|
||||
|
||||
// For each user associated with the notifications, send a push notification so local tasks can be updated.
|
||||
var uniqueUserIds = userIds.Distinct();
|
||||
foreach (var id in uniqueUserIds)
|
||||
{
|
||||
await _pushNotificationService.PushPendingSecurityTasksAsync(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,15 +14,19 @@ public class MarkTaskAsCompletedCommand : IMarkTaskAsCompleteCommand
|
||||
private readonly ISecurityTaskRepository _securityTaskRepository;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
private readonly ICurrentContext _currentContext;
|
||||
private readonly IMarkNotificationsForTaskAsDeletedCommand _markNotificationsForTaskAsDeletedAsync;
|
||||
|
||||
|
||||
public MarkTaskAsCompletedCommand(
|
||||
ISecurityTaskRepository securityTaskRepository,
|
||||
IAuthorizationService authorizationService,
|
||||
ICurrentContext currentContext)
|
||||
ICurrentContext currentContext,
|
||||
IMarkNotificationsForTaskAsDeletedCommand markNotificationsForTaskAsDeletedAsync)
|
||||
{
|
||||
_securityTaskRepository = securityTaskRepository;
|
||||
_authorizationService = authorizationService;
|
||||
_currentContext = currentContext;
|
||||
_markNotificationsForTaskAsDeletedAsync = markNotificationsForTaskAsDeletedAsync;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -46,5 +50,8 @@ public class MarkTaskAsCompletedCommand : IMarkTaskAsCompleteCommand
|
||||
task.RevisionDate = DateTime.UtcNow;
|
||||
|
||||
await _securityTaskRepository.ReplaceAsync(task);
|
||||
|
||||
// Mark all notifications related to this task as deleted
|
||||
await _markNotificationsForTaskAsDeletedAsync.MarkAsDeletedAsync(taskId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ public interface ICipherRepository : IRepository<Cipher, Guid>
|
||||
Guid userId);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the users and the cipher ids for security tawsks that are applicable to them.
|
||||
/// Returns the users and the cipher ids for security tasks that are applicable to them.
|
||||
///
|
||||
/// Security tasks are actionable when a user has manage access to the associated cipher.
|
||||
/// </summary>
|
||||
|
||||
@@ -24,5 +24,6 @@ public static class VaultServiceCollectionExtensions
|
||||
services.AddScoped<IGetSecurityTasksNotificationDetailsQuery, GetSecurityTasksNotificationDetailsQuery>();
|
||||
services.AddScoped<ICreateManyTaskNotificationsCommand, CreateManyTaskNotificationsCommand>();
|
||||
services.AddScoped<ICreateManyTasksCommand, CreateManyTasksCommand>();
|
||||
services.AddScoped<IMarkNotificationsForTaskAsDeletedCommand, MarkNotificationsForTaskAsDeletedCommand>();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user