diff --git a/src/ZirconiumPlugins/ChatSubsystem/Storage/EventStorageManager.cs b/src/ZirconiumPlugins/ChatSubsystem/Storage/EventStorageManager.cs index a94f0c6..f9edeef 100644 --- a/src/ZirconiumPlugins/ChatSubsystem/Storage/EventStorageManager.cs +++ b/src/ZirconiumPlugins/ChatSubsystem/Storage/EventStorageManager.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using ChatSubsystem.Storage.Interfaces; using ChatSubsystem.Storage.Models; @@ -23,7 +24,14 @@ namespace ChatSubsystem.Storage events = database.GetCollection(EventsCollectionName); } - public IList GetEventsForUser(EntityID user, EntityID token, int limit) + public async Task> GetEventsForUser(EntityID user, EntityID since, int limit) + { + var chats = await _chatStorageManager.GetChatsForUser(user); + + return new List(); + } + + private async Task> GetEventsForChat(EntityID since, int limit) { return new List(); } @@ -35,16 +43,21 @@ namespace ChatSubsystem.Storage var startWith = (AggregateExpressionDefinition)"$_id"; var @as = (FieldDefinition>)"Children"; + // link to previous global events var res = await events.Aggregate() .GraphLookup(events, connectFromField, connectToField, startWith, @as) - .Match("{ Children: { $size: 0 } }").FirstOrDefaultAsync(); - if (res == null) + .Match("{ Children: { $size: 0 } }").ToListAsync(); + if (res.Count == 0) { await events.InsertOneAsync(e); return; } - e.PrevID = res.Id; + res.ForEach(x => + { + e.PrevID.Append(x.Id); + }); + // link to previous events in chat connectToField = (FieldDefinition)"PrevEvents"; var opts = new AggregateGraphLookupOptions() { @@ -52,14 +65,17 @@ namespace ChatSubsystem.Storage }; res = await events.Aggregate() .GraphLookup(events, connectFromField, connectToField, startWith, @as, opts) - .Match("{ Children: { $size: 0 } }").FirstOrDefaultAsync(); - if (res == null) + .Match("{ Children: { $size: 0 } }").ToListAsync(); + if (res.Count == 0) { await events.InsertOneAsync(e); return; } + res.ForEach(x => + { + e.PrevEvents.Append(x.EventID); + }); - e.PrevEvent = res.EventID; await events.InsertOneAsync(e); } diff --git a/src/ZirconiumPlugins/ChatSubsystem/Storage/Interfaces/IEventStorageManager.cs b/src/ZirconiumPlugins/ChatSubsystem/Storage/Interfaces/IEventStorageManager.cs index f54ff03..93ecf38 100644 --- a/src/ZirconiumPlugins/ChatSubsystem/Storage/Interfaces/IEventStorageManager.cs +++ b/src/ZirconiumPlugins/ChatSubsystem/Storage/Interfaces/IEventStorageManager.cs @@ -7,7 +7,7 @@ namespace ChatSubsystem.Storage.Interfaces { public interface IEventStorageManager { - IList GetEventsForUser(EntityID user, EntityID token, int limit); + Task> GetEventsForUser(EntityID user, EntityID since, int limit); Task GetEventById(EntityID id); Task SaveEvent(Event e); } diff --git a/src/ZirconiumPlugins/ChatSubsystem/Storage/Models/Event.cs b/src/ZirconiumPlugins/ChatSubsystem/Storage/Models/Event.cs index 32fe214..6eb1bc9 100644 --- a/src/ZirconiumPlugins/ChatSubsystem/Storage/Models/Event.cs +++ b/src/ZirconiumPlugins/ChatSubsystem/Storage/Models/Event.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; using Zirconium.Core.Models; @@ -13,8 +14,8 @@ namespace ChatSubsystem.Storage.Models public EntityID ChatId { get; set; } public string Type { get; set; } public long Timestamp { get; set; } - public EntityID PrevEvent { get; set; } - public ObjectId PrevID { get; set; } + public IList PrevEvents { get; set; } + public IList PrevID { get; set; } public EntityID OriginServer { get; set; } public EventContent Content { get; set; } }