Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add conversation pagination #257

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface IBotSharpRepository
void UpdateConversationStates(string conversationId, List<StateKeyValue> states);
void UpdateConversationStatus(string conversationId, string status);
Conversation GetConversation(string conversationId);
List<Conversation> GetConversations(ConversationFilter filter);
PagedItems<Conversation> GetConversations(ConversationFilter filter);
void UpdateConversationTitle(string conversationId, string title);
List<Conversation> GetLastConversations();
#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ namespace BotSharp.Abstraction.Utilities;

public class Pagination
{
public int Page { get; set; } = 0;
public int Page { get; set; } = 1;
/// <summary>
/// Use -1 for all records
/// </summary>
public int Size { get; set; } = 20;
public int Offset => (Page + 1) * Size;
public int Offset => (Page - 1) * Size;
}

public class PagedItems<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,8 @@ public async Task<Conversation> GetConversation(string id)
public async Task<PagedItems<Conversation>> GetConversations(ConversationFilter filter)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var user = db.GetUserById(_user.Id);
var conversations = db.GetConversations(filter);
var result = new PagedItems<Conversation>
{
Count = conversations.Count(),
Items = conversations.OrderByDescending(x => x.CreatedTime)
};
return result;
return conversations;
}

public async Task<List<Conversation>> GetLastConversations()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public Conversation GetConversation(string conversationId)
throw new NotImplementedException();
}

public List<Conversation> GetConversations(ConversationFilter filter)
public PagedItems<Conversation> GetConversations(ConversationFilter filter)
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,14 @@ public Conversation GetConversation(string conversationId)
return record;
}

public List<Conversation> GetConversations(ConversationFilter filter)
public PagedItems<Conversation> GetConversations(ConversationFilter filter)
{
var records = new List<Conversation>();
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir);
var totalDirs = Directory.GetDirectories(dir);
var dirs = totalDirs.Skip(filter.Pager.Offset).Take(filter.Pager.Size).ToList();

foreach (var d in Directory.GetDirectories(dir))
foreach (var d in dirs)
{
var path = Path.Combine(d, CONVERSATION_FILE);
if (!File.Exists(path)) continue;
Expand All @@ -225,7 +227,11 @@ public List<Conversation> GetConversations(ConversationFilter filter)
records.Add(record);
}

return records;
return new PagedItems<Conversation>
{
Items = records.OrderByDescending(x => x.CreatedTime),
Count = totalDirs.Count(),
};
}

public List<Conversation> GetLastConversations()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public async Task<ConversationViewModel> NewConversation([FromRoute] string agen
return ConversationViewModel.FromSession(conv);
}

[HttpGet("/conversations")]
public async Task<PagedItems<ConversationViewModel>> GetConversations([FromQuery] ConversationFilter filter)
[HttpPost("/conversations")]
public async Task<PagedItems<ConversationViewModel>> GetConversations([FromBody] ConversationFilter filter)
{
var service = _services.GetRequiredService<IConversationService>();
var conversations = await service.GetConversations(filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public Conversation GetConversation(string conversationId)
};
}

public List<Conversation> GetConversations(ConversationFilter filter)
public PagedItems<Conversation> GetConversations(ConversationFilter filter)
{
var conversations = new List<Conversation>();
var builder = Builders<ConversationDocument>.Filter;
Expand All @@ -215,7 +215,10 @@ public List<Conversation> GetConversations(ConversationFilter filter)
if (!string.IsNullOrEmpty(filter.Channel)) filters.Add(builder.Eq(x => x.Channel, filter.Channel));
if (!string.IsNullOrEmpty(filter.UserId)) filters.Add(builder.Eq(x => x.UserId, filter.UserId));

var conversationDocs = _dc.Conversations.Find(builder.And(filters)).ToList();
var filterDef = builder.And(filters);
var sortDefinition = Builders<ConversationDocument>.Sort.Descending(x => x.CreatedTime);
var conversationDocs = _dc.Conversations.Find(filterDef).Sort(sortDefinition).Skip(filter.Pager.Offset).Limit(filter.Pager.Size).ToList();
var count = _dc.Conversations.CountDocuments(filterDef);

foreach (var conv in conversationDocs)
{
Expand All @@ -233,7 +236,11 @@ public List<Conversation> GetConversations(ConversationFilter filter)
});
}

return conversations;
return new PagedItems<Conversation>
{
Items = conversations,
Count = (int)count
};
}

public List<Conversation> GetLastConversations()
Expand Down