Skip to content

Commit d508c44

Browse files
authored
Merge pull request #1275 from colinin/remove-user-expired-notifications
fix: Delete users expired notifications
2 parents b7d70da + d9f1524 commit d508c44

7 files changed

Lines changed: 35 additions & 6 deletions

File tree

aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Domain/LINGYUN/Abp/Notifications/INotificationRepository.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Threading;
34
using System.Threading.Tasks;
45
using Volo.Abp.Domain.Repositories;
@@ -13,5 +14,6 @@ Task<Notification> GetByIdAsync(
1314

1415
Task<List<Notification>> GetExpritionAsync(
1516
int batchCount,
17+
DateTime expritionTime,
1618
CancellationToken cancellationToken = default);
1719
}

aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Domain/LINGYUN/Abp/Notifications/IUserNotificationRepository.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ Task<List<UserNotificationInfo>> GetNotificationsAsync(
2929
int maxResultCount = 10,
3030
CancellationToken cancellationToken = default);
3131

32+
Task<List<UserNotification>> GetListByNotificationIdssync(
33+
IEnumerable<long> notificationIds,
34+
CancellationToken cancellationToken = default);
35+
3236
Task<int> GetCountAsync(
3337
Guid userId,
3438
string filter = "",

aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Domain/LINGYUN/Abp/Notifications/NotificationStore.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,24 @@ public async virtual Task DeleteNotificationAsync(
102102
}
103103
}
104104

105-
public async virtual Task DeleteNotificationAsync(
105+
public async virtual Task DeleteExpritionNotificationAsync(
106+
Guid? tenantId,
106107
int batchCount,
108+
DateTime expritionTime,
107109
CancellationToken cancellationToken = default)
108110
{
109111
using (var unitOfWork = _unitOfWorkManager.Begin())
112+
using (_currentTenant.Change(tenantId))
110113
{
111-
var notitications = await _notificationRepository.GetExpritionAsync(batchCount, cancellationToken);
114+
var notitications = await _notificationRepository.GetExpritionAsync(
115+
batchCount, expritionTime, cancellationToken);
116+
var userNotitications = await _userNotificationRepository.GetListByNotificationIdssync(
117+
notitications.Select(notification => notification.Id));
112118

119+
// 清理过期通知
113120
await _notificationRepository.DeleteManyAsync(notitications);
121+
// 清理用户通知
122+
await _userNotificationRepository.DeleteManyAsync(userNotitications);
114123

115124
await unitOfWork.CompleteAsync(cancellationToken);
116125
}

aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.EntityFrameworkCore/LINGYUN/Abp/Notifications/EntityFrameworkCore/EfCoreNotificationRepository.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ public EfCoreNotificationRepository(
2121

2222
public async Task<List<Notification>> GetExpritionAsync(
2323
int batchCount,
24+
DateTime expritionTime,
2425
CancellationToken cancellationToken = default)
2526
{
2627
return await (await GetDbSetAsync())
27-
.Where(x => x.ExpirationTime < DateTime.Now)
28+
.Where(x => x.ExpirationTime < expritionTime)
2829
.OrderBy(x => x.ExpirationTime)
2930
.Take(batchCount)
3031
.ToListAsync(GetCancellationToken(cancellationToken));

aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.EntityFrameworkCore/LINGYUN/Abp/Notifications/EntityFrameworkCore/EfCoreUserNotificationRepository.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ public async virtual Task<List<UserNotification>> GetListAsync(
7474
.ToListAsync(GetCancellationToken(cancellationToken));
7575
}
7676

77+
public async virtual Task<List<UserNotification>> GetListByNotificationIdssync(
78+
IEnumerable<long> notificationIds,
79+
CancellationToken cancellationToken = default)
80+
{
81+
return await (await GetDbSetAsync())
82+
.Where(x => notificationIds.Contains(x.NotificationId))
83+
.ToListAsync(GetCancellationToken(cancellationToken));
84+
}
85+
7786
public async virtual Task<List<UserNotificationInfo>> GetNotificationsAsync(
7887
Guid userId,
7988
NotificationReadState? readState = null,

aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationStore.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ Task DeleteNotificationAsync(
6666
NotificationInfo notification,
6767
CancellationToken cancellationToken = default);
6868

69-
Task DeleteNotificationAsync(
69+
Task DeleteExpritionNotificationAsync(
70+
Guid? tenantId,
7071
int batchCount,
72+
DateTime expritionTime,
7173
CancellationToken cancellationToken = default);
7274

7375
Task InsertUserNotificationAsync(

aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NullNotificationStore.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ public Task DeleteNotificationAsync(
4646
return Task.CompletedTask;
4747
}
4848

49-
public Task DeleteNotificationAsync(
49+
public Task DeleteExpritionNotificationAsync(
50+
Guid? tenantId,
5051
int batchCount,
52+
DateTime expritionTime,
5153
CancellationToken cancellationToken = default)
5254
{
5355
return Task.CompletedTask;

0 commit comments

Comments
 (0)