Add stable inbox cursor pagination
This commit is contained in:
@@ -11,6 +11,7 @@ public interface IStore
|
||||
Task<DateTime?> WorkerLastSeen();
|
||||
Task RecordWorkerHeartbeat();
|
||||
Task<List<T>> List<T>(string hotel) where T : TenantDocument;
|
||||
Task<ConversationPage> ConversationPage(string hotel,DateTime? before,string beforeId,int limit);
|
||||
Task<T?> Get<T>(string hotel, string id) where T : TenantDocument;
|
||||
Task Insert<T>(T document) where T : TenantDocument;
|
||||
Task<bool> Replace<T>(string hotel, string id, long version, T document) where T : TenantDocument;
|
||||
@@ -78,6 +79,12 @@ public sealed class MongoStore : IStore
|
||||
if (typeof(T) == typeof(PmsChange) || typeof(T) == typeof(PaymentRequest)) query = query.Sort(Builders<T>.Sort.Descending("UpdatedAt"));
|
||||
return query.Limit(500).ToListAsync();
|
||||
}
|
||||
public async Task<ConversationPage> ConversationPage(string hotel,DateTime? before,string beforeId,int limit)
|
||||
{
|
||||
var filter=Scope<Conversation>(hotel);if(before!=null)filter&=Builders<Conversation>.Filter.Lt(x=>x.ReceivedAt,before.Value)|(Builders<Conversation>.Filter.Eq(x=>x.ReceivedAt,before.Value)&Builders<Conversation>.Filter.Lt(x=>x.Id,beforeId));
|
||||
var rows=await Collection<Conversation>().Find(filter).SortByDescending(x=>x.ReceivedAt).ThenByDescending(x=>x.Id).Limit(limit+1).ToListAsync();
|
||||
var more=rows.Count>limit;if(more)rows.RemoveAt(rows.Count-1);return new(rows,more?ConversationPaging.Encode(rows[^1]):null);
|
||||
}
|
||||
public async Task<T?> Get<T>(string hotel, string id) where T : TenantDocument => await Collection<T>().Find(Scope<T>(hotel) & Builders<T>.Filter.Eq(x => x.Id, id)).FirstOrDefaultAsync();
|
||||
public Task Insert<T>(T document) where T : TenantDocument
|
||||
{
|
||||
@@ -179,6 +186,11 @@ public sealed class PreviewStore : IStore
|
||||
static string Json<T>(T value) => System.Text.Json.JsonSerializer.Serialize(value);
|
||||
public Task Initialize() => Task.CompletedTask;
|
||||
public Task<List<T>> List<T>(string hotel) where T : TenantDocument => Task.FromResult(rows.Where(x => x.Key.StartsWith(typeof(T).Name + ":")).Select(x => Clone<T>(x.Value)).Where(x => x.HotelId == hotel).ToList());
|
||||
public async Task<ConversationPage> ConversationPage(string hotel,DateTime? before,string beforeId,int limit)
|
||||
{
|
||||
var query=(await List<Conversation>(hotel)).OrderByDescending(x=>x.ReceivedAt).ThenByDescending(x=>x.Id).Where(x=>before==null||x.ReceivedAt<before||x.ReceivedAt==before&&string.CompareOrdinal(x.Id,beforeId)<0).Take(limit+1).ToList();
|
||||
var more=query.Count>limit;if(more)query.RemoveAt(query.Count-1);return new(query,more?ConversationPaging.Encode(query[^1]):null);
|
||||
}
|
||||
public async Task<T?> Get<T>(string hotel, string id) where T : TenantDocument => (await List<T>(hotel)).SingleOrDefault(x => x.Id == id);
|
||||
public Task Insert<T>(T document) where T : TenantDocument { if (!rows.TryAdd(Key<T>(document.Id), Json(document))) throw new InvalidOperationException("Duplicate document"); return Task.CompletedTask; }
|
||||
public Task<bool> Replace<T>(string hotel, string id, long version, T document) where T : TenantDocument
|
||||
|
||||
Reference in New Issue
Block a user