sharp-chat/SharpChatCommon/EventStorage/MariaDBEventStorage.cs

145 lines
6.9 KiB
C#
Raw Normal View History

2022-08-30 15:00:58 +00:00
using MySqlConnector;
2024-05-24 03:44:20 +00:00
using SharpChat.Events;
2022-08-30 15:00:58 +00:00
using System;
using System.Collections.Generic;
2024-05-24 03:44:20 +00:00
using System.Reflection;
2022-08-30 15:00:58 +00:00
using System.Text;
using System.Text.Json;
namespace SharpChat.EventStorage {
public partial class MariaDBEventStorage : IEventStorage {
private string ConnectionString { get; }
2022-08-30 15:00:58 +00:00
2024-05-24 03:44:20 +00:00
private readonly JsonSerializerOptions jsonOpts = new() {
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault,
};
private static readonly Dictionary<string, Type> EventDataTypes = new();
static MariaDBEventStorage() {
foreach(Type type in Assembly.GetExecutingAssembly().GetTypes()) {
ChatEventDataForAttribute? forAttr = type.GetCustomAttribute<ChatEventDataForAttribute>();
if(forAttr == null)
continue;
if(EventDataTypes.ContainsKey(forAttr.EventName))
throw new InvalidOperationException($"Attempted to register more than one data type for {forAttr.EventName}!");
EventDataTypes.Add(forAttr.EventName, type);
}
}
public MariaDBEventStorage(string connString) {
ConnectionString = connString;
2022-08-30 15:00:58 +00:00
}
2024-05-24 03:44:20 +00:00
public void HandleEvent(ChatEventInfo info) {
MySqlParameter dataParam = new("data", MySqlDbType.Blob) {
Value = JsonSerializer.SerializeToUtf8Bytes(info.Data, info.Data.GetType(), jsonOpts)
};
2022-08-30 15:00:58 +00:00
RunCommand(
2024-05-24 00:23:31 +00:00
"INSERT INTO `sqc_events` (`event_id`, `event_created`, `event_type`, `event_target`, `event_data`"
+ ", `event_sender`, `event_sender_name`, `event_sender_colour`, `event_sender_rank`, `event_sender_nick`, `event_sender_perms`)"
2024-05-24 00:23:31 +00:00
+ " VALUES (@id, NOW(), @type, @target, @data"
+ ", @sender, @sender_name, @sender_colour, @sender_rank, @sender_nick, @sender_perms)",
2024-05-24 03:44:20 +00:00
new MySqlParameter("id", info.Id),
new MySqlParameter("type", info.Type),
new MySqlParameter("target", string.IsNullOrWhiteSpace(info.ChannelName) ? null : info.ChannelName),
dataParam,
new MySqlParameter("sender", info.SenderId < 1 ? null : info.SenderId),
new MySqlParameter("sender_name", info.SenderName),
new MySqlParameter("sender_colour", info.SenderColour.ToMisuzu()),
new MySqlParameter("sender_rank", info.SenderRank),
new MySqlParameter("sender_nick", info.SenderNickName),
new MySqlParameter("sender_perms", info.SenderPerms)
);
2024-05-24 03:44:20 +00:00
if(info.Type.Equals("msg:delete") && info.Data is MessageDeleteEventData msgDelete)
RunCommand(
"UPDATE IGNORE `sqc_events` SET `event_deleted` = NOW() WHERE `event_id` = @id AND `event_deleted` IS NULL",
new MySqlParameter("id", msgDelete.MessageId)
);
}
2024-05-24 03:44:20 +00:00
public ChatEventInfo? GetEvent(long eventId) {
try {
2024-05-10 19:18:55 +00:00
using MySqlDataReader? reader = RunQuery(
2024-05-24 00:23:31 +00:00
"SELECT `event_id`, `event_type`, `event_data`, `event_target`"
+ ", `event_sender`, `event_sender_name`, `event_sender_colour`, `event_sender_rank`, `event_sender_nick`, `event_sender_perms`"
+ ", UNIX_TIMESTAMP(`event_created`) AS `event_created`"
+ ", UNIX_TIMESTAMP(`event_deleted`) AS `event_deleted`"
+ " FROM `sqc_events`"
+ " WHERE `event_id` = @id",
2024-05-24 03:44:20 +00:00
new MySqlParameter("id", eventId)
);
2024-05-10 19:18:55 +00:00
if(reader != null)
while(reader.Read()) {
2024-05-24 03:44:20 +00:00
ChatEventInfo evt = ReadEvent(reader);
2024-05-10 19:18:55 +00:00
if(evt != null)
return evt;
}
} catch(MySqlException ex) {
Logger.Write(ex);
}
return null;
2022-08-30 15:00:58 +00:00
}
2024-05-24 03:44:20 +00:00
private static ChatEventInfo ReadEvent(MySqlDataReader reader) {
string eventType = Encoding.ASCII.GetString((byte[])reader["event_type"]);
ChatEventData eventData = EventDataTypes.ContainsKey(eventType)
? (ChatEventData)(JsonSerializer.Deserialize((byte[])reader["event_data"], EventDataTypes[eventType]) ?? ChatEventData.EmptyInstance)
: ChatEventData.EmptyInstance;
return new ChatEventInfo(
reader.GetInt64("event_id"),
2024-05-24 03:44:20 +00:00
eventType,
DateTimeOffset.FromUnixTimeSeconds(reader.GetInt32("event_created")),
2024-05-24 03:44:20 +00:00
reader.IsDBNull(reader.GetOrdinal("event_target")) ? string.Empty : Encoding.ASCII.GetString((byte[])reader["event_target"]),
reader.IsDBNull(reader.GetOrdinal("event_sender")) ? -1 : reader.GetInt64("event_sender"),
reader.IsDBNull(reader.GetOrdinal("event_sender_name")) ? string.Empty : reader.GetString("event_sender_name"),
reader.IsDBNull(reader.GetOrdinal("event_sender_colour")) ? Colour.None : Colour.FromMisuzu(reader.GetInt32("event_sender_colour")),
reader.GetInt32("event_sender_rank"),
reader.IsDBNull(reader.GetOrdinal("event_sender_nick")) ? null : reader.GetString("event_sender_nick"),
(UserPermissions)reader.GetInt32("event_sender_perms"),
eventData
);
2022-08-30 15:00:58 +00:00
}
2024-05-24 03:44:20 +00:00
public IEnumerable<ChatEventInfo> GetChannelEventLog(string channelName, int amount = 20, int startAt = 0) {
List<ChatEventInfo> events = new();
2022-08-30 15:00:58 +00:00
try {
2024-05-10 19:18:55 +00:00
using MySqlDataReader? reader = RunQuery(
2024-05-24 00:23:31 +00:00
"SELECT `event_id`, `event_type`, `event_data`, `event_target`"
+ ", `event_sender`, `event_sender_name`, `event_sender_colour`, `event_sender_rank`, `event_sender_nick`, `event_sender_perms`"
+ ", UNIX_TIMESTAMP(`event_created`) AS `event_created`"
+ ", UNIX_TIMESTAMP(`event_deleted`) AS `event_deleted`"
+ " FROM `sqc_events`"
+ " WHERE `event_deleted` IS NULL AND (`event_target` = @target OR `event_target` IS NULL)"
+ " AND `event_id` > @offset"
+ " ORDER BY `event_id` DESC"
+ " LIMIT @amount",
2023-02-16 22:47:30 +00:00
new MySqlParameter("target", channelName),
new MySqlParameter("amount", amount),
2024-05-24 00:23:31 +00:00
new MySqlParameter("offset", startAt)
2022-08-30 15:00:58 +00:00
);
2024-05-10 19:18:55 +00:00
if(reader != null)
while(reader.Read()) {
2024-05-24 03:44:20 +00:00
ChatEventInfo evt = ReadEvent(reader);
2024-05-10 19:18:55 +00:00
if(evt != null)
events.Add(evt);
}
2022-08-30 15:00:58 +00:00
} catch(MySqlException ex) {
Logger.Write(ex);
}
events.Reverse();
2022-08-30 15:00:58 +00:00
return events;
}
}
}