Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OneBot] Migrating to Realm #752

Merged
merged 8 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Lagrange.Core/Common/Interface/Api/GroupExt.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Lagrange.Core.Common.Entity;
using Lagrange.Core.Event.EventArg;
using Lagrange.Core.Message;
using Lagrange.Core.Message.Entity;

Expand Down
2 changes: 1 addition & 1 deletion Lagrange.Core/Event/EventInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal void PostEvent(EventBase e)
}
catch (Exception ex)
{
PostEvent(new BotLogEvent(Tag, LogLevel.Exception, $"{ex.StackTrace}\n{ex.Message}"));
PostEvent(new BotLogEvent(Tag, LogLevel.Exception, $"{ex}"));
}
});
}
Expand Down
4 changes: 1 addition & 3 deletions Lagrange.Core/Message/Entity/BounceFaceEntity.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Text;
using Lagrange.Core.Common.Entity;
using Lagrange.Core.Common.Entity;
using Lagrange.Core.Internal.Packets.Message.Element;
using Lagrange.Core.Internal.Packets.Message.Element.Implementation;
using Lagrange.Core.Internal.Packets.Message.Element.Implementation.Extra;
using Lagrange.Core.Utility.Extension;
using ProtoBuf;

namespace Lagrange.Core.Message.Entity;
Expand Down
35 changes: 20 additions & 15 deletions Lagrange.OneBot/Core/Notify/NotifyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
using Lagrange.OneBot.Core.Entity.Notify;
using Lagrange.OneBot.Core.Network;
using Lagrange.OneBot.Database;
using LiteDB;
using Lagrange.OneBot.Utility;
using Microsoft.Extensions.Logging;
using static Lagrange.Core.Message.MessageChain;

namespace Lagrange.OneBot.Core.Notify;

