diff --git a/products/ASC.Files/Core/Core/Dao/TeamlabDao/SecurityDao.cs b/products/ASC.Files/Core/Core/Dao/TeamlabDao/SecurityDao.cs index 6ab15634fde..d37c2809122 100644 --- a/products/ASC.Files/Core/Core/Dao/TeamlabDao/SecurityDao.cs +++ b/products/ASC.Files/Core/Core/Dao/TeamlabDao/SecurityDao.cs @@ -274,9 +274,11 @@ public async IAsyncEnumerable GetPureSharesAsync(FileEntry e q = q.Take(count); } - await foreach (var r in q.ToAsyncEnumerable()) + var records = q.ToAsyncEnumerable().SelectAwait(async r => await ToFileShareRecordAsync(r)); + + await foreach (var r in DeleteExpiredAsync(records, filesDbContext)) { - yield return await ToFileShareRecordAsync(r); + yield return r; } } @@ -516,6 +518,33 @@ private async Task> GetPureSharesQuery(FileEntry return q; } + + protected async IAsyncEnumerable DeleteExpiredAsync(IAsyncEnumerable records, FilesDbContext filesDbContext) + { + var expired = new List(); + + await foreach (var r in records) + { + if (r.SubjectType == SubjectType.InvitationLink && r.Options is { IsExpired: true }) + { + expired.Add(r.Subject); + continue; + } + + yield return r; + } + + if (expired.Count <= 0) + { + yield break; + } + + var tenantId = await _tenantManager.GetCurrentTenantIdAsync(); + + await filesDbContext.Security + .Where(s => s.TenantId == tenantId && s.SubjectType == SubjectType.InvitationLink && expired.Contains(s.Subject)) + .ExecuteDeleteAsync(); + } } [Scope] @@ -597,41 +626,12 @@ public async Task> GetSharesAsync(FileEntry en q = q.Where(r => subjects.Contains(r.Subject)); } - var records = await q.ToAsyncEnumerable() + var records = q.ToAsyncEnumerable() .Select(ToFileShareRecord) .OrderBy(r => r.Level) - .ThenByDescending(r => r.Share, new FileShareRecord.ShareComparer()) - .ToListAsync(); - - await DeleteExpiredAsync(records, filesDbContext); - - return records; - } + .ThenByDescending(r => r.Share, new FileShareRecord.ShareComparer()); - private async Task DeleteExpiredAsync(List records, FilesDbContext filesDbContext) - { - var expired = new List(); - - for (var i = 0; i < records.Count; i++) - { - var r = records[i]; - if (r.SubjectType != SubjectType.InvitationLink || r.Options is not { IsExpired: true }) - { - continue; - } - - expired.Add(r.Subject); - records.RemoveAt(i); - } - - if (expired.Count > 0) - { - var tenantId = await _tenantManager.GetCurrentTenantIdAsync(); - - await filesDbContext.Security - .Where(s => s.TenantId == tenantId && s.SubjectType == SubjectType.InvitationLink && expired.Contains(s.Subject)) - .ExecuteDeleteAsync(); - } + return await DeleteExpiredAsync(records, filesDbContext).ToListAsync(); } }