sharp-chat/SharpChatCommon/EventStorage/VirtualEventStorage.cs

32 lines
1.1 KiB
C#

using SharpChat.Events;
using System.Collections.Generic;
using System.Linq;
namespace SharpChat.EventStorage {
public class VirtualEventStorage : IEventStorage {
private readonly Dictionary<string, ChatEventInfo> Events = new();
public void HandleEvent(ChatEventInfo info) {
Events.Add(info.Id.ToString(), info);
if(info.Type.Equals("msg:delete") && info.Data is MessageDeleteEventData msgDelete)
Events.Remove(msgDelete.MessageId);
}
public ChatEventInfo? GetEvent(long eventId) {
return Events.TryGetValue(eventId.ToString(), out ChatEventInfo? evt) ? evt : null;
}
public IEnumerable<ChatEventInfo> GetChannelEventLog(string channelName, int amount = 20, int startAt = 0) {
IEnumerable<ChatEventInfo> subset = Events.Values.Where(ev => ev.ChannelName == channelName);
int start = subset.Count() - startAt - amount;
if(start < 0) {
amount += start;
start = 0;
}
return subset.Skip(start).Take(amount).ToArray();
}
}
}