Make linking multiple events when saving the event

This commit is contained in:
ChronosX88 2021-03-08 14:21:35 +03:00
parent 6ea903ebb9
commit 8b49ea7cd3
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
3 changed files with 27 additions and 10 deletions

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using ChatSubsystem.Storage.Interfaces; using ChatSubsystem.Storage.Interfaces;
using ChatSubsystem.Storage.Models; using ChatSubsystem.Storage.Models;
@ -23,7 +24,14 @@ namespace ChatSubsystem.Storage
events = database.GetCollection<Event>(EventsCollectionName); events = database.GetCollection<Event>(EventsCollectionName);
} }
public IList<Event> GetEventsForUser(EntityID user, EntityID token, int limit) public async Task<IList<Event>> GetEventsForUser(EntityID user, EntityID since, int limit)
{
var chats = await _chatStorageManager.GetChatsForUser(user);
return new List<Event>();
}
private async Task<IList<Event>> GetEventsForChat(EntityID since, int limit)
{ {
return new List<Event>(); return new List<Event>();
} }
@ -35,16 +43,21 @@ namespace ChatSubsystem.Storage
var startWith = (AggregateExpressionDefinition<Event, string>)"$_id"; var startWith = (AggregateExpressionDefinition<Event, string>)"$_id";
var @as = (FieldDefinition<EventWithChildren, IEnumerable<Event>>)"Children"; var @as = (FieldDefinition<EventWithChildren, IEnumerable<Event>>)"Children";
// link to previous global events
var res = await events.Aggregate() var res = await events.Aggregate()
.GraphLookup(events, connectFromField, connectToField, startWith, @as) .GraphLookup(events, connectFromField, connectToField, startWith, @as)
.Match("{ Children: { $size: 0 } }").FirstOrDefaultAsync(); .Match("{ Children: { $size: 0 } }").ToListAsync();
if (res == null) if (res.Count == 0)
{ {
await events.InsertOneAsync(e); await events.InsertOneAsync(e);
return; return;
} }
e.PrevID = res.Id; res.ForEach(x =>
{
e.PrevID.Append(x.Id);
});
// link to previous events in chat
connectToField = (FieldDefinition<Event, EntityID>)"PrevEvents"; connectToField = (FieldDefinition<Event, EntityID>)"PrevEvents";
var opts = new AggregateGraphLookupOptions<Event, Event, EventWithChildren>() var opts = new AggregateGraphLookupOptions<Event, Event, EventWithChildren>()
{ {
@ -52,14 +65,17 @@ namespace ChatSubsystem.Storage
}; };
res = await events.Aggregate() res = await events.Aggregate()
.GraphLookup(events, connectFromField, connectToField, startWith, @as, opts) .GraphLookup(events, connectFromField, connectToField, startWith, @as, opts)
.Match("{ Children: { $size: 0 } }").FirstOrDefaultAsync(); .Match("{ Children: { $size: 0 } }").ToListAsync();
if (res == null) if (res.Count == 0)
{ {
await events.InsertOneAsync(e); await events.InsertOneAsync(e);
return; return;
} }
res.ForEach(x =>
{
e.PrevEvents.Append(x.EventID);
});
e.PrevEvent = res.EventID;
await events.InsertOneAsync(e); await events.InsertOneAsync(e);
} }

View File

@ -7,7 +7,7 @@ namespace ChatSubsystem.Storage.Interfaces
{ {
public interface IEventStorageManager public interface IEventStorageManager
{ {
IList<Event> GetEventsForUser(EntityID user, EntityID token, int limit); Task<IList<Event>> GetEventsForUser(EntityID user, EntityID since, int limit);
Task<Event> GetEventById(EntityID id); Task<Event> GetEventById(EntityID id);
Task SaveEvent(Event e); Task SaveEvent(Event e);
} }

View File

@ -1,3 +1,4 @@
using System.Collections.Generic;
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
using Zirconium.Core.Models; using Zirconium.Core.Models;
@ -13,8 +14,8 @@ namespace ChatSubsystem.Storage.Models
public EntityID ChatId { get; set; } public EntityID ChatId { get; set; }
public string Type { get; set; } public string Type { get; set; }
public long Timestamp { get; set; } public long Timestamp { get; set; }
public EntityID PrevEvent { get; set; } public IList<EntityID> PrevEvents { get; set; }
public ObjectId PrevID { get; set; } public IList<ObjectId> PrevID { get; set; }
public EntityID OriginServer { get; set; } public EntityID OriginServer { get; set; }
public EventContent Content { get; set; } public EventContent Content { get; set; }
} }