From 6ea903ebb9f4cc5494f7cd9a386d7576b1cd6904 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Sun, 7 Mar 2021 23:12:25 +0300 Subject: [PATCH] Implement linking event to previous in chat room when saving event --- .../Storage/EventStorageManager.cs | 19 +++++++++++++++++-- .../ChatSubsystem/Storage/Models/Event.cs | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/ZirconiumPlugins/ChatSubsystem/Storage/EventStorageManager.cs b/src/ZirconiumPlugins/ChatSubsystem/Storage/EventStorageManager.cs index e1642cf..a94f0c6 100644 --- a/src/ZirconiumPlugins/ChatSubsystem/Storage/EventStorageManager.cs +++ b/src/ZirconiumPlugins/ChatSubsystem/Storage/EventStorageManager.cs @@ -43,8 +43,23 @@ namespace ChatSubsystem.Storage await events.InsertOneAsync(e); return; } - // TODO link to previous events in the room - res.PrevID = res.Id; + e.PrevID = res.Id; + + connectToField = (FieldDefinition)"PrevEvents"; + var opts = new AggregateGraphLookupOptions() + { + RestrictSearchWithMatch = "{ ChatId: "+e.ChatId+" }" + }; + res = await events.Aggregate() + .GraphLookup(events, connectFromField, connectToField, startWith, @as, opts) + .Match("{ Children: { $size: 0 } }").FirstOrDefaultAsync(); + if (res == null) + { + await events.InsertOneAsync(e); + return; + } + + e.PrevEvent = res.EventID; await events.InsertOneAsync(e); } diff --git a/src/ZirconiumPlugins/ChatSubsystem/Storage/Models/Event.cs b/src/ZirconiumPlugins/ChatSubsystem/Storage/Models/Event.cs index ef25d7c..32fe214 100644 --- a/src/ZirconiumPlugins/ChatSubsystem/Storage/Models/Event.cs +++ b/src/ZirconiumPlugins/ChatSubsystem/Storage/Models/Event.cs @@ -13,7 +13,7 @@ namespace ChatSubsystem.Storage.Models public EntityID ChatId { get; set; } public string Type { get; set; } public long Timestamp { get; set; } - public EntityID[] PrevEvents { get; set; } + public EntityID PrevEvent { get; set; } public ObjectId PrevID { get; set; } public EntityID OriginServer { get; set; } public EventContent Content { get; set; }