sharp-chat/SharpChat/EventStorage/VirtualEventStorage.cs

56 lines
1.9 KiB
C#
Raw Normal View History

using System.Text.Json;
namespace SharpChat.EventStorage {
public class VirtualEventStorage : EventStorage {
2025-04-25 15:49:46 +00:00
private readonly Dictionary<long, StoredEventInfo> Events = [];
public void AddEvent(
2025-04-25 18:18:13 +00:00
long id,
string type,
string channelName,
2025-04-25 18:18:13 +00:00
long senderId,
string senderName,
2025-04-26 13:15:27 +00:00
ColourInheritable senderColour,
2025-04-25 18:18:13 +00:00
int senderRank,
string senderNick,
UserPermissions senderPerms,
2025-04-25 18:18:13 +00:00
object? data = null,
StoredEventFlags flags = StoredEventFlags.None
) {
2025-04-25 15:49:46 +00:00
ArgumentNullException.ThrowIfNull(type);
// VES is meant as an emergency fallback but this is something else
JsonDocument hack = JsonDocument.Parse(data == null ? "{}" : JsonSerializer.Serialize(data));
Events.Add(id, new(id, type, senderId < 1 ? null : new User(
senderId,
senderName,
senderColour,
senderRank,
senderPerms,
senderNick
), DateTimeOffset.Now, null, channelName, hack, flags));
}
2025-04-25 18:18:13 +00:00
public StoredEventInfo? GetEvent(long seqId) {
return Events.TryGetValue(seqId, out StoredEventInfo? evt) ? evt : null;
}
public void RemoveEvent(StoredEventInfo evt) {
2025-04-25 15:49:46 +00:00
ArgumentNullException.ThrowIfNull(evt);
Events.Remove(evt.Id);
}
public IEnumerable<StoredEventInfo> GetChannelEventLog(string channelName, int amount = 20, int offset = 0) {
IEnumerable<StoredEventInfo> subset = Events.Values.Where(ev => ev.ChannelName == channelName);
int start = subset.Count() - offset - amount;
if(start < 0) {
amount += start;
start = 0;
}
2025-04-25 15:49:46 +00:00
return [.. subset.Skip(start).Take(amount)];
}
}
}