mirror of
https://github.com/cadmium-im/zirconium-sharp.git
synced 2024-11-08 11:41:04 +00:00
Implement saving events in EventStorageManager
This commit is contained in:
parent
eb11191c8f
commit
2485612c87
@ -0,0 +1,55 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using ChatSubsystem.Storage.Models;
|
||||
using MongoDB.Driver;
|
||||
using Zirconium.Core.Models;
|
||||
|
||||
namespace ChatSubsystem.Storage
|
||||
{
|
||||
public class ChatStorageManager
|
||||
{
|
||||
// get private chat by user 1 and user 2
|
||||
// get chat by id
|
||||
// update chat
|
||||
|
||||
private const string ChatsCollectionName = "chats";
|
||||
|
||||
private IMongoCollection<Chat> chatsCollection;
|
||||
|
||||
public ChatStorageManager(IMongoDatabase database)
|
||||
{
|
||||
chatsCollection = database.GetCollection<Chat>(ChatsCollectionName);
|
||||
}
|
||||
|
||||
public async Task<Chat> GetPrivateChat(EntityID u1, EntityID u2)
|
||||
{
|
||||
var filter =
|
||||
Builders<Chat>.Filter.Where(
|
||||
x => x.Members.Contains(u1.ToString()) && x.Members.Contains(u2.ToString()) && x.Members.Length == 2 && x.IsPrivateChat);
|
||||
var res = await chatsCollection.FindAsync(filter);
|
||||
var chat = res.FirstOrDefault();
|
||||
return chat;
|
||||
}
|
||||
|
||||
public async Task<Chat> GetById(EntityID chatId)
|
||||
{
|
||||
var filter =
|
||||
Builders<Chat>.Filter.Where(
|
||||
x => x.ChatID == chatId.ToString());
|
||||
var res = await chatsCollection.FindAsync(filter);
|
||||
var chat = res.FirstOrDefault();
|
||||
return chat;
|
||||
}
|
||||
|
||||
public async Task<IList<Chat>> GetChatsForUser(EntityID user)
|
||||
{
|
||||
var filter =
|
||||
Builders<Chat>.Filter.Where(
|
||||
x => x.Members.Contains(user.ToString()));
|
||||
var res = await chatsCollection.FindAsync(filter);
|
||||
var chat = res.ToList();
|
||||
return chat;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using ChatSubsystem.Storage.Interfaces;
|
||||
using ChatSubsystem.Storage.Models;
|
||||
using MongoDB.Driver;
|
||||
using Zirconium.Core.Models;
|
||||
|
||||
namespace ChatSubsystem.Storage
|
||||
{
|
||||
public class EventStorageManager : IEventStorageManager
|
||||
{
|
||||
private const string EventsCollectionName = "events";
|
||||
|
||||
// get event by id
|
||||
// get events for user after some point in timeline (via sorting by event id)
|
||||
|
||||
private ChatStorageManager _chatStorageManager;
|
||||
private IMongoCollection<Event> events;
|
||||
|
||||
public EventStorageManager(ChatStorageManager chatStorageManager, IMongoDatabase database)
|
||||
{
|
||||
_chatStorageManager = chatStorageManager;
|
||||
events = database.GetCollection<Event>(EventsCollectionName);
|
||||
}
|
||||
|
||||
public IList<Event> GetEventsForUser(EntityID user, EntityID token, int limit)
|
||||
{
|
||||
return new List<Event>();
|
||||
}
|
||||
|
||||
public async Task SaveEvent(Event e)
|
||||
{
|
||||
var connectFromField = (FieldDefinition<Event, EntityID>)"_id";
|
||||
var connectToField = (FieldDefinition<Event, EntityID>)"PrevID";
|
||||
var startWith = (AggregateExpressionDefinition<Event, string>)"$_id";
|
||||
var @as = (FieldDefinition<EventWithChildren, IEnumerable<Event>>)"Children";
|
||||
|
||||
var res = await events.Aggregate()
|
||||
.GraphLookup(events, connectFromField, connectToField, startWith, @as)
|
||||
.Match("{ Children: { $size: 0 } }").FirstOrDefaultAsync();
|
||||
if (res == null)
|
||||
{
|
||||
await events.InsertOneAsync(e);
|
||||
return;
|
||||
}
|
||||
// TODO link to previous events in the room
|
||||
res.PrevID = res.Id;
|
||||
await events.InsertOneAsync(e);
|
||||
}
|
||||
|
||||
public async Task<Event> GetEventById(EntityID id)
|
||||
{
|
||||
var filter =
|
||||
Builders<Event>.Filter.Where(
|
||||
x => x.Id.ToString() == id.LocalPart && x.OriginServer.ServerPart == id.ServerPart);
|
||||
var res = await events.FindAsync(filter);
|
||||
var ev = res.FirstOrDefault();
|
||||
return ev;
|
||||
}
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@ namespace ChatSubsystem.Storage.Interfaces
|
||||
public interface IEventStorageManager
|
||||
{
|
||||
IList<Event> GetEventsForUser(EntityID user, EntityID token, int limit);
|
||||
|
||||
Task<Event> GetEventById(EntityID id);
|
||||
Task SaveEvent(Event e);
|
||||
}
|
||||
}
|
@ -6,14 +6,21 @@ namespace ChatSubsystem.Storage.Models
|
||||
{
|
||||
public class Event
|
||||
{
|
||||
[BsonId]
|
||||
public ObjectId Id { get; set; }
|
||||
[BsonId] public ObjectId Id { get; set; }
|
||||
|
||||
public EntityID EventID { get; set; }
|
||||
public EntityID From { get; set; }
|
||||
public EntityID ChatId { get; set; }
|
||||
public string Type { get; set; }
|
||||
public long Timestamp { get; set; }
|
||||
public EntityID[] PrevEvents { get; set; }
|
||||
public ObjectId PrevID { get; set; }
|
||||
public EntityID OriginServer { get; set; }
|
||||
public EventContent Content { get; set; }
|
||||
}
|
||||
|
||||
public class EventWithChildren : Event
|
||||
{
|
||||
public Event[] Children { get; set; }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user