From d9f15240eebb98adbb77dd6fdcee60393a731fff Mon Sep 17 00:00:00 2001 From: colin Date: Tue, 15 Jul 2025 17:25:01 +0800 Subject: [PATCH] fix: Delete users expired notifications - When clearing expired notifications, user notifications should also be cleared at the same time --- .../Abp/Notifications/INotificationRepository.cs | 4 +++- .../Notifications/IUserNotificationRepository.cs | 4 ++++ .../LINGYUN/Abp/Notifications/NotificationStore.cs | 13 +++++++++++-- .../EfCoreNotificationRepository.cs | 3 ++- .../EfCoreUserNotificationRepository.cs | 9 +++++++++ .../LINGYUN/Abp/Notifications/INotificationStore.cs | 4 +++- .../Abp/Notifications/NullNotificationStore.cs | 4 +++- 7 files changed, 35 insertions(+), 6 deletions(-) diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Domain/LINGYUN/Abp/Notifications/INotificationRepository.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Domain/LINGYUN/Abp/Notifications/INotificationRepository.cs index 7d318c6db..a451c41e4 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Domain/LINGYUN/Abp/Notifications/INotificationRepository.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Domain/LINGYUN/Abp/Notifications/INotificationRepository.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories; @@ -13,5 +14,6 @@ public interface INotificationRepository : IBasicRepository Task> GetExpritionAsync( int batchCount, + DateTime expritionTime, CancellationToken cancellationToken = default); } diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Domain/LINGYUN/Abp/Notifications/IUserNotificationRepository.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Domain/LINGYUN/Abp/Notifications/IUserNotificationRepository.cs index 18bfb256e..02bd12cdc 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Domain/LINGYUN/Abp/Notifications/IUserNotificationRepository.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Domain/LINGYUN/Abp/Notifications/IUserNotificationRepository.cs @@ -29,6 +29,10 @@ public interface IUserNotificationRepository : IBasicRepository> GetListByNotificationIdssync( + IEnumerable notificationIds, + CancellationToken cancellationToken = default); + Task GetCountAsync( Guid userId, string filter = "", diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Domain/LINGYUN/Abp/Notifications/NotificationStore.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Domain/LINGYUN/Abp/Notifications/NotificationStore.cs index a00dd76ce..a28945df8 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Domain/LINGYUN/Abp/Notifications/NotificationStore.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Domain/LINGYUN/Abp/Notifications/NotificationStore.cs @@ -102,15 +102,24 @@ public class NotificationStore : INotificationStore } } - public async virtual Task DeleteNotificationAsync( + public async virtual Task DeleteExpritionNotificationAsync( + Guid? tenantId, int batchCount, + DateTime expritionTime, CancellationToken cancellationToken = default) { using (var unitOfWork = _unitOfWorkManager.Begin()) + using (_currentTenant.Change(tenantId)) { - var notitications = await _notificationRepository.GetExpritionAsync(batchCount, cancellationToken); + var notitications = await _notificationRepository.GetExpritionAsync( + batchCount, expritionTime, cancellationToken); + var userNotitications = await _userNotificationRepository.GetListByNotificationIdssync( + notitications.Select(notification => notification.Id)); + // 清理过期通知 await _notificationRepository.DeleteManyAsync(notitications); + // 清理用户通知 + await _userNotificationRepository.DeleteManyAsync(userNotitications); await unitOfWork.CompleteAsync(cancellationToken); } diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.EntityFrameworkCore/LINGYUN/Abp/Notifications/EntityFrameworkCore/EfCoreNotificationRepository.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.EntityFrameworkCore/LINGYUN/Abp/Notifications/EntityFrameworkCore/EfCoreNotificationRepository.cs index af03f35ec..4a0c1adfc 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.EntityFrameworkCore/LINGYUN/Abp/Notifications/EntityFrameworkCore/EfCoreNotificationRepository.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.EntityFrameworkCore/LINGYUN/Abp/Notifications/EntityFrameworkCore/EfCoreNotificationRepository.cs @@ -21,10 +21,11 @@ public class EfCoreNotificationRepository : EfCoreRepository> GetExpritionAsync( int batchCount, + DateTime expritionTime, CancellationToken cancellationToken = default) { return await (await GetDbSetAsync()) - .Where(x => x.ExpirationTime < DateTime.Now) + .Where(x => x.ExpirationTime < expritionTime) .OrderBy(x => x.ExpirationTime) .Take(batchCount) .ToListAsync(GetCancellationToken(cancellationToken)); diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.EntityFrameworkCore/LINGYUN/Abp/Notifications/EntityFrameworkCore/EfCoreUserNotificationRepository.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.EntityFrameworkCore/LINGYUN/Abp/Notifications/EntityFrameworkCore/EfCoreUserNotificationRepository.cs index 345053ead..077b6956c 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.EntityFrameworkCore/LINGYUN/Abp/Notifications/EntityFrameworkCore/EfCoreUserNotificationRepository.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.EntityFrameworkCore/LINGYUN/Abp/Notifications/EntityFrameworkCore/EfCoreUserNotificationRepository.cs @@ -74,6 +74,15 @@ public class EfCoreUserNotificationRepository : EfCoreRepository> GetListByNotificationIdssync( + IEnumerable notificationIds, + CancellationToken cancellationToken = default) + { + return await (await GetDbSetAsync()) + .Where(x => notificationIds.Contains(x.NotificationId)) + .ToListAsync(GetCancellationToken(cancellationToken)); + } + public async virtual Task> GetNotificationsAsync( Guid userId, NotificationReadState? readState = null, diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationStore.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationStore.cs index 7871a7423..9c68f51a9 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationStore.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationStore.cs @@ -66,8 +66,10 @@ public interface INotificationStore NotificationInfo notification, CancellationToken cancellationToken = default); - Task DeleteNotificationAsync( + Task DeleteExpritionNotificationAsync( + Guid? tenantId, int batchCount, + DateTime expritionTime, CancellationToken cancellationToken = default); Task InsertUserNotificationAsync( diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NullNotificationStore.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NullNotificationStore.cs index 5033a5e5f..e55f068e0 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NullNotificationStore.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NullNotificationStore.cs @@ -46,8 +46,10 @@ public class NullNotificationStore : INotificationStore, ISingletonDependency return Task.CompletedTask; } - public Task DeleteNotificationAsync( + public Task DeleteExpritionNotificationAsync( + Guid? tenantId, int batchCount, + DateTime expritionTime, CancellationToken cancellationToken = default) { return Task.CompletedTask;