2023-02-23 21:46:49 +00:00
|
|
|
|
using System;
|
2023-02-10 06:07:59 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2023-02-23 21:46:49 +00:00
|
|
|
|
using System.Text.Json;
|
2023-02-10 06:07:59 +00:00
|
|
|
|
|
2023-07-23 21:31:13 +00:00
|
|
|
|
namespace SharpChat.EventStorage {
|
2023-02-10 06:07:59 +00:00
|
|
|
|
public class VirtualEventStorage : IEventStorage {
|
2023-02-23 21:46:49 +00:00
|
|
|
|
private readonly Dictionary<long, StoredEventInfo> Events = new();
|
2023-02-10 06:07:59 +00:00
|
|
|
|
|
2023-07-23 21:31:13 +00:00
|
|
|
|
public void AddEvent(
|
|
|
|
|
long id, string type,
|
2024-05-10 19:18:55 +00:00
|
|
|
|
object? data = null,
|
2023-07-23 21:31:13 +00:00
|
|
|
|
StoredEventFlags flags = StoredEventFlags.None
|
|
|
|
|
) {
|
|
|
|
|
AddEvent(id, type, null, 0, null, ChatColour.None, 0, null, 0, data, flags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddEvent(
|
|
|
|
|
long id, string type,
|
2024-05-10 19:18:55 +00:00
|
|
|
|
string? channelName,
|
|
|
|
|
object? data = null,
|
2023-07-23 21:31:13 +00:00
|
|
|
|
StoredEventFlags flags = StoredEventFlags.None
|
|
|
|
|
) {
|
|
|
|
|
AddEvent(id, type, channelName, 0, null, ChatColour.None, 0, null, 0, data, flags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddEvent(
|
|
|
|
|
long id, string type,
|
2024-05-10 19:18:55 +00:00
|
|
|
|
long senderId, string? senderName, ChatColour senderColour, int senderRank, string? senderNick, ChatUserPermissions senderPerms,
|
|
|
|
|
object? data = null,
|
2023-07-23 21:31:13 +00:00
|
|
|
|
StoredEventFlags flags = StoredEventFlags.None
|
|
|
|
|
) {
|
|
|
|
|
AddEvent(id, type, null, senderId, senderName, senderColour, senderRank, senderNick, senderPerms, data, flags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddEvent(
|
|
|
|
|
long id, string type,
|
2024-05-10 19:18:55 +00:00
|
|
|
|
string? channelName,
|
|
|
|
|
long senderId, string? senderName, ChatColour senderColour, int senderRank, string? senderNick, ChatUserPermissions senderPerms,
|
|
|
|
|
object? data = null,
|
2023-07-23 21:31:13 +00:00
|
|
|
|
StoredEventFlags flags = StoredEventFlags.None
|
|
|
|
|
) {
|
2023-02-23 21:46:49 +00:00
|
|
|
|
// VES is meant as an emergency fallback but this is something else
|
|
|
|
|
JsonDocument hack = JsonDocument.Parse(data == null ? "{}" : JsonSerializer.Serialize(data));
|
2023-07-23 21:31:13 +00:00
|
|
|
|
Events.Add(id, new(id, type, senderId < 1 ? null : new ChatUser(
|
|
|
|
|
senderId,
|
2024-05-19 01:53:14 +00:00
|
|
|
|
senderName ?? string.Empty,
|
2023-07-23 21:31:13 +00:00
|
|
|
|
senderColour,
|
|
|
|
|
senderRank,
|
|
|
|
|
senderPerms,
|
|
|
|
|
senderNick
|
|
|
|
|
), DateTimeOffset.Now, null, channelName, hack, flags));
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 19:18:55 +00:00
|
|
|
|
public long AddEvent(string type, ChatUser user, ChatChannel channel, object? data = null, StoredEventFlags flags = StoredEventFlags.None) {
|
2023-02-23 21:46:49 +00:00
|
|
|
|
long id = SharpId.Next();
|
2023-07-23 21:31:13 +00:00
|
|
|
|
|
|
|
|
|
AddEvent(
|
|
|
|
|
id, type,
|
|
|
|
|
channel?.Name,
|
|
|
|
|
user?.UserId ?? 0,
|
|
|
|
|
user?.UserName,
|
|
|
|
|
user?.Colour ?? ChatColour.None,
|
|
|
|
|
user?.Rank ?? 0,
|
|
|
|
|
user?.NickName,
|
|
|
|
|
user?.Permissions ?? 0,
|
|
|
|
|
data,
|
|
|
|
|
flags
|
|
|
|
|
);
|
2023-02-23 21:46:49 +00:00
|
|
|
|
|
|
|
|
|
return id;
|
2023-02-10 06:07:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 19:18:55 +00:00
|
|
|
|
public StoredEventInfo? GetEvent(long seqId) {
|
|
|
|
|
return Events.TryGetValue(seqId, out StoredEventInfo? evt) ? evt : null;
|
2023-02-10 06:07:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-23 21:46:49 +00:00
|
|
|
|
public void RemoveEvent(StoredEventInfo evt) {
|
|
|
|
|
Events.Remove(evt.Id);
|
2023-02-10 06:07:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-23 21:46:49 +00:00
|
|
|
|
public IEnumerable<StoredEventInfo> GetChannelEventLog(string channelName, int amount = 20, int offset = 0) {
|
|
|
|
|
IEnumerable<StoredEventInfo> subset = Events.Values.Where(ev => ev.ChannelName == channelName);
|
2023-02-10 06:07:59 +00:00
|
|
|
|
|
|
|
|
|
int start = subset.Count() - offset - amount;
|
|
|
|
|
if(start < 0) {
|
|
|
|
|
amount += start;
|
|
|
|
|
start = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return subset.Skip(start).Take(amount).ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|