using System; using System.Collections.Generic; using System.Linq; using System.Text.Json; namespace SharpChat.EventStorage { public class VirtualEventStorage : IEventStorage { private readonly Dictionary Events = new(); public long AddEvent(string type, ChatUser user, ChatChannel channel, object data = null, StoredEventFlags flags = StoredEventFlags.None) { if(type == null) throw new ArgumentNullException(nameof(type)); // VES is meant as an emergency fallback but this is something else JsonDocument hack = JsonDocument.Parse(data == null ? "{}" : JsonSerializer.Serialize(data)); long id = SharpId.Next(); Events.Add(id, new(id, type, user, DateTimeOffset.Now, null, channel?.Name ?? null, hack, flags)); return id; } public StoredEventInfo GetEvent(long seqId) { return Events.TryGetValue(seqId, out StoredEventInfo evt) ? evt : null; } public void RemoveEvent(StoredEventInfo evt) { if(evt == null) throw new ArgumentNullException(nameof(evt)); Events.Remove(evt.Id); } public IEnumerable GetChannelEventLog(string channelName, int amount = 20, int offset = 0) { IEnumerable subset = Events.Values.Where(ev => ev.ChannelName == channelName); int start = subset.Count() - offset - amount; if(start < 0) { amount += start; start = 0; } return subset.Skip(start).Take(amount).ToArray(); } } }