From e3a0536fcde9e88bac37a258ec50359dc8678e75 Mon Sep 17 00:00:00 2001 From: DarkRRb <177549718+CwkDark@users.noreply.github.com> Date: Mon, 30 Sep 2024 05:30:27 +0800 Subject: [PATCH] Get the MessageChain for the Reply through the BotContext. --- Lagrange.Core | 2 +- .../src/Converters/MessageConverter.cs | 40 ++++++++--------- .../src/Converters/NoticeEventConverter.cs | 4 +- .../Kritor/Grpc/Firend/KritorFirendService.cs | 1 - .../Kritor/Grpc/Group/KritorGroupService.cs | 2 - .../Grpc/Message/KritorMessageService.cs | 44 ++++++------------- .../src/Utilities/BotContextUtility.cs | 29 ++++++++++++ .../src/Utilities/MessageIdUtility.cs | 16 ++++++- 8 files changed, 79 insertions(+), 59 deletions(-) create mode 100644 Lagrange.Kritor/src/Utilities/BotContextUtility.cs diff --git a/Lagrange.Core b/Lagrange.Core index c007401..a1b9e55 160000 --- a/Lagrange.Core +++ b/Lagrange.Core @@ -1 +1 @@ -Subproject commit c0074010ec8c8243821702a7ae3ebd5ed9b37b67 +Subproject commit a1b9e5577bf0963dd5914fd3938e2e4f8d166f76 diff --git a/Lagrange.Kritor/src/Converters/MessageConverter.cs b/Lagrange.Kritor/src/Converters/MessageConverter.cs index 3ffdf1c..41b8062 100644 --- a/Lagrange.Kritor/src/Converters/MessageConverter.cs +++ b/Lagrange.Kritor/src/Converters/MessageConverter.cs @@ -2,9 +2,12 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; +using System.Threading; +using System.Threading.Tasks; using System.Xml; using Google.Protobuf.Collections; using Kritor.Common; +using Lagrange.Core; using Lagrange.Core.Message; using Lagrange.Core.Message.Entity; using Lagrange.Kritor.Utilities; @@ -156,23 +159,23 @@ public static bool TryToForwardElement(this XmlDocument document, [NotNullWhen(t return true; } - public static MessageChain ToGroupChain(this RepeatedField elements, uint groupUin) { - return elements.Aggregate( - MessageBuilder.Group(groupUin), - (builder, element) => builder.AddElement(element), - (builder) => builder.Build() - ); + public static async Task ToGroupChainAsync(this RepeatedField elements, BotContext bot, uint groupUin, CancellationToken token) { + MessageBuilder builder = MessageBuilder.Group(groupUin); + foreach (Element element in elements) { + await builder.AddElementAsync(bot, element, token); + } + return builder.Build(); } - public static MessageChain ToFriendChain(this RepeatedField elements, uint userUin) { - return elements.Aggregate( - MessageBuilder.Friend(userUin), - (builder, element) => builder.AddElement(element), - (builder) => builder.Build() - ); + public static async Task ToFriendChainAsync(this RepeatedField elements, BotContext bot, uint userUin, CancellationToken token) { + MessageBuilder builder = MessageBuilder.Friend(userUin); + foreach (Element element in elements) { + await builder.AddElementAsync(bot, element, token); + } + return builder.Build(); } - public static MessageBuilder AddElement(this MessageBuilder builder, Element element) { + public static async Task AddElementAsync(this MessageBuilder builder, BotContext bot, Element element, CancellationToken token) { return element.Type switch { Element.Types.ElementType.Unspecified => throw new NotSupportedException( $"Not supported ElementType({Element.Types.ElementType.Unspecified})" @@ -188,12 +191,7 @@ public static MessageBuilder AddElement(this MessageBuilder builder, Element ele Element.Types.ElementType.BubbleFace => throw new NotSupportedException( $"Not supported ElementType({Element.Types.ElementType.BubbleFace})" ), - Element.Types.ElementType.Reply => builder.Add(new ForwardEntity { - Time = DateTimeOffset.Now.DateTime, - Sequence = MessageIdUtility.GetSequence(element.Reply.MessageId), - ClientSequence = 0, // Private reply need - TargetUin = 1 // Tail at - }), + Element.Types.ElementType.Reply => builder.Forward(await bot.GetMessageByMessageIdAsync(element.Reply.MessageId, token)), Element.Types.ElementType.Image => element.Image.DataCase switch { ImageElement.DataOneofCase.None => throw new NotSupportedException( $"Not supported DataOneofCase({ImageElement.DataOneofCase.None})" @@ -293,8 +291,8 @@ public static MessageBuilder AddElement(this MessageBuilder builder, Element ele Type = button.Action.Type, Permission = new Permission { Type = button.Action.Permission.Type, - SpecifyRoleIds = button.Action.Permission.RoleIds.ToList(), - SpecifyUserIds = button.Action.Permission.UserIds.ToList(), + SpecifyRoleIds = [.. button.Action.Permission.RoleIds], + SpecifyUserIds = [.. button.Action.Permission.UserIds], }, UnsupportTips = button.Action.UnsupportedTips, Data = button.Action.Data, diff --git a/Lagrange.Kritor/src/Converters/NoticeEventConverter.cs b/Lagrange.Kritor/src/Converters/NoticeEventConverter.cs index 61ccc51..a7d9b22 100644 --- a/Lagrange.Kritor/src/Converters/NoticeEventConverter.cs +++ b/Lagrange.Kritor/src/Converters/NoticeEventConverter.cs @@ -35,10 +35,10 @@ public static EventStructure ToNoticeEvent(this EventBase @event) { // NoticeId = Guid.NewGuid().ToString(), // PrivateRecall = new PrivateRecallNotice { // OperatorUin = recall.FriendUin, - // MessageId = MessageIdUtility.BuildPrivateMessageId(recall.FriendUin, recall.Sequence) + // MessageId = MessageIdUtility.BuildPrivateMessageId(recall.FriendUin, recall.ClientSequence) // } // } - // } + // }, GroupPokeEvent poke => new EventStructure { Type = EventType.Notice, Notice = new NoticeEvent { diff --git a/Lagrange.Kritor/src/Services/Kritor/Grpc/Firend/KritorFirendService.cs b/Lagrange.Kritor/src/Services/Kritor/Grpc/Firend/KritorFirendService.cs index 54b93a6..1fc7ba1 100644 --- a/Lagrange.Kritor/src/Services/Kritor/Grpc/Firend/KritorFirendService.cs +++ b/Lagrange.Kritor/src/Services/Kritor/Grpc/Firend/KritorFirendService.cs @@ -7,7 +7,6 @@ using Lagrange.Core; using Lagrange.Core.Common.Entity; using Lagrange.Core.Common.Interface.Api; -using Lagrange.Core.Message.Entity; using static Kritor.Friend.FriendService; namespace Lagrange.Kritor.Services.Kritor.Grpc.Friend; diff --git a/Lagrange.Kritor/src/Services/Kritor/Grpc/Group/KritorGroupService.cs b/Lagrange.Kritor/src/Services/Kritor/Grpc/Group/KritorGroupService.cs index 15f4521..3d759d1 100644 --- a/Lagrange.Kritor/src/Services/Kritor/Grpc/Group/KritorGroupService.cs +++ b/Lagrange.Kritor/src/Services/Kritor/Grpc/Group/KritorGroupService.cs @@ -7,9 +7,7 @@ using Lagrange.Core; using Lagrange.Core.Common.Entity; using Lagrange.Core.Common.Interface.Api; -using Lagrange.Core.Message.Entity; using static Kritor.Group.GroupService; -using MSFile = System.IO.File; namespace Lagrange.Kritor.Services.Kritor.Grpc.Group; diff --git a/Lagrange.Kritor/src/Services/Kritor/Grpc/Message/KritorMessageService.cs b/Lagrange.Kritor/src/Services/Kritor/Grpc/Message/KritorMessageService.cs index 400417c..da6e179 100644 --- a/Lagrange.Kritor/src/Services/Kritor/Grpc/Message/KritorMessageService.cs +++ b/Lagrange.Kritor/src/Services/Kritor/Grpc/Message/KritorMessageService.cs @@ -16,46 +16,30 @@ namespace Lagrange.Kritor.Services.Kritor.Grpc.Message; public class KritorMessageService(BotContext bot) : MessageServiceBase { private readonly BotContext _bot = bot; - private async Task SendGroupMessage(SendMessageRequest request, CancellationToken token) { - uint groupUin = uint.Parse(request.Contact.Peer); + public override async Task SendMessage(SendMessageRequest request, ServerCallContext context) { + uint uin = uint.Parse(request.Contact.Peer); - MessageResult result = await _bot.SendMessage(request.Elements.ToGroupChain(groupUin)); - if (result.Result != 0 || result.Sequence == null) { - throw new Exception($"Send group message failed"); - } - - return new SendMessageResponse { - MessageId = MessageIdUtility.BuildGroupMessageId(groupUin, (ulong)result.Sequence), - MessageTime = result.Timestamp + MessageChain chain = request.Contact.Scene switch { + Scene.Unspecified => throw new NotSupportedException($"Not supported Scene({Scene.Unspecified})"), + Scene.Group => await request.Elements.ToGroupChainAsync(_bot, uin, context.CancellationToken), + Scene.Friend => await request.Elements.ToFriendChainAsync(_bot, uin, context.CancellationToken), + Scene.Guild => throw new NotSupportedException($"Not supported Scene({Scene.Guild})"), + Scene.StrangerFromGroup => throw new NotSupportedException($"Not supported Scene({Scene.StrangerFromGroup})"), + Scene.Nearby => throw new NotSupportedException($"Not supported Scene({Scene.Nearby})"), + Scene.Stranger => throw new NotSupportedException($"Not supported Scene({Scene.Stranger})"), + Scene unknown => throw new NotSupportedException($"Not supported Scene({unknown})"), }; - } - - private async Task SendFriendMessage(SendMessageRequest request, CancellationToken token) { - uint groupUin = uint.Parse(request.Contact.Peer); - MessageResult result = await _bot.SendMessage(request.Elements.ToFriendChain(groupUin)); + MessageResult result = await _bot.SendMessage(chain); if (result.Result != 0 || result.Sequence == null) { - throw new Exception($"Send friend message failed"); + throw new Exception($"({result.Result}) Send message failed"); } return new SendMessageResponse { - MessageId = MessageIdUtility.BuildGroupMessageId(groupUin, (ulong)result.Sequence), + MessageId = MessageIdUtility.BuildGroupMessageId(uin, (ulong)result.Sequence), MessageTime = result.Timestamp }; } - public override Task SendMessage(SendMessageRequest request, ServerCallContext context) { - return request.Contact.Scene switch { - Scene.Unspecified => throw new NotSupportedException($"Not supported Scene({Scene.Unspecified})"), - Scene.Group => SendGroupMessage(request, context.CancellationToken), - Scene.Friend => SendFriendMessage(request, context.CancellationToken), - Scene.Guild => throw new NotSupportedException($"Not supported Scene({Scene.Guild})"), - Scene.StrangerFromGroup => throw new NotSupportedException($"Not supported Scene({Scene.StrangerFromGroup})"), - Scene.Nearby => throw new NotSupportedException($"Not supported Scene({Scene.Nearby})"), - Scene.Stranger => throw new NotSupportedException($"Not supported Scene({Scene.Stranger})"), - Scene unknown => throw new NotSupportedException($"Not supported Scene({unknown})"), - }; - } - // TODO I'm tired. I'll do it next time. } \ No newline at end of file diff --git a/Lagrange.Kritor/src/Utilities/BotContextUtility.cs b/Lagrange.Kritor/src/Utilities/BotContextUtility.cs new file mode 100644 index 0000000..b5c500a --- /dev/null +++ b/Lagrange.Kritor/src/Utilities/BotContextUtility.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Lagrange.Core; +using Lagrange.Core.Common.Interface.Api; +using Lagrange.Core.Message; + +namespace Lagrange.Kritor.Utilities; + +public static class BotContextUtility { + public static async Task GetMessageByMessageIdAsync(this BotContext bot, string id, CancellationToken token) { + uint sequence = MessageIdUtility.GetSequence(id); + uint uin = MessageIdUtility.GetUin(id); + + List? chains; + if (MessageIdUtility.IsGroup(id)) { + chains = await bot.GetGroupMessage(uin, sequence, sequence); + } else if (MessageIdUtility.IsGroup(id)) { + chains = await bot.GetC2cMessage(uin, sequence, sequence); + } else throw new NotSupportedException($"Not supported message id({id})"); + + if (chains == null || chains.Count != 1) { + throw new Exception($"Get message chain by message id({id}) failed"); + } + + return chains[0]; + } +} \ No newline at end of file diff --git a/Lagrange.Kritor/src/Utilities/MessageIdUtility.cs b/Lagrange.Kritor/src/Utilities/MessageIdUtility.cs index 70ba75a..f134808 100644 --- a/Lagrange.Kritor/src/Utilities/MessageIdUtility.cs +++ b/Lagrange.Kritor/src/Utilities/MessageIdUtility.cs @@ -1,12 +1,24 @@ namespace Lagrange.Kritor.Utilities; public static class MessageIdUtility { + public static string BuildPrivateMessageId(ulong uin, ulong sequence) { + return $"p{uin:D20}{sequence:D20}"; + } + public static string BuildGroupMessageId(ulong uin, ulong sequence) { return $"g{uin:D20}{sequence:D20}"; } - public static string BuildPrivateMessageId(ulong uin, ulong sequence) { - return $"p{uin:D20}{sequence:D20}"; + public static bool IsGroup(string id) { + return id.StartsWith('g'); + } + + public static bool IsPrivate(string id) { + return id.StartsWith('p'); + } + + public static uint GetUin(string id) { + return uint.Parse(id[1..21]); } public static uint GetSequence(string id) {