|
1 | 1 | using BotSharp.Abstraction.Conversations.Models; |
2 | 2 | using BotSharp.Abstraction.Repositories.Filters; |
| 3 | +using MongoDB.Driver.Linq; |
3 | 4 | using System.Text.Json; |
4 | 5 |
|
5 | 6 | namespace BotSharp.Plugin.MongoStorage.Repository; |
@@ -560,45 +561,14 @@ public async Task<List<string>> GetIdleConversations(int batchSize, int messageL |
560 | 561 | while (true) |
561 | 562 | { |
562 | 563 | var skip = (page - 1) * batchSize; |
563 | | - var builder = Builders<ConversationDocument>.Filter; |
564 | | - var filters = new List<FilterDefinition<ConversationDocument>>(); |
565 | | - |
566 | | - // Build the OR condition: (!excludeAgentIds.Contains(AgentId) && DialogCount <= messageLimit) |
567 | | - // || (excludeAgentIds.Contains(AgentId) && DialogCount == 0) |
568 | | - var orFilters = new List<FilterDefinition<ConversationDocument>>(); |
569 | | - |
570 | | - // First condition: !excludeAgentIds.Contains(AgentId) && DialogCount <= messageLimit |
571 | | - if (excludeAgentIdsList.Any()) |
572 | | - { |
573 | | - orFilters.Add(builder.And( |
574 | | - builder.Nin(x => x.AgentId, excludeAgentIdsList), |
575 | | - builder.Lte(x => x.DialogCount, messageLimit) |
576 | | - )); |
577 | | - } |
578 | | - else |
579 | | - { |
580 | | - // If excludeAgentIds is empty, all agents match the first condition |
581 | | - orFilters.Add(builder.Lte(x => x.DialogCount, messageLimit)); |
582 | | - } |
583 | | - |
584 | | - // Second condition: excludeAgentIds.Contains(AgentId) && DialogCount == 0 |
585 | | - if (excludeAgentIdsList.Any()) |
586 | | - { |
587 | | - orFilters.Add(builder.And( |
588 | | - builder.In(x => x.AgentId, excludeAgentIdsList), |
589 | | - builder.Eq(x => x.DialogCount, 0) |
590 | | - )); |
591 | | - } |
592 | | - |
593 | | - filters.Add(builder.Or(orFilters)); |
594 | | - filters.Add(builder.Lte(x => x.UpdatedTime, utcNow.AddHours(-bufferHours))); |
595 | | - |
596 | | - var filter = builder.And(filters); |
597 | | - var candidates = await _dc.Conversations.Find(filter) |
598 | | - .Skip(skip) |
599 | | - .Limit(batchSize) |
600 | | - .Project(x => x.Id) |
601 | | - .ToListAsync(); |
| 564 | + var candidates = await _dc.Conversations.AsQueryable() |
| 565 | + .Where(x => ((!excludeAgentIds.Contains(x.AgentId) && x.DialogCount <= messageLimit) |
| 566 | + || (excludeAgentIds.Contains(x.AgentId) && x.DialogCount == 0)) |
| 567 | + && x.UpdatedTime <= utcNow.AddHours(-bufferHours)) |
| 568 | + .Skip(skip) |
| 569 | + .Take(batchSize) |
| 570 | + .Select(x => x.Id) |
| 571 | + .ToListAsync(); |
602 | 572 |
|
603 | 573 | if (candidates.IsNullOrEmpty()) |
604 | 574 | { |
|
0 commit comments