|
| 1 | +using HuaJiBot.NET.Bot; |
| 2 | +using LiteDB; |
| 3 | + |
| 4 | +namespace HuaJiBot.NET.DataBase; |
| 5 | + |
| 6 | +public class GroupMessage |
| 7 | +{ |
| 8 | + [BsonId] |
| 9 | + public required string MessageId { get; set; } |
| 10 | + public required string GroupId { get; set; } |
| 11 | + public required string SenderId { get; set; } |
| 12 | + public required string SenderName { get; set; } |
| 13 | + public required DateTime Timestamp { get; set; } = DateTime.Now; |
| 14 | + public required string Content { get; set; } |
| 15 | +} |
| 16 | + |
| 17 | +public class MessageHistory : IDisposable |
| 18 | +{ |
| 19 | + private readonly LiteDatabase _db; |
| 20 | + private readonly ILiteCollection<GroupMessage> _messages; |
| 21 | + private bool _disposed = false; |
| 22 | + |
| 23 | + public MessageHistory(BotService service, string? dbPath = null) |
| 24 | + { |
| 25 | + dbPath ??= Path.Combine(service.GetPluginDataPath(), "database", "messages.db"); |
| 26 | + // Ensure directory exists |
| 27 | + Directory.CreateDirectory(Path.GetDirectoryName(dbPath)!); |
| 28 | + _db = new LiteDatabase(dbPath); |
| 29 | + _messages = _db.GetCollection<GroupMessage>("messages"); |
| 30 | + |
| 31 | + // Create indices for faster querying |
| 32 | + _messages.EnsureIndex(x => x.GroupId); |
| 33 | + _messages.EnsureIndex(x => x.SenderId); |
| 34 | + _messages.EnsureIndex(x => x.Timestamp); |
| 35 | + } |
| 36 | + |
| 37 | + public void StoreMessage(GroupMessage message) |
| 38 | + { |
| 39 | + if (message == null) |
| 40 | + throw new ArgumentNullException(nameof(message)); |
| 41 | + _messages.Insert(message); |
| 42 | + } |
| 43 | + |
| 44 | + public GroupMessage GetMessage(string messageId) |
| 45 | + { |
| 46 | + return _messages.FindById(messageId); |
| 47 | + } |
| 48 | + |
| 49 | + public IEnumerable<GroupMessage> GetGroupMessages(string groupId, int limit = 100, int skip = 0) |
| 50 | + { |
| 51 | + return _messages.Find(x => x.GroupId == groupId, skip, limit); |
| 52 | + } |
| 53 | + |
| 54 | + public IEnumerable<GroupMessage> GetUserMessages(string userId, int limit = 100, int skip = 0) |
| 55 | + { |
| 56 | + return _messages.Find(x => x.SenderId == userId, skip, limit); |
| 57 | + } |
| 58 | + |
| 59 | + public IEnumerable<GroupMessage> GetUserGroupMessages( |
| 60 | + string userId, |
| 61 | + string groupId, |
| 62 | + int limit = 100, |
| 63 | + int skip = 0 |
| 64 | + ) |
| 65 | + { |
| 66 | + return _messages.Find(x => x.SenderId == userId && x.GroupId == groupId, skip, limit); |
| 67 | + } |
| 68 | + |
| 69 | + public IEnumerable<GroupMessage> GetMessagesByTimeRange( |
| 70 | + DateTime start, |
| 71 | + DateTime end, |
| 72 | + int limit = 100, |
| 73 | + int skip = 0 |
| 74 | + ) |
| 75 | + { |
| 76 | + return _messages.Find(x => x.Timestamp >= start && x.Timestamp <= end, skip, limit); |
| 77 | + } |
| 78 | + |
| 79 | + public void DeleteMessage(string messageId) |
| 80 | + { |
| 81 | + _messages.Delete(messageId); |
| 82 | + } |
| 83 | + |
| 84 | + public bool UpdateMessage(GroupMessage message) |
| 85 | + { |
| 86 | + if (message == null) |
| 87 | + throw new ArgumentNullException(nameof(message)); |
| 88 | + |
| 89 | + return _messages.Update(message); |
| 90 | + } |
| 91 | + |
| 92 | + public void ClearGroupHistory(string groupId) |
| 93 | + { |
| 94 | + _messages.DeleteMany(x => x.GroupId == groupId); |
| 95 | + } |
| 96 | + |
| 97 | + protected virtual void Dispose(bool disposing) |
| 98 | + { |
| 99 | + if (!_disposed) |
| 100 | + { |
| 101 | + if (disposing) |
| 102 | + { |
| 103 | + _db?.Dispose(); |
| 104 | + } |
| 105 | + _disposed = true; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public void Dispose() |
| 110 | + { |
| 111 | + Dispose(true); |
| 112 | + GC.SuppressFinalize(this); |
| 113 | + } |
| 114 | + |
| 115 | + ~MessageHistory() |
| 116 | + { |
| 117 | + Dispose(false); |
| 118 | + } |
| 119 | +} |
0 commit comments