public sealed class NotifyService(BotContext bot, ILogger<NotifyService> logger, LagrangeWebSvcCollection service, LiteDatabase database)
public sealed class NotifyService(BotContext bot, ILogger<NotifyService> logger, LagrangeWebSvcCollection service, RealmHelper realm)
{
public void RegisterEvents()
{
Expand Down Expand Up @@ -147,17 +148,18 @@ await service.SendJsonAsync(new OneBotGroupRecall(bot.BotUin)
{
logger.LogInformation(@event.ToString());

var collection = database.GetCollection<MessageRecord>();
var record = collection.FindOne(Query.And(
Query.EQ("FriendUin", new BsonValue(@event.FriendUin)),
Query.EQ("ClientSequence", new BsonValue(@event.ClientSequence)),
Query.EQ("MessageId", new BsonValue(0x01000000L << 32 | @event.Random))
));
var sequence = realm.Do(realm => realm.All<MessageRecord>()
.First(record => record.TypeInt == (int)MessageType.Friend
&& record.FromUinLong == @event.FriendUin
&& record.ClientSequenceLong == @event.ClientSequence
&& record.MessageIdLong == (0x01000000L << 32 | @event.Random)
)
.Sequence);

await service.SendJsonAsync(new OneBotFriendRecall(bot.BotUin)
{
UserId = @event.FriendUin,
MessageId = MessageRecord.CalcMessageHash(@event.Random, record.Sequence),
MessageId = MessageRecord.CalcMessageHash(@event.Random, (uint)sequence),
Tip = @event.Tip
});
};
Expand Down Expand Up @@ -207,24 +209,27 @@ await service.SendJsonAsync(new OneBotGroupEssence(bot.BotUin)
{
logger.LogInformation(@event.ToString());

var record = database.GetCollection<MessageRecord>().FindOne(Query.And(
Query.EQ("GroupUin", new BsonValue(@event.TargetGroupUin)),
Query.EQ("Sequence", new BsonValue(@event.TargetSequence))
));
var id = realm.Do(realm => realm.All<MessageRecord>()
.FirstOrDefault(record => record.TypeInt == (int)MessageType.Group
&& record.ToUinLong == @event.TargetGroupUin
&& record.SequenceLong == @event.TargetSequence)?
.Id);

if (record == null)
if (id == null)
{
logger.LogInformation(
"Unable to find the corresponding message using GroupUin: {} and Sequence: {}",
@event.TargetGroupUin,
@event.TargetSequence
);

return;
}

await service.SendJsonAsync(new OneBotGroupReaction(
bot.BotUin,
@event.TargetGroupUin,
record?.MessageHash ?? 0,
id.Value,
@event.OperatorUin,
@event.IsAdd ? "add" : "remove",
@event.Code,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using Lagrange.Core;
using Lagrange.Core.Message;
using Lagrange.Core.Common.Interface.Api;
using Lagrange.OneBot.Core.Entity.Action;
using Lagrange.OneBot.Core.Operation.Converters;
using Lagrange.OneBot.Database;
using LiteDB;
using Lagrange.OneBot.Utility;

namespace Lagrange.OneBot.Core.Operation.Generic;


[Operation(".join_friend_emoji_chain")]
public class FriendJoinEmojiChainOperation(LiteDatabase database) : IOperation
public class FriendJoinEmojiChainOperation(RealmHelper realm) : IOperation
{
public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? payload)
{
if (payload.Deserialize<OneBotPrivateJoinEmojiChain>(SerializerOptions.DefaultOptions) is { } data)
{
var message = (MessageChain)database.GetCollection<MessageRecord>().FindById(data.MessageId);
bool res = await context.FriendJoinEmojiChain(data.UserId, data.EmojiId, message.Sequence);
var sequence = realm.Do(realm => realm.All<MessageRecord>()
.First(record => record.Id == data.MessageId)
.Sequence);

bool res = await context.FriendJoinEmojiChain(data.UserId, data.EmojiId, (uint)sequence);
return new OneBotResult(null, res ? 0 : -1, res ? "ok" : "failed");
}
throw new Exception();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using Lagrange.Core;
using Lagrange.Core.Message;
using Lagrange.Core.Common.Interface.Api;
using Lagrange.OneBot.Core.Entity.Action;
using Lagrange.OneBot.Core.Operation.Converters;
using Lagrange.OneBot.Database;
using LiteDB;
using Lagrange.OneBot.Utility;

namespace Lagrange.OneBot.Core.Operation.Generic;


[Operation(".join_group_emoji_chain")]
public class GroupJoinEmojiChainOperation(LiteDatabase database) : IOperation
public class GroupJoinEmojiChainOperation(RealmHelper realm) : IOperation
{
public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? payload)
{
if (payload.Deserialize<OneBotGroupJoinEmojiChain>(SerializerOptions.DefaultOptions) is { } data)
{
var message = (MessageChain)database.GetCollection<MessageRecord>().FindById(data.MessageId);
bool res = await context.GroupJoinEmojiChain(data.GroupId, data.EmojiId, message.Sequence);
var sequence = realm.Do(realm => realm.All<MessageRecord>()
.First(record => record.Id == data.MessageId)
.Sequence);

bool res = await context.GroupJoinEmojiChain(data.GroupId, data.EmojiId, (uint)sequence);
return new OneBotResult(null, res ? 0 : -1, res ? "ok" : "failed");
}
throw new Exception();
Expand Down
16 changes: 11 additions & 5 deletions Lagrange.OneBot/Core/Operation/Group/SetGroupReactionOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,30 @@
using System.Text.Json.Nodes;
using Lagrange.Core;
using Lagrange.Core.Common.Interface.Api;
using Lagrange.Core.Message;
using Lagrange.OneBot.Core.Entity.Action;
using Lagrange.OneBot.Core.Operation.Converters;
using Lagrange.OneBot.Database;
using LiteDB;
using Lagrange.OneBot.Utility;

namespace Lagrange.OneBot.Core.Operation.Group;

[Operation("set_group_reaction")]
public class SetGroupReactionOperation(LiteDatabase database) : IOperation
public class SetGroupReactionOperation(RealmHelper realm) : IOperation
{
public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? payload)
{
if (payload.Deserialize<OneBotSetGroupReaction>(SerializerOptions.DefaultOptions) is { } data)
{
var message = (MessageChain)database.GetCollection<MessageRecord>().FindById(data.MessageId);
var sequence = realm.Do(realm => realm.All<MessageRecord>()
.First(record => record.Id == data.MessageId)
.Sequence);

bool result = await context.GroupSetMessageReaction(data.GroupId, message.Sequence, data.Code, data.IsAdd);
bool result = await context.GroupSetMessageReaction(
data.GroupId,
(uint)sequence,
data.Code,
data.IsAdd
);
return new OneBotResult(null, result ? 0 : 1, result ? "ok" : "failed");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
using Lagrange.OneBot.Core.Entity.Action;
using Lagrange.OneBot.Core.Operation.Converters;
using Lagrange.OneBot.Database;
using LiteDB;
using Lagrange.OneBot.Utility;

namespace Lagrange.OneBot.Core.Operation.Message;

[Operation("delete_essence_msg")]
public class DeleteEssenceMessageOperation(LiteDatabase database) : IOperation
public class DeleteEssenceMessageOperation(RealmHelper realm) : IOperation
{
public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? payload)
{
if (payload.Deserialize<OneBotGetMessage>(SerializerOptions.DefaultOptions) is { } getMsg)
{
var record = database.GetCollection<MessageRecord>().FindById(getMsg.MessageId);
var chain = (MessageChain)record;
var chain = realm.Do<MessageChain>(realm => realm.All<MessageRecord>()
.First(record => record.Id == getMsg.MessageId));

bool result = await context.RemoveEssenceMessage(chain);
return new OneBotResult(null, result ? 0 : 1, result ? "ok" : "failed");
}
Expand Down
17 changes: 11 additions & 6 deletions Lagrange.OneBot/Core/Operation/Message/DeleteMessageOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,27 @@
using Lagrange.OneBot.Core.Entity.Action;
using Lagrange.OneBot.Core.Operation.Converters;
using Lagrange.OneBot.Database;
using LiteDB;
using Lagrange.OneBot.Utility;

namespace Lagrange.OneBot.Core.Operation.Message;

[Operation("delete_msg")]
public class DeleteMessageOperation(LiteDatabase database) : IOperation
public class DeleteMessageOperation(RealmHelper realm) : IOperation
{
public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? payload)
{
if (payload.Deserialize<OneBotGetMessage>(SerializerOptions.DefaultOptions) is { } getMsg)
{
var record = database.GetCollection<MessageRecord>().FindById(getMsg.MessageId);
var chain = (MessageChain)record;
var chain = realm.Do<MessageChain>(realm => realm.All<MessageRecord>()
.First(record => record.Id == getMsg.MessageId));

if (chain.IsGroup && await context.RecallGroupMessage(chain)) return new OneBotResult(null, 0, "ok");
if (!chain.IsGroup && await context.RecallFriendMessage(chain)) return new OneBotResult(null, 0, "ok");
if (chain.Type switch
{
MessageChain.MessageType.Group => await context.RecallGroupMessage(chain),
MessageChain.MessageType.Temp => throw new NotSupportedException(),
MessageChain.MessageType.Friend => await context.RecallFriendMessage(chain),
_ => throw new NotImplementedException(),
}) return new OneBotResult(null, 0, "ok");
}

throw new Exception();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@
using Lagrange.OneBot.Core.Operation.Converters;
using Lagrange.OneBot.Database;
using Lagrange.OneBot.Message;
using LiteDB;
using Lagrange.OneBot.Utility;

namespace Lagrange.OneBot.Core.Operation.Message;

[Operation("get_friend_msg_history")]
public class GetFriendMessageHistoryOperation(LiteDatabase database, MessageService message) : IOperation
public class GetFriendMessageHistoryOperation(RealmHelper realm, MessageService message) : IOperation
{
public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? payload)
{
if (payload.Deserialize<OneBotFriendMsgHistory>(SerializerOptions.DefaultOptions) is { } history)
{
var collection = database.GetCollection<MessageRecord>();
var record = history.MessageId == 0
? collection.Find(x => x.FriendUin == history.UserId).OrderByDescending(x => x.Time).First()
: collection.FindById(history.MessageId);
var chain = (MessageChain)record;
var chain = realm.Do<MessageChain>(realm => history.MessageId == 0
? realm.All<MessageRecord>()
.Where(record => record.FromUin == history.UserId)
.OrderByDescending(record => record.Time)
.First()
: realm.All<MessageRecord>()
.First(record => record.Id == history.MessageId));

if (await context.GetRoamMessage(chain, history.Count) is { } results)
{
Expand All @@ -33,7 +35,7 @@ public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? pa
return new OneBotResult(new OneBotFriendMsgHistoryResponse(messages), 0, "ok");
}
}

throw new Exception();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,34 @@
using System.Text.Json.Nodes;
using Lagrange.Core;
using Lagrange.Core.Common.Interface.Api;
using Lagrange.Core.Message;
using Lagrange.OneBot.Core.Entity.Action;
using Lagrange.OneBot.Core.Entity.Message;
using Lagrange.OneBot.Core.Operation.Converters;
using Lagrange.OneBot.Database;
using Lagrange.OneBot.Message;
using LiteDB;
using Lagrange.OneBot.Utility;

namespace Lagrange.OneBot.Core.Operation.Message;

[Operation("get_group_msg_history")]
public class GetGroupMessageHistoryOperation(LiteDatabase database, MessageService message) : IOperation
public class GetGroupMessageHistoryOperation(RealmHelper realm, MessageService message) : IOperation
{
public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? payload)
{
if (payload.Deserialize<OneBotGroupMsgHistory>(SerializerOptions.DefaultOptions) is { } history)
{
var collection = database.GetCollection<MessageRecord>();
var record = history.MessageId == 0
? collection.Find(x => x.GroupUin == history.GroupId).OrderByDescending(x => x.Time).First()
: collection.FindById(history.MessageId);
var chain = (MessageChain)record;
var sequence = realm.Do(realm => history.MessageId == 0
? realm.All<MessageRecord>()
.Where(record => record.ToUin == history.GroupId)
.OrderByDescending(x => x.Time)
.First()
.Sequence
: realm.All<MessageRecord>()
.First(record => record.Id == history.MessageId)
.Sequence);

if (await context.GetGroupMessage(history.GroupId, (uint)(chain.Sequence - history.Count + 1), chain.Sequence) is { } results)
uint start = (uint)(sequence - (ulong)history.Count + 1);
if (await context.GetGroupMessage(history.GroupId, start, (uint)sequence) is { } results)
{
var messages = results
.Select(x => message.ConvertToGroupMsg(context.BotUin, x))
Expand Down
Loading